• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

log4j with Applet

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I use log4j in Applets? Googling didn't help much.

Any help would be appriciated.

Thanks.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic