Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure log4j using log4j.xml to append to different log files based on class name?

Tags:

log4j

I want to set up log4j so that all log meessages produced from classes under package com.foo.bar go to bar.log, and all the log meessages produced from classes under package com.bar.blatz go to blatz.log.

Questions

  • How do I do this using log4j.xml?
  • I know its possible using property files, but how do I do it using the XML configuration?
like image 689
benhsu Avatar asked May 19 '09 00:05

benhsu


People also ask

How do you set log file path in log4j properties dynamically?

System. setProperty("{my. log", "C:/logfile. log");

Where should log4j xml be placed?

Locate the log4j. xml file under the oarm/WEB-INF/classes/ directory. Update the log output path for each appender.


1 Answers

This is based on my answer to a similar question:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <!-- general application log -->
    <appender name="BarLogFile" class="org.apache.log4j.FileAppender">
        <param name="File" value="bar.log" />
        <param name="Threshold" value="INFO" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
        </layout>
    </appender> 

    <!-- additional fooSystem logging -->
    <appender name="BlatzLogFile" class="org.apache.log4j.FileAppender">
        <param name="File" value="blatz.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
        </layout>
    </appender>

    <logger name="com.foo.bar">
        <appender-ref ref="BarLogFile"/>
    </logger>

    <logger name="com.bar.blatz">
        <appender-ref ref="BlatzLogFile"/>
    </logger>

    <root>
        <level value="INFO"/>
        <!-- no appender, output will be swallowed (I think) -->
    </root>
</log4j:configuration>

If you add an appender-ref to the root element, it will also receive com.foo.bar etc messages. You can stop that by specifying 'additivity="false"' on the loggers.

like image 170
araqnid Avatar answered Oct 10 '22 16:10

araqnid