Question

I have a peculiar problem with Log4j. We are deploying on Weblogic 10 using log4j.xml. That works fine. We figured a way to run Tomcat 6 in such a way that we can develop on this platform and deploy on Weblogic 10.

I have figured out how to make log4j.properies work with Tomcat 6 with additional JArs that use the JULI logging mechanism in Tomcat 6. Unfortunately, my log4j.xml (that works with Weblogic) fails to work with Tomcat 6. So I am forced to use log4j.properties.

I have not been able to figure out how to indicate application class hierarchies in log4j.properties. So my question is how does on convert the following log4j.xml entries to log4j.properties

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="vccashib" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
    <param name="File" value="vccashib_10.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %t %-5p %c - %m%n"/>
    </layout>
  </appender>

  <logger name="org.hibernate">
    <level value="DEBUG"/>
    <appender-ref ref="vccashib" />
  </logger>

</log4j:configuration>

So let us say I was to write a logger that captures logs for classes in my com.abc.xyz hierarchy. How do I indicate that in log4j.properties? (I have taken care of a other values, such as type of appenders, DatePattern, ConversionPattern, FileName, layout etc.)

Please note, I have sent you the only problem-specific code.

Alternatively, if you know the answer on how to make log4j.xml that is part of my war archive with Tomcat 6 (JULI jars are in place and work well with log4j.properties), please let me know

Please help.

Suhaas

Was it helpful?

Solution

I'm not entirely clear on your question. I've deployed log4j on tomcat 6 with no problems (can you post the specific problems you're having with your xml config?). I'm not sure what you mean by "I have figured out how to make log4j.properies work with Tomcat 6 with additional jars that use the JULI logging mechanism in Tomcat 6").

  • is the config in the proper location?
  • are some of your jars being loaded from the global tomcat ($TOMCAT_HOME/lib) directory? If so the library might be looking for the config from the global directory, rather than from your webapp.

If you're just talking about how to manage separate class hierarchies in your log4j config, it's something like :

log4j.rootCategory=INFO,stdout

#e.g. not interested in most stripes or spring warnings
log4j.logger.net.sourceforge.stripes=ERROR
log4j.logger.org.springframework=ERROR

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d %-5p [%t] %c{1} %3x - 
%m%n

#This creates the separate log "Foo" at DEBUG level. Everythign within 
#the "com.foo" package will
# get logged here
log4j.category.com.foo=DEBUG,Foo
log4j.appender.Foo=org.apache.log4j.RollingFileAppender
log4j.appender.Foo.layout=org.apache.log4j.PatternLayout
log4j.appender.Foo.layout.ConversionPattern=%-d %-5p [%t] %c{1} %3x - %m%n
log4j.appender.DetailLogFile.File=foo.log
#this is the important line - if this isn't here the stuff in
#com.foo package will show up in both logs.
log4j.additivity.com.foo=false

OTHER TIPS

IT appears to me that there was no problem with creation of logs even with log4j.xml

I did use http://tomcat.apache.org/tomcat-6.0-doc/logging.html to create logging mechanism as suggested.

Interestingly the logs were being created in the bin directory of Tomcat 6 !!! It is something to do with the current working directory and in my case it seems to be the bin directory as per some Catalina setting

Few simple steps described below will help you to set up web application's log4j to be initialized:

1) copy log4j.xml to your (TOMCAT_HOME)/CATALINA_HOME/conf directory. The log4j.xml of my application looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
 <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
   <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%d{ABSOLUTE}
      %5p %c{1}:%L -Preetam %m%n"/>

        </layout>
     </appender>
       <root>
        <!--   <priority value="info"></priority> -->
               <priority value="debug"></priority>
          <appender-ref ref="stdout"/>
        </root>
</log4j:configuration>

2) stop the tomcat

tomcat6 stop /var/tmp/tomcat-20120622.log (use your application specific stop.sh script)

3) In (TOMCAT_HOME)/CATALINA_HOME/conf directory, you will have the tomcat6.conf file. Edit the file as described below. If there is no CATALINA_OPTS in the tomcat6.conf, create one.

# Settings for the CATALINA_OPTS to pick web application's log4j.xml from the specified location
CATALINA_OPTS="${CATALINA_OPTS}-Dlog4j.debug - Dlog4j.configuration=file:${CATALINA_HOME}/conf/log4j.xml"

4) Start the tomcat with your application specific startup.sh script.

5) Thats it, you are all done. Check the log to see if the tomcat is reading the log4j.xml specified by you in tomcat6.conf.

tail -f /var/tmp/tomcat-20120622.log

Jun 22, 2012 3:28:35 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jun 22, 2012 3:28:35 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jun 22, 2012 3:28:35 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 22, 2012 3:28:35 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/12  config=null
Jun 22, 2012 3:28:35 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 547 ms
log4j: Using URL [file:/opt/otccalypso/guiadmd01/tomcat6/conf/log4j.xml] for automatic    log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: Threshold ="null".
log4j: Level value for root is  [debug].
log4j: root level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE}        %5p %c{1}:%L -Preetam %m%n].
log4j: Adding appender named [stdout] to category [root].

We are using tomcat6 with log4j. We followed the following manual to install log4j in the tomcat - it is more complicated than on the old versions (4.1-5.5). I believe you should put your log4j.xml under the WEB-INF/classes directory in your war.

An additional approach is to use the spring framework for this - see Log4jConfigListener's API

According to http://boncey.org/2006_7_25_log4j_with_xml_configuration

  1. export CATALINA_OPTS="-Dlog4j.configuration=log4j.xml" (I suppose to force it to look for .xml rather than .properties)
  2. put log4j.xml file into WEB-INF/classes

I had a similar problem and this worked for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top