Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any body use Log4J with Tomcat?

 
james frain
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1208
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18154
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
james frain
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18154
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WEB-INF, not web-inf. Case counts.
 
james frain
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic