• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Multiple File Appenders in log4j

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to log messages using log4j to multiple log files. I am using the following configuration below ...

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

<log4j:configuration debug="false">

<appender name="YOURFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="log/your.log"/>
<param name="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH'.log'"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} [%5p] [%c] - %m%n"/>
</layout>
</appender>

<appender name="MYFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="log/mine.log"/>
<param name="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} [%5p] [%c] - %m%n"/>
</layout>
</appender>

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} [%5p] [%c] - %m%n"/>
</layout>
</appender>

<category name="org.dmk">
<priority value="DEBUG"/>
<appender-ref ref="YOURFILE"/>
</category>

<category name="org.dmk">
<priority value="INFO"/>
<appender-ref ref="MYFILE"/>
</category>

<root>
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>

With this configuration and my sample program, I received the following output in the console:

10:44:19,834 [ INFO] [org.dmk.test.logging.sub1.Sub1] - Creating a Sub1...
10:44:19,835 [ INFO] [org.dmk.test.logging.sub2.Sub2] - Creating a Sub2 instance...
10:44:19,835 [DEBUG] [org.dmk.test.logging.sub1.Sub1] - sub1 doing DEBUG logging...
10:44:19,836 [ INFO] [org.dmk.test.logging.sub1.Sub1] - sub1 doing INFO logging...
10:44:19,836 [ WARN] [org.dmk.test.logging.sub1.Sub1] - sub1 doing WARN logging...
10:44:19,836 [ERROR] [org.dmk.test.logging.sub1.Sub1] - sub1 doing ERROR logging...
10:44:19,836 [FATAL] [org.dmk.test.logging.sub1.Sub1] - sub1 doing FATAL logging...
10:44:19,837 [DEBUG] [org.dmk.test.logging.sub2.Sub2] - sub2 doing DEBUG logging...
10:44:19,837 [ INFO] [org.dmk.test.logging.sub2.Sub2] - sub2 doing INFO logging...
10:44:19,838 [ WARN] [org.dmk.test.logging.sub2.Sub2] - sub2 doing WARN logging...
10:44:19,838 [ERROR] [org.dmk.test.logging.sub2.Sub2] - sub2 doing ERROR logging...
10:44:19,838 [FATAL] [org.dmk.test.logging.sub2.Sub2] - sub2 doing FATAL logging...

BUT, if I look at the log files generated, I get everything correctly in mine.log and NOTHING in your.log.

If I swap the order of the category tags in the XML file, I get everything correct in your.log and nothing in mine.log.

I am assuming that this means you can only have one appender assigned to a name (and the latter overrides this).

Is there a way that anyone knows of to be able to set different priorities for different appenders for the same name?

Thanks in advance,
David
 
This is my favorite tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic