Using multiple appenders in log4J

I am using more than one log4j appender in the application that i am working on.

Is it possible to have a different logging level for each appender. ie: one appender picks up all logging and the other picks up only logging warnings and above.

Also is it possible to assign individual appenders to pick up logging from specific classes.

Thanks,
Mark
CHARMS1Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Yes (although it's the logger that has the level) and yes. Here's an example:


#
#       This program makes use of log4j, the documentation for which can be found at
#       http://logging.apache.org/log4j/docs/documentation.html
#       This is powerful and flexibe software and if necessary, can be rendered more
#       powerful still by overriding the properties in this file, either on the command
#       line or in a file in the user's home directory.
#


# Set root logger level to ERROR and its only appender to F (the file appender).

log4j.rootLogger=ERROR,F

# No debug for apache commons classes
log4j.logger.org.apache.commons.httpclient=ERROR,F

# No debugging for wire
log4j.logger.httpclient.wire=ERROR,WireAppender
# Don't log to F for wire - we want separate output
log4j.additivity.httpclient.wire=false


############## GAMEBOOKERS ######################
# Logger for main Gambookers app class - only log error and above to console
log4j.logger.httpclient.code=WARN


# Console is set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender


# Console uses PatternLayout.
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
# log4j.appender.Console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.Console.layout.ConversionPattern=%d{dd/MM/yy HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n




log4j.appender.F=org.apache.log4j.RollingFileAppender
log4j.appender.F.File=gamebookers.log
log4j.appender.F.maxFileSize=1048576
log4j.appender.F.layout=org.apache.log4j.PatternLayout
#log4j.appender.F.layout.ConversionPattern=%d{dd/MM/yy HH:mm:ss,SSS} %-5p %c %x - %m%n
log4j.appender.F.layout.ConversionPattern=%d{dd/MM/yy HH:mm:ss,SSS} %-5p %x - %m%n

# Minimal printing for 'wire' debugging (just the message + newline)
log4j.appender.WireAppender=org.apache.log4j.RollingFileAppender
log4j.appender.WireAppender.File=gamebookers-wire.log
log4j.appender.WireAppender.maxFileSize=1048576
log4j.appender.WireAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WireAppender.layout.ConversionPattern=%m%n

0
 
objectsCommented:
> Is it possible to have a different logging level for each appender. ie: one appender picks up all logging and the other picks up only logging warnings and above.

Yes, you would set the level in your logger, then attach that logger to you appender.

> Also is it possible to assign individual appenders to pick up logging from specific classes.

Yes define a logger for each class, and assign the required appender to that logger
0
 
Nguyen Huu PhuocSenior ManagerCommented:
Why do you reask the same question
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21865198.html
Here is the log4j.xml of JBOSS (J2EE Application Server), you can see that it has many appender:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!-- ===================================================================== -->
<!--                                                                       -->
<!--  Log4j Configuration                                                  -->
<!--                                                                       -->
<!-- ===================================================================== -->

<!-- $Id: log4j.xml,v 1.13.2.8 2003/09/23 14:16:27 slaboure Exp $ -->

<!--
   | For more configuration infromation and examples see the Jakarta Log4j
   | owebsite: http://jakarta.apache.org/log4j
 -->

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

   <!-- ================================= -->
   <!-- Preserve messages in a local file -->
   <!-- ================================= -->

   <!-- A time/date based rolling appender -->
   <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="File" value="${jboss.server.home.dir}/log/server.log"/>
      <param name="Append" value="false"/>

      <!-- Rollover at midnight each day -->
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>

      <!-- Rollover at the top of each hour
      <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
      -->

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>

         <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
         <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
          -->
      </layout>
   </appender>

   <!-- A size based file rolling appender
   <appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="File" value="${jboss.server.home.dir}/log/server.log"/>
     <param name="Append" value="false"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="1"/>

     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
     </layout>         
   </appender>
   -->

   <!-- ============================== -->
   <!-- Append messages to the console -->
   <!-- ============================== -->

   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="Target" value="System.out"/>
      <param name="Threshold" value="INFO"/>

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

   <appender name="JSR77" class="org.apache.log4j.FileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="Append" value="false"/>
      <param name="File" value="${jboss.server.home.dir}/log/jsr77.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

   <!-- ====================== -->
   <!-- More Appender examples -->
   <!-- ====================== -->

   <!-- Buffer events and log them asynchronously
   <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <appender-ref ref="FILE"/>
     <appender-ref ref="CONSOLE"/>
     <appender-ref ref="SMTP"/>
   </appender>
   -->

   <!-- EMail events to an administrator
   <appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="Threshold" value="ERROR"/>
     <param name="To" value="admin@myhost.domain.com"/>
     <param name="From" value="nobody@myhost.domain.com"/>
     <param name="Subject" value="JBoss Sever Errors"/>
     <param name="SMTPHost" value="localhost"/>
     <param name="BufferSize" value="10"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
     </layout>
   </appender>
   -->

   <!-- Syslog events
   <appender name="SYSLOG" class="org.apache.log4j.net.SyslogAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="Facility" value="LOCAL7"/>
     <param name="FacilityPrinting" value="true"/>
     <param name="SyslogHost" value="localhost"/>
   </appender>
   -->

   <!-- Log events to JMS (requires a topic to be created)
   <appender name="JMS" class="org.apache.log4j.net.JMSAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="Threshold" value="ERROR"/>
     <param name="TopicConnectionFactoryBindingName" value="java:/ConnectionFactory"/>
     <param name="TopicBindingName" value="topic/MyErrorsTopic"/>
   </appender>
   -->

   <!-- Log events through SNMP
   <appender name="TRAP_LOG" class="org.apache.log4j.ext.SNMPTrapAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="ImplementationClassName" value="org.apache.log4j.ext.JoeSNMPTrapSender"/>
     <param name="ManagementHost" value="127.0.0.1"/>
     <param name="ManagementHostTrapListenPort" value="162"/>
     <param name="EnterpriseOID" value="1.3.6.1.4.1.24.0"/>
     <param name="LocalIPAddress" value="127.0.0.1"/>
     <param name="LocalTrapSendPort" value="161"/>
     <param name="GenericTrapType" value="6"/>
     <param name="SpecificTrapType" value="12345678"/>
     <param name="CommunityString" value="public"/>
     <param name="ForwardStackTraceWithTrap" value="true"/>
     <param name="Threshold" value="DEBUG"/>
     <param name="ApplicationTrapOID" value="1.3.6.1.4.1.24.12.10.22.64"/>
     <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d,%p,[%t],[%c],%m%n"/>
     </layout>
   </appender>
   -->

   <!-- ================ -->
   <!-- Limit categories -->
   <!-- ================ -->

   <!-- Limit the org.apache.commons category to INFO as its DEBUG is verbose -->
   <category name="org.apache.commons">
      <priority value="INFO"/>
   </category>

   <!-- Limit JBoss categories to INFO -->
   <category name="org.jboss">
     <priority value="INFO"/>
   </category>

   <!-- Decrease the priority threshold for the org.jboss.varia category
   <category name="org.jboss.varia">
     <priority value="DEBUG"/>
   </category>
   -->

   <!-- Show the evolution of the DataSource pool in the logs [inUse/Available/Max]
   <category name="org.jboss.resource.connectionmanager.JBossManagedConnectionPool">
     <priority value="TRACE" class="org.jboss.logging.XLevel"/>
   </category>
   -->

   <!--
      | An example of enabling the custom TRACE level priority that is used
      | by the JBoss internals to diagnose low level details. This example
      | turns on TRACE level msgs for the org.jboss.ejb.plugins package and its
      | subpackages. This will produce A LOT of logging output.
   <category name="org.jboss.system">
     <priority value="TRACE" class="org.jboss.logging.XLevel"/>
   </category>
   <category name="org.jboss.ejb.plugins">
     <priority value="TRACE" class="org.jboss.logging.XLevel"/>
   </category>
   -->
 
   <!--
       | Logs these events to SNMP:
           - server starts/stops
           - cluster evolution (node death/startup)
           - When an EJB archive is deployed (and associated verified messages)
           - When an EAR archive is deployed
            
   <category name="org.jboss.system.server.Server">
     <priority value="INFO" />
     <appender-ref ref="TRAP_LOG"/>
   </category>
 
   <category name="org.jboss.ha.framework.interfaces.HAPartition.lifecycle">
     <priority value="INFO" />
     <appender-ref ref="TRAP_LOG"/>
   </category>

   <category name="org.jboss.deployment.MainDeployer">
     <priority value="ERROR" />
     <appender-ref ref="TRAP_LOG"/>
   </category>
   
   <category name="org.jboss.ejb.EJBDeployer">
     <priority value="INFO" />
     <appender-ref ref="TRAP_LOG"/>
   </category>
   
   <category name="org.jboss.deployment.EARDeployer">
     <priority value="INFO" />
     <appender-ref ref="TRAP_LOG"/>
   </category>
   
 
   -->


   <!-- ======================= -->
   <!-- Setup the Root category -->
   <!-- ======================= -->

   <root>
      <appender-ref ref="CONSOLE"/>
      <appender-ref ref="FILE"/>
   </root>

   <!-- Clustering logging -->
   <!-- Uncomment the following to redirect the org.jgroups and
      org.jboss.ha categories to a cluster.log file.

   <appender name="CLUSTER" class="org.jboss.logging.appender.RollingFileAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="File" value="${jboss.server.home.dir}/log/cluster.log"/>
     <param name="Append" value="false"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="1"/>

     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
     </layout>
   </appender>
   <category name="org.jgroups">
     <priority value="DEBUG" />
     <appender-ref ref="CLUSTER"/>
   </category>
   <category name="org.jboss.ha">
     <priority value="DEBUG" />
     <appender-ref ref="CLUSTER"/>
   </category>
   -->

</log4j:configuration>
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Giant2Commented:
Appenders are differenced by a name.
Use this to difference the things you want.

Bye, Giant.
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
That is the same as what I hasd already posted, can you explain why you accepted only that comment please?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.