aspose file tools*
The moose likes Tomcat and the fly likes Issues logging with log4j 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 » Products » Tomcat
Bookmark "Issues logging with log4j" Watch "Issues logging with log4j" New topic
Author

Issues logging with log4j

Chaz Cowan
Greenhorn

Joined: Jun 04, 2010
Posts: 4
I have been trying to get my application to log using the log4j jar. It works fine in Dev Mode, but when I deploy it to tomcat the log files denoted by the configuration don't even seem to appear.

The following log4j.properties file is in my war directory for my app:


Just to get it working I have a simple class with static log methods only accessing the rootLogger:


On server side calls I simply have statements such as:


Are there settings within the Tomcat environment to allow application specific use of log4j, or am I simply going about this wrong? I have read in a few places about people saying to put my .properties file into WEB-INF/classes directory, but when my war file is unpacked by Tomcat I don't see that directory.
Chaz Cowan
Greenhorn

Joined: Jun 04, 2010
Posts: 4
I found a few resources detailing how the Catalina.policy file by default only allows web applications "read" access to files. Is this true?

I've been trying to alter the policy file to allow my application read/write/execute, but am so far unable to do so.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

No, you don't need to muck around with your Tomcat or JVM settings - and especially not the security settings.

It is better, however, if you give an absolute pathname and not a relative name for your logfile, and yes, the log4j.properties file must be located in the WEB-INF/classes directory of your WAR. Whether Tomcat exploded it out or not, it does have to be in the orignal WAR that you deploy.

Incidentally, just in case you didn't know - and it's not well documented - when an exploded WAR is detected in the Tomcat webapps directory and an unexploded WAR is present in the same directory, the exploded WAR is the one that gets used. Even if the unexploded WAR is newer than the exploded WAR! The safest way to deploy is either to delete both exploded and unexploded copies before updating or to turn off the option to explode.


Customer surveys are for companies who didn't pay proper attention to begin with.
Chaz Cowan
Greenhorn

Joined: Jun 04, 2010
Posts: 4
Tim Holloway wrote:No, you don't need to muck around with your Tomcat or JVM settings - and especially not the security settings.

It is better, however, if you give an absolute pathname and not a relative name for your logfile, and yes, the log4j.properties file must be located in the WEB-INF/classes directory of your WAR. Whether Tomcat exploded it out or not, it does have to be in the orignal WAR that you deploy.

Incidentally, just in case you didn't know - and it's not well documented - when an exploded WAR is detected in the Tomcat webapps directory and an unexploded WAR is present in the same directory, the exploded WAR is the one that gets used. Even if the unexploded WAR is newer than the exploded WAR! The safest way to deploy is either to delete both exploded and unexploded copies before updating or to turn off the option to explode.


Ok I removed the entry I tried to make in the catalina.policy file just in case that would mess stuff up.

I also altered my ANT build file so that it includes the log4j.properties, and the file now ends up in the WEB-INF/classes directory when Tomcat unpacks the WAR file.

I altered my log4j.properties file to use absolute paths for the log files to see if that would help(I tried it with "\\" and "/" and "\" delimeters):


It still doesn't seem to want to log anything, or even create the log files themselves. I even stopped the server, deleted everything, then copied my WAR file into webapps, then restarted it each time I changed anything. Any other ideas?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

I make no guarantees, but file/directory names with spaces in them are trouble. Instead of "Program files" &co, try creating a "C:\logs" directory and specify things like:


Since I use the XML form for log4j specs, I can't really tell if there's any general fault in the log4 properties file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issues logging with log4j
 
Similar Threads
Issues with log4j in Tomcat
thread safe logging in a servlet
Need help with log4j logging tool to log into database
Can Log4j read two properties?
Using Netbean 6.8 to create a Servlet app that queries a database