February 05, 2004
Log4j and J2EE

I've not used log4j in anger before. Now I am, and I must say; it rocks. simple and powerful.

One question, though. Where should you put your log4j.properties file? The usual place seems to be in your /WEB-INF/classes directory, but I have two problems with that.

Firstly, it's not a damn class. OK, OK, I should probably just get over that one - /WEB-INF/classes is your basic runtime class path, whatever it may be called.

Secondly, and more seriously; if you ship your application as a WAR file, you can't get at it to edit it, which is a real problem.

Any suggestions?

Posted to Java by Simon Brunning at February 05, 2004 12:45 PM
Comments

Yeah, thats where I put my config file. As for editing in a WAR file, the webapp specification does not specifically state that WAR files should be expanded before being run - Tomcat does, as does Resin - Jetty doesn't. Best practises (and common sense) state that your application should only need the contents of the WAR file to run - placing your properties file outside of the WAR would likely cause more problems than it solves (you'd probably have to tweak the Java policy file to change the security sandbox settings).
Log4J can be configured in a variety of ways - either by property file, xml file or by direct method calls. Therefore you could expose the configuration via some kind of web interface. A big problem with this is that your classes will probably me keeping a static fional reference to their log instance - as such I'm not sure that changing configuration on the fly would actually trickle down to these already instantiated log instances.

Posted by: sam on February 5, 2004 01:41 PM

What we do is configure log4j manually when an initialisation servlet starts up.

In the Servlet.init(ServletConfig servletConfig) method we look for a servlet initialisation parameters like this

servletConfig.getInitParameter("log.config");

If we find one, we assume it is an explicit path to a log4j config file. We then invoke the
PropertyConfigurator.configureAndWatch method which will configure Log4J and monitor it for changes.

The init parameters are typically stored in the web.xml, but most servlet containers allow you to specify them at deploy time. How Tomcat does this, I'm afraid I don't know.

(Actually, what we do is quite a bit more complicated than that, but that's the general gist).

It's a cludge, but it's better than a poke-in-the-eye-with-a-long-stick.

Cheers,

Charles.

Posted by: Charles Anthony on February 5, 2004 01:54 PM

If you use the correct method for setting up log4j then it will "watch" for changes to the log file pick up any changes made.
configureAndWatch is the method name I think.

Make sure you get it to put file numbers in the log file too, %F:%L should give you the filename and line number.

Lastly, I use the xml config and put it in WEB-INF, and I use xslt to rewrite the config file for the appropriate server as part of my build script.

Posted by: Darren on February 5, 2004 02:00 PM

you can put the properties file wherever you want and have your build tool of choice, like make or ant, copy it to the WEB-INF/classes directory when you compile.

Posted by: Lou on May 11, 2004 05:01 AM

If u kindly tell me the steps for including xml properties file in WEB-INF/classes then I could get over from the problem since I am also facing the same problem.
tanking you

Posted by: Debabrata Sarkar on May 27, 2004 11:55 AM

You just put the log4j.properties file in the WEB-INF/classes directory, that's all.

Posted by: Simon Brunning on May 27, 2004 11:57 AM

You can specify the classpath when starting the server. java -cp config. Will add the config directory to the classpath. Just put the log4j.properties in that folder.

Posted by: no on July 1, 2004 09:15 AM

None of those solutions really answer the problem.
If you place the properties file into web-inf/classes (or lib) then how do you edit it at run-time?

And if you use a servlet property or dd element to point to a file location, how do you edit that xml dd without rebuilding the war either?

Posted by: James on October 22, 2004 01:04 PM

I have struggled with this same problem. The answer we came up with is to hard code one value in the web.xml which points to a file on the local server dir, this file is outside the web-app context, we use a simple properties file

Then we create a servlet that opens the property file to get at any properties. To avoid the need to re-start the server, we make the Servlet implement Runnable and then in the Run method, we watch the timestamp of the properties file.

Posted by: Scott on January 5, 2005 10:06 PM

hi everyone,
i have the following folder stru in webapps/.
1.
2.
3.
4.

now i have the log4j.jar in the folder. The log4j configuration file (I have named it config.properties) is in the folder and the generated log file should be in the folder . I have my jsp's in . but when i run the jsp which calls the method PropertyConfigurator.configure(config.properties), i get an error saying that the system cannot read the configuration file. It works only if i give the absolute path starting from my C drive where tomcat is installed. but i want to give relative path. i work on win2K. Can someone help me with the same

Posted by: Anoop Kavalloor on January 25, 2005 05:29 AM

Hi friends,

i want a help. How to use the log4j in windows environment.

Very Urgent

Durai

Posted by: Durai on March 16, 2005 09:38 AM

I have the same problem as Anoop. Please help.

Posted by: is on June 22, 2005 08:27 PM

why don't you set a system property when starting the application server. this works fine for me:
java -Dlog4j.configuration=file:/c:/conf/log4j.xml com.foo.bar

Posted by: steffen on October 11, 2005 05:57 PM

once again:

- I am using Resin 2.1.14 or even 3.0.14
- I have log4j-1.2.7.jar in my WEB-INF/lib directory.
- there is *no* log4j.xml or log4j.properties in classes dir.
- the log4j configuration is outside the web-app: file:/c:/conf/log4j.xml
- in the resin.conf I placed globally this system-property:

Well, it works fine!

Posted by: steffen on October 11, 2005 06:22 PM

wrestling sge sumis yo cock ipussy nostalgia merchanvhs movies gaymale piss masturbation stories gay ski weekend

Posted by: Sasha on October 27, 2008 07:25 AM

ebony domina naked ebony female wrestling sge sumis yo cock ipussy suckit

Posted by: Dimka on October 27, 2008 11:06 AM

ebony domina naked ebony female wrestling sge sumis yo cock ipussy suckit

Posted by: Dimka on October 27, 2008 11:06 AM

free gay jerk offf huge cocks screwing girls strip centers in florida cumsumitionjunction

Posted by: Future_dim on October 29, 2008 06:11 AM

Posted by: Future_dim on October 30, 2008 04:02 AM

Posted by: Future_dim on November 1, 2008 06:08 AM

Posted by: Future_dim on November 4, 2008 01:58 PM

Posted by: Alina_m on November 4, 2008 10:38 PM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?