views:

45

answers:

2

Hello, i'm starting with Log4J and i want to have a default log4j.properties in our Java Web Start distributed application, which only logs errors and important events.

But if something was wrong in one client i want to have a more detailed log, the way to do this is to define an alternate log4j configuration file in this client. This can be done by specifiying the alternate config file with the log4j.configuration system property.

but... How can i define an system property for this particular client in a java web start launched application? (i know that i can define theese propeties in the .jnlp file, but this affects all clients).

Our users work in windows environment but they often have a restricted permissions computer and they can't acces My Pc->Properties-->Advanced Options-->Enviroment Variables (i'm in a spanish configured computer i don't know the exact names in english).

+1  A: 

Can you access to a defined directory on the client disk ?

If you can, you can define a convention : if no configuration file is found in the directory, the default config is used. Else, the specific configuration file is loaded.

You can do that with the PropertyConfigurator class of Log4J :

File log4jConfigFile == new File(conventionLocation);
if(log4jConfigFile.isFile() && log4jConfigFile.exists()) {
    PropertyConfigurator.configure(conventionLocation);
} else {
    PropertyConfigurator.configure(defaultEmbeddedJarLocation);
}
Benoit Courtine
It works! y use the user.home to specify the path of the alternate log4j configuration file.
Telcontar
my questions would be #1 how do you know to load the other configuration file on an error. #2 If that file exists on the machine it will load that specific configuration file every time.
controlfreak123
A: 

First off if you are using applets you should use an appender that can write to a remote location so you can actually see the errors without being physically on the local machine that the applet is running. Appender Types. Next, you need to create an appender with a threshold of whatever level you are logging the normal "access" type messages. Set the layout to whatever you desire. Then create another appender with a level of at least the level you log "errors" at as well as its own format that suits your needs as being "more detailed". That way when your code calls an error message it will use that different layout. Log4j is fairly complex but not impossible to understand. Look at the documentation at The log4j site and get your feet wet on some simple logging. After that you should be able to modify the code to get what you desire.

controlfreak123
But i want to activate the more verbose only when an error is detected
Telcontar
yes. So if you use the error method of the logger in error conditions only call the logger configured with the more verbose log settings.
controlfreak123