logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gregor Schneider" <rc4...@googlemail.com>
Subject Getting nuts with log4j and Tomcat - real world example :(
Date Fri, 08 Jun 2007 22:00:38 GMT
Hi guys,

the following scenario:

I have configured Apache Tomcat logging with log4j. I've created 5
different appenders (each one for DEBUG, TRACE, INFO, ERROR and FATAL)
and assigned them to the root-logger
$catalina_home/common/classes/log4j.xml expecting the output of the
Tomcat basic classes going in there.

Now we have amongs others 2 servlets, some of the using the same
component which uses it's own logger - let's call it "CompassLogger".

Servlet 1 let's call it "IndexDOM", Servlet 2 let's call it "OccupationDOM".

For each Servlet I've created their very own appenders (again 5 for
DEBUG, TRACE, INFO, ERROR and FATAL) and their own logger, where I
linked to the appenders created above.

Example:

  <logger name="OccupationLogger" additivity="false">
    <appender-ref ref="OccupationDOMDailyFileAppenderInfo" />
    <appender-ref ref="OccupationDOMDailyFileAppenderWarning" />
    <appender-ref ref="OccupationDOMDailyFileAppenderError" />
    <appender-ref ref="OccupationDOMDailyFileAppenderFatal" />
  </logger>

No I also have to create the logger "CompassLogger" since the
component I'm using is instantiating this logger ("CompassLogger") in
both my servlets.

So I specified in each log4j.xml the appender for CompassLogger and
then linked within my CompassLogger to the appenders (did that for
IndexDOM and OccupationDOM).

Example:

  <logger name="CompassLogger" additivity="false">
    <level value="info" />
    <appender-ref ref="CompassDailyFileAppenderInfo" />
    <appender-ref ref="CompassDailyFileAppenderWarning" />
    <appender-ref ref="CompassDailyFileAppenderError" />
    <appender-ref ref="CompassDailyFileAppenderFatal" />
  </logger>

My idea was, that every servlet is writing it's own DEBUG, TRACE,
INFO, ERROR and FATAL-files, and the conponent that makes use of the
CompassLogger writes to it's own log-file.

Unfortunaetely, that doesn't work.

IndexDOM writes it's own log-files as expected, however, OccupationDOM
always writes into the log-file that I've specified for Tomcat output.

I've got no more Idea how I should get this to work and I'm about to
create just one f*ck*ng BIG logfile, and kiss goodbye.

- Yes, I've asked in Tomcat maling-list, no answer by now.
- Yes, I've read the docs, maybe didn't udnerstand them properly
- Yes, I even browsed the *complete* log4j-manual available for money,
didn't find what I'm looking for

Attached I have my log4x.xml-files. I know it's a lot of code, and I
apologize for that, however, maybe it helps to understand the idea
behind it all.

Also, if you guys have some other suggestions how I can achieve, that
each servlet write it's own log-files, although accessing the same
component (jar-file) which calls "CompassLogger" (I do not have access
to the sources of that component).

If this post is too confusing, please let me know, I will then try to
put things in a more clearer way.

OK, this is the log4j.xml from $catalina_home/common/classes:

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

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

  <!-- Tomcat Daily Rolling File Appender TRACE, DEBUG, INFO -->
  <appender name="TomcatDailyFileAppenderInfo"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
        <param name="file" value="${catalina.home}/logs/tomcat.log.INFO" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMax" value="INFO"/>
                <param name="LevelMin" value="TRACE"/>
                <param name="AcceptOnMatch" value="true"/>
        </filter>
  </appender>

  <!-- Tomcat Daily Rolling File Appender WARNING -->
  <appender name="TomcatDailyFileAppenderWarning"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
        <param name="file" value="${catalina.home}/logs/tomcat.log.WARN" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMax" value="WARNING"/>
                <param name="LevelMin" value="WARNING"/>
                <param name="AcceptOnMatch" value="true"/>
        </filter>
  </appender>

  <!-- Tomcat Daily Rolling File Appender ERROR -->
  <appender name="TomcatDailyFileAppenderError"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
        <param name="file" value="${catalina.home}/logs/tomcat.log.ERROR" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMax" value="ERROR"/>
                <param name="LevelMin" value="ERROR"/>
                <param name="AcceptOnMatch" value="true"/>
        </filter>
  </appender>

  <!-- Tomcat Daily Rolling File Appender FATAL -->
  <appender name="TomcatDailyFileAppenderFatal"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
        <param name="file" value="${catalina.home}/logs/tomcat.log.FATAL" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMax" value="FATAL"/>
                <param name="LevelMin" value="FATAL"/>
                <param name="AcceptOnMatch" value="true"/>
        </filter>
  </appender>

  <root>
    <level value="info" />
    <appender-ref ref="TomcatDailyFileAppenderInfo" />
    <appender-ref ref="TomcatDailyFileAppenderWarning" />
    <appender-ref ref="TomcatDailyFileAppenderError" />
    <appender-ref ref="TomcatDailyFileAppenderFatal" />
  </root>

</log4j:configuration>

This is the configuration from Servlet IndexDOM:

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 <!-- Daily Rolling File Appender TRACE, DEBUG, INFO -->
  <appender name="IndexDOMDailyFileAppenderInfo"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/IndexDOM.log.INFO" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="INFO"/>
        <param name="LevelMin" value="TRACE"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- Daily Rolling File Appender WARNING -->
  <appender name="IndexDOMDailyFileAppenderWarning"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/IndexDOM.log.WARN" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="WARNING"/>
        <param name="LevelMin" value="WARNING"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- Daily Rolling File Appender ERROR -->
  <appender name="IndexDOMDailyFileAppenderError"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/IndexDOM.log.ERROR" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="ERROR"/>
        <param name="LevelMin" value="ERROR"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- Daily Rolling File Appender FATAL -->
  <appender name="IndexDOMDailyFileAppenderFatal"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/IndexDOM.log.FATAL" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="FATAL"/>
        <param name="LevelMin" value="FATAL"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender TRACE, DEBUG, INFO -->
  <appender name="CompassDailyFileAppenderInfo"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.INFO" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="INFO"/>
        <param name="LevelMin" value="INFO"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender WARNING -->
  <appender name="CompassDailyFileAppenderWarning"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.WARN" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="WARNING"/>
        <param name="LevelMin" value="WARNING"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender ERROR -->
  <appender name="CompassDailyFileAppenderError"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.ERROR" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="ERROR"/>
        <param name="LevelMin" value="ERROR"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender FATAL -->
  <appender name="CompassDailyFileAppenderFatal"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.FATAL" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="FATAL"/>
        <param name="LevelMin" value="FATAL"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <logger name="IndexLogger" additivity="false">
    <appender-ref ref="IndexDOMDailyFileAppenderInfo" />
    <appender-ref ref="IndexDOMDailyFileAppenderWarning" />
    <appender-ref ref="IndexDOMDailyFileAppenderError" />
    <appender-ref ref="IndexDOMDailyFileAppenderFatal" />
  </logger>

  <logger name="CompassLogger" additivity="false">
    <level value="info" />
    <appender-ref ref="CompassDailyFileAppenderInfo" />
    <appender-ref ref="CompassDailyFileAppenderWarning" />
    <appender-ref ref="CompassDailyFileAppenderError" />
    <appender-ref ref="CompassDailyFileAppenderFatal" />
  </logger>
</log4j:configuration>

And finally, this is the configuration from the Servlet OccupationDOM:

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 <!-- Daily Rolling File Appender TRACE, DEBUG, INFO -->
  <appender name="OccupationDOMDailyFileAppenderInfo"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/OccupationDOM.log.INFO" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="INFO"/>
        <param name="LevelMin" value="TRACE"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- Daily Rolling File Appender WARNING -->
  <appender name="OccupationDOMDailyFileAppenderWarning"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/OccupationDOM.log.WARN" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="WARNING"/>
        <param name="LevelMin" value="WARNING"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- Daily Rolling File Appender ERROR -->
  <appender name="OccupationDOMDailyFileAppenderError"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/OccupationDOM.log.ERROR" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="ERROR"/>
        <param name="LevelMin" value="ERROR"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- Daily Rolling File Appender FATAL -->
  <appender name="OccupationDOMDailyFileAppenderFatal"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/OccupationDOM.log.FATAL" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="FATAL"/>
        <param name="LevelMin" value="FATAL"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender TRACE, DEBUG, INFO -->
  <appender name="CompassDailyFileAppenderInfo"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.INFO" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="INFO"/>
        <param name="LevelMin" value="INFO"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender WARNING -->
  <appender name="CompassDailyFileAppenderWarning"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.WARN" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="WARNING"/>
        <param name="LevelMin" value="WARNING"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender ERROR -->
  <appender name="CompassDailyFileAppenderError"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.ERROR" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="ERROR"/>
        <param name="LevelMin" value="ERROR"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <!-- COMPASS Daily Rolling File Appender FATAL -->
  <appender name="CompassDailyFileAppenderFatal"
class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="Append" value="true" />
    <param name="file" value="${catalina.home}/logs/compass.log.FATAL" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601}\t%-5p\t%X{session}\t%X{file}\t[%t]:\t%m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMax" value="FATAL"/>
        <param name="LevelMin" value="FATAL"/>
        <param name="AcceptOnMatch" value="true"/>
    </filter>
  </appender>

  <logger name="OccupationLogger" additivity="false">
    <appender-ref ref="OccupationDOMDailyFileAppenderInfo" />
    <appender-ref ref="OccupationDOMDailyFileAppenderWarning" />
    <appender-ref ref="OccupationDOMDailyFileAppenderError" />
    <appender-ref ref="OccupationDOMDailyFileAppenderFatal" />
  </logger>

  <logger name="CompassLogger" additivity="false">
    <level value="info" />
    <appender-ref ref="CompassDailyFileAppenderInfo" />
    <appender-ref ref="CompassDailyFileAppenderWarning" />
    <appender-ref ref="CompassDailyFileAppenderError" />
    <appender-ref ref="CompassDailyFileAppenderFatal" />
  </logger>
/log4j:configuration>

Any suggestions will result in an instant improvement of ypu personal carma...

Gregor
-- 
what's puzzlin' you, is the nature of my game
gpgp-fp: 79A84FA526807026795E4209D3B3FE028B3170B2
gpgp-key available @ http://pgpkeys.pca.dfn.de:11371

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message