How to configure log4j on Tomcat for one of your apps?

This instruction is only tested with deploying one and only one app on Tomcat. I only needed one anyway. 😉

1. Create a log4j.properties file. You can use the included code below. Make sure you change the “File” parameter to the correct value for your local environment. The source of the code listed below came from here:

log4j.debug=TRUE
log4j.rootLogger=INFO, R

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/home/i/h/ihrname.ch/tomcatlogs/tomcat.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSSS} %p %t %c – %m%n

2. Copy the file to /webapps/ping-server/WEB-INF/classes directory.

3. Restart Tomcat.

Advertisements

55 thoughts on “How to configure log4j on Tomcat for one of your apps?

  1. Hey
    Thanks for a nice Info. I came to ur blogs searching for log4j configuration on tomacat without restarting it. Do u have any idea abt it. I have a site on shared webspace so restarting tomcat everytime is a pain…
    Manoj

  2. Do you have access to redeploy the web app? i.e. ask the server to reload your web app. This is necessary to load servlets. If you can do that, then I that should work also.

  3. Hi Rebin,

    If you don’t mind having a separate log4j config file for each app, you can always copy a new config file into individual web app’s classes directory.

  4. Hi,

    I had a problem with Tomcat console. I have written a log4j.xml file like the following:

    After putting this in my application ie in classes directory and running , i am not able to see the deplpyment views in the Tomcat console window ie the information what we will be getting normally.

    So please help and this is my mail id
    etta.ramesh@gmail.com

    Thanking you
    Ramesh

  5. Laxmikanth,

    You should be able to use the instructions for each separate web application. Just repeat the steps for each web application with it’s own separate configurations. Perhaps you can read Richard Unger’s comment toward the bottom of this page

    Hope this helps.

  6. I had really hard time to try to get log4j and tomcat working, and with your page i was able to resolve the issue quickly and efficiently.

    Thanks!

  7. Found a general solution.
    Simply place the log4j.xml file in tomcat/lib directory. It’s in the classpath, so log4j classes will simply pick it up.
    It will work with all web applications using log4j.

    1. But if we have only one log4j.properties /log4j.xml for all web app then all the logs will go in one file only, or do we have a way to configure log4j to write logs to diff files for diff application, using just one log4j.properties/log4j.xml

  8. Hey Dave,

    1) I just added log4j.xml and log4j.dtd in the src folder. DO I have to do anything else inorder to use logging in my application. like configuring log4j in xml etc..
    2) can I just use log4j.properties instead of log4j.xml

    Thanks

  9. hi dave,

    i included the log4j.prop file u gave and added to the path where u have specified. i have log4j jar in my classpath and also i have log.java.

    but i dont get any log file created on the specified path ?

    Please help.

  10. Hi Dave,
    U have all the configuration file in tomcat. How will u access the log object in servlet / jsp and Is there any configuration is required in Web.xml

    Karthik

  11. Hey Dave,

    This works perfectly. But, I’m curious to know how does log4j know about the log4j.properties file ..is it something that is already setup in Tomcat for log4j ?

    Thanks,
    Vamsi

  12. Hi guys,

    Log4j with tomcat 6 doesn’t seem to be working for me. I have a log4j.prop file placed in the classes dir and the log4j.jar and commons-logging.jar in both classpath and buildpath.

    When I try to run build from a cmd the log messages are getting displayed, but the same is not happening on the tomcat console.

    My prop file looks like this:
    ————————-
    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n
    log4j.rootLogger=warn, stdout
    ————————-

    Can anyone help me out on this??

    NOTE: I cannot see the anything on the tomcat console which states that the log4j jars are being loaded.

    Thanks,
    Param

  13. Hi,
    Can any one help me to solve the issue of having indivdual logs for each indivudual aar file running in axis2 (tomcat) environment.

    now log4j.xml is under axis2/classes and is overwriting all the services logs in the same file.

    this is the content in build.xml

    how to put all log4j.xml & lib inside aar. any idea is appreciated.

    each Service – each Log file log4j where to configure how to configure..
    Any links or suggestions are highly appreciated.

  14. hi,

    iam new writing code in web application i have one log4j.xml and log.dtd its fine but i have deployed it with web-inf/classes and using it i write the code looger object and log.error or log.info when it is project was developed it no message regrading to the log4j

    please me regarding to this matter in web applications

  15. I am trying to configure the log4j for our tomcat application. I have put the log4j.properties file in the tomcat/common/classes and the log4.jar is also available in the tomcat/common/lib. After restarting the server, i was able to see the file tomcat.log in the tomcat/logs but its empty.

    this is the code i ran:
    log4j.properties

    log4j.rootLogger=INFO, A1
    log4j.appender.A1=org.apache.log4j.FileAppender
    log4j.appender.A1=org.apache.log4j.RollingFileAppender
    log4j.appender.A1.File=${catalina.home}/logs/tomcat.log
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c – %m%n

    simple jsp test code:

    FYI, all this is running on a Unix box.

    Pl, help me out with this.

  16. One thing in my above code, the “R” at appender.R.layout and appender.R.ConversionPattern should be “A1”.

    Also, i this working somehow, now all the logs are going into tomcat.log.

    But i want to know if there is a way to log on webapp level.
    for example, i have a application called “APPS” under the tomcat/webapps.
    Is it possible to use log4j and create a log file for just the APPS application?

  17. Hey Dave,

    I have my log4j.properties(the same u have given here) in WEB-INF/classes and the needed jars in WEB-INF/lib , but still its not working in my web app…..i am using tomcat 5.5…can you please help me out ….thanks

  18. Hi

    I have a Axis2 web service that I have deployed in a Tomcat server. Inside the webservice I have the log4j.xml file (in the src folder), which has the FilePath=”./log/MyWebService.log” for log file to be created. Whenever the web service is invoked, I can see the log statements generated by log4j printed on catalina.out. But the MyWebService.log file is not being generated. Iam using DailyRollingFileAppender. Can anybody please help me out..Thanks in advance.

  19. Thanks Dave,
    Trolling the net for a solution, yours is the most useful info on the subject I found! I just copied your snippet and saved it as log4j.properties in the WEB-INF/classes folder and hey presto! It worked!
    If you have more than one war-file deployed in Tomcat’s webapps folder, the “trick” that worked for me is to give each war-file’s log file a different name, e.g.
    assuming you have two apps deployed, this is how you could name them (my system is a Windows XP):
    log4j.appender.R.File=C:/Program Files/apache-tomcat-6.0.24/logs/app1.log for the one app,
    and log4j.appender.R.File=C:/Program Files/apache-tomcat-6.0.24/logs/app2.log for the other.
    This way, each app writes to its own log file.
    Thanks again for your help! It was short and to the point.

    1. Hi Harry,
      can u pls elaborate on ur solution.By adding two lines
      ” log4j.appender.R.File=C:/Program Files/apache-tomcat-6.0.24/logs/app1.log for the one app,
      and log4j.appender.R.File=C:/Program Files/apache-tomcat-6.0.24/logs/app2.log for the other. ”
      Ho will log4j come to know which application log will go in app1.log and wich will go to app2.log, Can u please paste ur complete log4j.prop and tell in detail how to do this.

  20. Hi,
    I am using log4j logging mechanism in my application.
    But i m getting an error on tomcat console like : Unable to find log4j.xml…..

    In which directory i need to put log4j.xml ????

    Please reply me om my mailid: amol.chitte87@gmail.com

    i m struggling on this …………… 😦

  21. Hi

    I have a log4j.properties file for my web app and it gives me proper logging in Eclipse console and a log file as i have specified.But this works on my local tomcat.Now i deployed my application in an external tomcat server .But how do i configure the log4j to show me the logs on this server?

  22. Hi

    I have a log4j.properties file for my web app and it gives me proper logging in Eclipse console and a log file as i have specified.But this works on my local tomcat.Now i deployed my application in an external tomcat server .But how do i configure the log4j to show me the logs on this server?

  23. With this information, I was able to answer
    concerns out of my head. I have plenty of contacts who might just need this ideas.
    Is it fine if I share it with them?.

  24. A person necessarily assist to make severely articles I would state.
    That is the first time I frequented your web page and thus far?
    I amazed with the analysis you made to make this actual post extraordinary.
    Excellent process!

  25. Hello there! I could have sworn I’ve been to this site before but after browsing through some of the post I realized it’s
    new to me. Nonetheless, I’m definitely happy I found it
    and I’ll be book-marking and checking back frequently!

  26. Hi to every one, the contents present at this site are actually
    awesome for people experience, well, keep up the good
    work fellows.

  27. Hello, I desire to subscribe for this blog to get most up-to-date updates,
    so where can i do it please help.

  28. Hi there, yeah this piece of writing iѕ in fact fastidious аnd I ɦave learned lot оf things from it
    օn the topic οf blogging. thankѕ.

  29. Good answers in return of this issue with solid arguments and telling
    all regarding that.

  30. Hi! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m absolutely enjoying your blog and look forward to new
    updates.

  31. Just want to say your article is as amazing. The clearness on your post is simply cool and that i can assume you’re knowledgeable
    in this subject. Fine along with your permission allow me
    to snatch your feed to keep up to date with approaching post.
    Thanks one million and please carry on the gratifying work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s