aspose file tools*
The moose likes Servlets and the fly likes Log4j configuration in webapplication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Log4j configuration in webapplication" Watch "Log4j configuration in webapplication" New topic
Author

Log4j configuration in webapplication

John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192
Hi,

I am trying to make changes to an application which uses a natively developed logging class. We have integrated log4j with custom class, so that it behined the scene log4j is used for logging

Right now the log4j.properties is read using


We have placed the log4j.properties in root of src direcotry, i.e the code structure is as follows
src
|
|--->com
| |
| |-->testcompany
| | |
| | |-->util
| | | |
| | | | -->someutil.java
| |
|->log4j.properties


What we want to do is instead of reading from root of src we want to read from config/LogConfig directory like show below


src
|
|--->com
| |
| |-->testcompany
| | |
| | |-->util
| | | |--someutil.java
| |
| |
| |-->Config
| | |
| | |-->LogConfig
| | | |
| | | |--log4j.properties

I tried to set the same thing via propertyconfigurator, as shown below but it is not working



can someone please throw some light

Thanks for all the help

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18165
    
    8

Web applications don't have a "src" directory. Their classes are stored either in WEB-INF/classes or in a jar in WEB-INF/lib. So you should base your design planning on that rule. Once you have figured out where you want the config file to go (I put mine in WEB-INF/classes) then you can figure out how to get your deployment procedure (where you build your WAR or EAR for the application) to put it there.

(Also you might want to spell "congfig" better than that... )
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

I cannot speak for other containers (although I would think it would be similar), but for Tomcat, all that is required is that "log4j.properties" exist directly under WEB-INF/classes or WEB-INF/lib in my web application, along with the log4j jar file in WEB-INF/lib.

If log4j-xxx.jar is located on the web application's classpath (ie WEB-INF/lib), Tomcat should attempt to implement log4j. If I am missing my log4j.properties, it sends a message to standard out (catalina.out) that log4j was not properly initialized.



Also, I haven't had to call "PropertyConfigurator.configure("log4j.properties");", it's generally been enough simply to call


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192
Hi Paul & Pete,

Thanks for the response.

Paul,

I have currently in WEB-INF/classes/log4j.properties -- works

When i place the file in WEB-INF/classes/com/somecompany/config/log4j.properties -- does not work

Please let me know how can i read from com/somecompany/config directory

Thanks for all the help

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9343
    
111

As far as I know, the PropertyConfigurator uses the classloader to lookup the resource (i.e. the config file). So I guess this should work:



[My Blog] [JavaRanch Journal]
John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192
That was first obvious thing i tired, but is not working. Any ideas
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18165
    
    8

Next obvious thing to try:

Final obvious thing to try is the thing which you already had working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Log4j configuration in webapplication
 
Similar Threads
where to put log4j.properties file in visual web jsf
Loading Log4j.jar from another Jar
Unable to set classpath for log4j
Not getting Line Numbers while logging with Log4j
In Eclipse, how to add log4j.properties to classpath ?