• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

Log4J question

Can you have more than one Appender in a config file. And do i set a specific appender to a specific class in a config file?

Thanks,
Mark.
0
CHARMS1
Asked:
CHARMS1
1 Solution
 
CEHJCommented:
Yes and yes. Here's a file with 3 appenders


# 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
 
girionisCommented:
You can even have a class to use two (or more) appenders.
0
 
aflat362Commented:
You can certainly have more than one appender.  I would recommend using the DomConfigurator to config log4j with an XML instead.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
      <appender name="cdms" class="org.apache.log4j.DailyRollingFileAppender">
            <param name="File" value="/shoapp/clms/logs/cdms.std.log"/>
            <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss.SSS} %-5p [%t]: %m%n"/>
        </layout>
      </appender>
      <appender name="cdms.xml" class="org.apache.log4j.DailyRollingFileAppender">
            <param name="File" value="/shoapp/clms/logs/cdms.log.xml"/>
            <layout class="org.apache.log4j.xml.XMLLayout"/>
      </appender>
      <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss.SSS} %-5p [%t]: %m%n"/>
            </layout>
      </appender>
      <logger name="com.cdms.logger">
            <level value="debug"/>
            <appender-ref ref="cdms"/>
            <appender-ref ref="cdms.xml"/>
      </logger>
      <root>
            <level value="info"/>
            <appender-ref ref="stdout"/>
      </root>
</log4j:configuration>

Java Code . . .

class level:

      private static Logger logger = null;

methods in class:

      public void createLogger() throws Exception {
##Put your path to the XML in here
            DOMConfigurator.configure("/shoapp/clms/cdmslog4j.xml");
            setLogger(Logger.getLogger("CmAdminTools.logger"));
            
      }

      /**
       * @param logger
       */
      public static void setLogger(Logger loggerObj) {
            logger = loggerObj;
      }


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
> Can you have more than one Appender in a config file. And do i set a specific appender to a specific class in a config file?

You can have as many appenders youi you want and they would be specified in your log4j.xml.
You should attach your appenders to loggers, not to a specific class, what you assign to what is up to you.
0
 
phuocnhCommented:
You can use how many appender you want. One appender is file appender ( write log to file), one appender is SMTP appender (send log to an email), one appender to console (output log to console),....
Even file appender you can have many file appender such as DailyRolingFileAppender, RollingFileAppender
Appender is associate with logger which have a name. If you want your class asscociate with that appender you just set the logger of that class have a logger with the name you set for appender.
Phuoc H.Nguyen
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now