Log4j

From Null-pointer

Jump to: navigation, search

Contents

Configuring Log4J

  • log4j.configuration=log4j.properties Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties.
  • log4j.rootCategory=priority [, appender]*

Set the default (root) logger priority.

  • log4j.logger.logger.name=priority Set the priority for the named logger and all loggers hierarchically lower than, or below, the named logger. logger.name corresponds to the parameter of LogFactory.getLog(logger.name), used to create the logger instance. Priorities are: DEBUG, INFO, WARN, ERROR, or FATAL.

Log4J understands hierarchical names, enabling control by package or high-level qualifiers: log4j.logger.org.apache.component=DEBUG will enable debug messages for all classes in both org.apache.component and org.apache.component.sub. Likewise, setting log4j.logger.org.apache.component=DEBUG will enable debug message for all 'component' classes, but not for other Apache projects.

  • log4j.appender.appender.Threshold=priority

Log4J appenders correspond to different output devices: console, files, sockets, and others. If appender's threshold is less than or equal to the message priority then the message is written by that appender. This allows different levels of detail to be appear at different log destinations. For example: one can capture DEBUG (and higher) level information in a logfile, while limiting console output to INFO (and higher). [1]

Properties file

log4j.rootLogger=TRACE,out
 
log4j.logger.org.apache.camel=TRACE
 
log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
 
 
log4j.appender.LOG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.layout.ConversionPattern=[%d{ISO8601}] [%p] [%t] %c{1} %x %m%n
log4j.appender.LOG.file=${jboss.server.log.dir}/cognos.log
log4j.appender.LOG.encoding=UTF-8

Xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
		<param name="Threshold" value="INFO" />
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%X{hostname} %d{yyyy-MM-dd  HH:mm:ss,SSS} %p %t %c - %m%n"/>
        </layout>
    </appender>
 
	<appender name="ACE" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${server.log.location}/ACE.log" />
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="10" />
		<param name="Threshold" value="INFO" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" 
			   value="%X{hostname} %d{yyyy-MM-dd  HH:mm:ss,SSS} %p %t %c - %m%n" />
		</layout>
	</appender>
 
    <logger name="bbc" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="ACE" />
    </logger>
 
    <root>
        <priority value="WARN" />
        <appender-ref ref="CONSOLE" />
    </root>
 
</log4j:configuration>

Levels

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

[2]

See also

Static logger

References

  1. (26 Nov 2007). "Commons logging" Retrieved 20 Jul 2011.
  2. http://logging.apache.org/log4j/1.2/manual.html Retrieved 7 Nov 2011.
Personal tools