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

Posted: August 31, 2006 in howto, Java, log4j, Technology, Tomcat

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.

About these ads
Comments
  1. Manoj says:

    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. zencin says:

    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. NIshikanta says:

    above configuration doesn’t working in sun soloris 5.9
    please guide

  4. Dave says:

    Please provide more information that will help others to help you.

  5. rebin says:

    please tell me how to configure log4j on two apps in one server

  6. Dave says:

    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.

  7. Ramesh says:

    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

  8. Dave says:

    Hi Ramesh,

    Your log4j.xml content wasn’t included. Did you see the information you normally see in the console in the lof4j log file?

  9. Laxmikanth says:

    How to configure 1 log4j.xml in axis 2 for different services

  10. Laxmikanth says:

    How do I configure Log4J to log separately for each web application

  11. Dave says:

    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.

  12. somnath says:

    hi

    thanks for u r answers

  13. Dave says:

    Hi somnath,

    You welcome.

  14. Kfir Ozer says:

    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!

  15. Dave says:

    Hi Kfir,

    Good to know this blog entry was useful to you and welcome.

  16. asaf says:

    I know that in JBoss you can place log4j.xml in the conf dir.
    From some reason, this does not work in tomcat.

  17. asaf says:

    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.

  18. Dave says:

    Hi asaf,

    Thanks for sharing your experience and tip.

    • surjan says:

      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

  19. neel says:

    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

  20. Dave says:

    Hi Neel,

    Does log4j.xml and log4j.dtd works for you? If so, just keep it. If not, you can always try what I wrote.

  21. Hitemeha2 says:

    Tthe article was really helpful for me to start using logger funcationality. … thanks :)

  22. Praveen says:

    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.

  23. Dave says:

    Hi Praveen,

    Can you take a look in Tomcat’s log file and see if there is anything related to log4j library being loaded or not?

  24. karthik says:

    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

  25. Vamsi says:

    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

  26. Parambir says:

    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

  27. Parambir says:

    If anyone has a suggestion please mail me: paramvir_virdi@rediffmail.com

    thanks

  28. Laxmikanth says:

    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.

  29. Arun says:

    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

  30. [...]  Specifying a hardcoded path on the log4j.properties file can do the trick (as suggested by dave), but that is not a very elegant solution, in my opinion.  I do not even know if my application [...]

  31. SS says:

    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.

  32. SS says:

    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?

  33. Varun says:

    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

  34. Mohith Sanjay says:

    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.

  35. ExNexu says:

    You have to make sure, that the user, who runs tomcat, has enough rights to write into the specified file…

  36. Harry says:

    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.

    • surjan says:

      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.

  37. Amol Chitte says:

    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 …………… :(

  38. nalaka Sri Lanka says:

    Thank your very much for posting this.

  39. Namrata says:

    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?

  40. Jual Jaket Kulit Murah says:

    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?

  41. 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?.

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 )

Connecting to %s