aspose file tools*
The moose likes Tomcat and the fly likes Any body use Log4J with Tomcat? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Any body use Log4J with Tomcat?" Watch "Any body use Log4J with Tomcat?" New topic
Author

Any body use Log4J with Tomcat?

james frain
Ranch Hand

Joined: Apr 06, 2011
Posts: 36
I had a previous post in the applications folder but as I think the issue is related to Tomcat configuration and know how this is prob a better place for this more detailed post.

Been looking at the log4j documentation in relation to Tomcat and it seems it should be straight forward to have Tomcat use Log4J with my own configuration.
I have created a Log4J.xml file and everything works fine until I deploy the app to Tomcat.
I can't tell where to put this xml file along with the dtd file ( or do I even need the dtd in Tomcat 7.0? )
Do I need to modify any of the Tomcat startup scripts to tell it about my own xml file for Log4J?
Even if I could get things to work using the Tomcat startup scripts that would do for now. (I would eventually like to get it to work with Log4J when I launch Tomcat from eclipse )
I want to log all Tomcats internal logs and my own Apps logs to the same file.

However, the file does not get created when I start the App and call a few beans.
Here is the xml file I use, just hard coded the file name to remove any doubt as to where the file might go.

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 529
    
    6
Refer this page

The idea is this.
Tomcat -> logs to a JUL (java util logging) implementation called JULI -> Output to be redirected to log4j -> Through a JUL to log4j adaptor -> output to Log4j logs

I haven't tried with an XML configuration - only with a properties file - but I think the DTD and XML should go into $CATALINA_HOME\lib, just like the properties file.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

One thing that it's important to realize is that logging for each individual webapp is a completely separate thing than logging for the Tomcat server itself. The Tomcat docs on logging are only for the Tomcat server. Log4j Logging for a webapp is just straight log4j logging with no special requirements, and the configuration file must be in the application's classpath (WEB-INF/classes) regardless of whether its the properties config file or the XML config file.

Customer surveys are for companies who didn't pay proper attention to begin with.
james frain
Ranch Hand

Joined: Apr 06, 2011
Posts: 36
I would settle for getting my own apps logging to go to my own file. tomcats logs can go where ever
What I have done so far:
Added log4j.jar to tomcat/lib
Added log4j.xml and log4j.dtd to my app/web-inf/classes folder
This is my bean

I have a breakpoint st the logger.debugger line and I hit this when I open my JSP
Below is the tomcat startup logs,



I actually hacked my .xml file so that the apender name is invalid but no warnings given about "could not find Appender xyz" so this would seem to indicate that log4J does not even attempt to parse my xml file.
Any other ideas what I could try?

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

OK. For a webapp to log, the log4j jar must be in the webapp itself (WEB-INF/lib/log4j.jar). The config must be in WEB-INF/classes. Nothing is done to Tomcat itself.
james frain
Ranch Hand

Joined: Apr 06, 2011
Posts: 36
Darn ..... Still no joy, I had tried that but forgot to mention it. I put the log4j.jar in the app/web-inf/lib folder, the log4j.xml and log4j.dtd in my app/web-inf/classes folder, removed log4j.jar from tomcat/lib,
Still no logging and no attempt to read the logging config file.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61610
    
  67

WEB-INF, not web-inf. Case counts.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
james frain
Ranch Hand

Joined: Apr 06, 2011
Posts: 36
I am using WEB-INF in uppercase, also, also not sure if it makes any difference but I have the "log4j" files spelt as is.
If anyone is interested I have attached the war file I deploy to Tomcat, maybe something jumps out in that?
james frain
Ranch Hand

Joined: Apr 06, 2011
Posts: 36
Got it ..... It was a PICNIC
Problem in Chair, not in computer.
For anyone in the future who might have the same problem I was mixing commons-logging up with log4j ( for want of a better phrase )

Modified my bean to import the apache.log4J Logger class, and not the commons-logging logger
new Code ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any body use Log4J with Tomcat?