aspose file tools*
The moose likes Applets and the fly likes log4j with Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "log4j with Applet" Watch "log4j with Applet" New topic
Author

log4j with Applet

Vicky In
Greenhorn

Joined: Mar 20, 2008
Posts: 3
How do I use log4j in Applets? Googling didn't help much.

Any help would be appriciated.

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
You'd use it the same way as in any other application. Of course, since logging generally creates files -which applets are not allowed to do-, it needs to be signed or the local security policy altered. See HowCanAnAppletReadFilesOnTheLocalFileSystem for details.


Ping & DNS - my free Android networking tools app
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
Is there anyway to not include the log4j.properties file in the applet's jar itself? So that the log level can be configurable at the server, and those configuration changes affect the log level of the applet code?

I'm not sure exactly what I'm looking for, basically a way for a log4j.properties file on the server to make it's way into client applet jars, and *still* be configurable?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Originally posted by Tom Katz:
Is there anyway to not include the log4j.properties file in the applet's jar itself? So that the log level can be configurable at the server, and those configuration changes affect the log level of the applet code?
Well, sure, it just has to be in the applet's classpath. Which is a set of resources on the server (you specify them in the HTML element which describes the applet). One of those resources would be the applet's jar, assuming you decided to put the applet in a jar. There can be others.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
I think it would be the applet's codebase vs its classpath.

Using a getResource method would return a URL/InputStream you could use to read the file.
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
I may have misstated the problem a little.

I *do* have the log4j.properties file in the codebase, and it can be read by the classes there. On the server side, the log4j.configuration property can be changed to point to another file. I was hoping to be able to put multiple log4j.properties in the applet's jar file/codebase, and using applet params (or something) be able to point at a specific file.

After some researching and trying things, I dont think that's possible. If I'm wrong, please let me know. If there's other ideas for doing what I"m attempting, please let me know that also.

I think reading the server file via getResource would require extra coding, and I"m being pushed to do this without any extra coding if possible. I"m really starting to wonder if there's a way to take advantage of the flexibilty of log4j/slf4j in a web app that includes applet code.

Open for any/all suggestions!
thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Well, log4j will automatically look for log4j.properties in the classpath, which in the case of an applet is given by the "archive" property of the <applet> tag, I believe.

But I'm bemused by the whole idea of "configuring log4j at the server". Where else would you configure it? And I don't know what you mean by those "client applet jars" which you mentioned in one of your posts. Did you intend to have log4j write its logs to somewhere in the clients? (It certainly can't write to files on the server.) And if so, how do you plan to configure client locations on the server?

Basically I seem to have missed the point of what you want to do. Logging to a file on the server isn't possible, and logging to a file on the client is problematic. Of course you aren't limited to files, there are other appenders, but you didn't mention that possibility.
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
Well, glad to bemuse you - smug much?

I admit, I haven't done the best job of explaining my situation . . I'm a little lost (or wouldn't be asking)

Before converting the code to use log4j/slf4j, the applet code was writing to a log file on the client machine's desktop, by redirecting the stdout/stderr to the file. Basically, I"m hoping to set a log4j priority level for this stdout. I"m hoping to do this in a way that uses the same log level as the code that's running on the server.

Our server code sets log4j.configuration explicitly to use a log4jdebug.properties file.

So, my main issue is somehow passing that log level/priority/filename on the server TO the applet code that runs on the client. I"ve tried through applet params, and I've tried setting the log4j.configuration param in the java plugin params.

Will keep trying, thanks for the suggestions..
Tom
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
passing that log level/priority/filename on the server TO the applet code that runs on the client. I"ve tried through applet params,


Don't understand why you weren't able to pass that data via the APPLEt <PARAM tag. What was the problem? Generating the tag, getting the value or using the value?
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
The value gets set fine via the applet's PARAM. I can retrieve it in the applet code, and everything looks fine.

I"m not really using it explicitly. Which is probably my problem. I was thinking that if it was set as one of the parameters that it would be picked up by the jvm (like adding -Dlo4j.configuration), but it would seem not.

I also tried adding a specific props file to java_archive list. I get errors that indicate I should only be putting .jar files in that param (i.e. I got ZipException errors on that file.)
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
So I was a bit 'informationally stunted' on using the PARAM tag. I think I gave up that avenue of effort after I tried setting log4j.configuration via the JRE plugin. I set it there, and dumped the system values via the java console, saw the value correctly set there, and the applet classes using log4j weren't picking up the file it was pointing at.
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
I think I need to restate the problem.

* I'm using signed SLF4J/LOG4J jars to write to a log on the client machine where the applet is running. I"m replacing tons of out.println calls with slf4j calls.

* So far I've only been able to set the log level by changing the level in the log4j.properties INSIDE the applet jar. Unfortunately, I'd like the user to be able to change the log level when required. So I was wondering if there's any way to have a log4j.properties file on the server that the applet reads from? I was hoping there'd be a way to specify this server-side log4j.properties file in the applet/object tag. It seems like using the codebase/code properties is still pointing to somewhere inside the jar, but if I've got that wrong, I'd be glad to know.

thanks,
Tom

PS: the applet jar is inside a .war which is served up by Jetty.
Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
Hello,
I am not sure, if this will help you much, but if you talk about the applet param tags, you can pass all the values in these tags which need to configured for your log4j.
I see you can access them in your applet code, once in applet code you can set some standard
System.setProperty("level",param1);
System.setProperty("priority",param2);
After these property set, you can configure you log4j.xml accordingly on the basis of these System properties.

System.setProperty("LOG_PATH_PREFIX", logPathPrefix);
BasicConfigurator.resetConfiguration();
URL url = MYclass.class.getClassLoader().getResource("clientlog4j.xml");
DOMConfigurator.configure(url);



And you clientlog4j.xml may look like :
<appender name="AOCLIENT_DEBUG_WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/temp/${LOG_PATH_PREFIX}Server/log/client.log"/>
<param name="MaxFileSize" value="5MB"/>



Hope this makes sense.

Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: log4j with Applet