Вы находитесь на странице: 1из 5

Log4j

Log4j is package-oriented, but it treats the package as „Category (before version1.2)“ or


as a “Logger”.
For example, the logger named "com.foo" is a parent of the logger named
"com.foo.Bar". Similarly, "java" is a parent of "java.util" and an ancestor of
"java.util.Vector".

In the configuration file, it looks like:


<category name=" com.foo">
<priority value="INFO"/>
</category>
<category name=" com.foo.Bar">
<priority value="WARN"/>
</category>

TRACE, DEBUG, INFO, WARN, ERROR and FATAL are so-called “LEVEL” in
Loggers. To ensure that all loggers can eventually inherit a level, the root logger always
has an assigned level.
<root>
<priority value ="INFO"/>
</root>

Logger Assigned Inherited


name level level
root Proot Proot
X none Proot
X.Y none Proot
X.Y.Z none Proot
Example 1

In example 1 above, only the root logger is assigned a level. This level value, Proot, is
inherited by the other loggers X, X.Y and X.Y.Z.

Logger Assigned Inherited


name level level
root Proot Proot
X Px Px
X.Y Pxy Pxy
X.Y.Z Pxyz Pxyz
Example 2

In example 2, all loggers have an assigned level value. There is no need for level
inheritance.

Logger Assigned Inherited


name level level
root Proot Proot
X Px Px
X.Y none Px
X.Y.Z Pxyz Pxyz
Example 3

In example 3, the loggers root, X and X.Y.Z are assigned the levels Proot, Px and Pxyz
respectively. The logger X.Y inherits its level value from its parent X.

Logger Assigned Inherited


name level level
root Proot Proot
X Px Px
X.Y none Px
X.Y.Z none Px

In example 4, the loggers root and X and are assigned the levels Proot and Px
respectively. The loggers X.Y and X.Y.Z inherit their level value from their nearest parent
X having an assigned level.

Attention: In terms of the configuration of the level (the priority property in the element
category) in log4j.xml, only counts the inheritance, rather than the rule declared below.

If a Logger is assigned a level in the configuration file (log4j.xml), for example,


<category name=" com.foo.Bar">
<priority value="WARN"/>
</category>
But in the coding, this Logger is requested with a level “INFO” (thisLogger.info(…)),
then this request cannot be fulfilled, because the level rule is:
DEBUG < INFO < WARN < ERROR < FATAL.
Only the requested level is >= the configured level, then this request to this logger can be
fulfilled.

Appender Additivity

The rules governing appender additivity are summarized below.

Logger Added Additivity Output


Comment
Name Appenders Flag Targets
The root logger is anonymous but can
not be accessed with the
root A1 A1
applicable Logger.getRootLogger() method. There
is no default appender attached to root.
A1, A-x1,
x A-x1, A-x2 true Appenders of "x" and root.
A-x2
A1, A-x1,
x.y none true Appenders of "x" and root.
A-x2
A1, A-x1,
x.y.z A-xyz1 true A-x2, A- Appenders in "x.y.z", "x" and root.
xyz1
No appender accumulation since the
security A-sec false A-sec
additivity flag is set to false.
Only appenders of "security" because
security.access none true A-sec the additivity flag in "security" is set to
false.

An Example:

<root>
<priority value ="INFO"/>
<appender-ref ref="ASYNC"/>
</root>

<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">


<appender-ref ref="STDOUT"/>
</appender>

<category name="com.vodafone.germany" additivity="false">


<priority value="INFO"/>
<appender-ref ref="LEGOBRICKS" />
</category>

<category name="com.vodafone.germany.d2" additivity="true">


<priority value="INFO"/>
<appender-ref ref="A-xyz1" />
</category>

<appender name="LEGOBRICKS"
class="org.apache.log4j.RollingFileAppender">
<param name="File" value="legoBricks.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{ISO8601};hostname;application;%c;%-5p;%m%n"/>
</layout>
</appender>
<appender name=" A-xyz1" …/>

According the above rule table for additivity, the example is the following:
Additivit
Logger Added Output
y Comment
Name Appenders Targets
Flag
The root logger is
anonymous but can be
accessed with the
not
root ASYNC ASYNC Logger.getRootLogger()
applicable
method. There is no
default appender attached
to root.
com.vodafone.germany LEGOBRICK
No appender accumulation
false LEGOBRICKS since the additivity flag is
S
set to false.
No ASYNC. If an ancestor
of logger C, say P, has the
additivity flag set to
false, then C's output will
be directed to all the
appenders in C and it's
ancestors upto and
com.vodafone.germany. LEGOBRICKS including P but not the
d2
A-xyz1 true , A-xyz1
appenders in any of the
ancestors of P.
C:=
com.vodafone.germany.
d2
P:=
com.vodafone.germany

Configuration
BasicConfigurator.configure(); // configuration xml or properties file
are not necessary, The invocation of the BasicConfigurator.configure()
method creates a rather simple log4j setup. This method is hardwired to
add to the root logger a ConsoleAppender. The output will be formatted
using a PatternLayout set to the pattern "%-4r [%t] %-5p %c %x - %m%n".

PropertyConfigurator.configure(args[0]); // parse a configuration file


(file name ends only with *.properties)at run-time and set up logging
accordingly.

DOMConfigurator.configure(args[0]); // parse a configuration file (file


name ends only with *.xml )at run-time and set up logging accordingly.

// use the loader helper from log4j


URL url = Loader.getResource("my.properties");
PropertyConfigurator.configure(url);

// use the same class loader as your class


URL url = LogClass.class.getResource("/my.properties");
PropertyConfigurator.configure(url);

// load custom XML configuration


URL url = Loader.getResource("my.xml");
DOMConfigurator.configure(url);

Default Initialization Procedure


Log4j sucht zuerst nach einer Datei log4j.xml und dann nach den
log4j.properties im Root-Verzeichnis des classes Verzeichnisses(= src
Verzeichnis vor dem Kompilieren).
Man kann die Konfiguration auch aus anderen Dateien laden.

http://logging.apache.org/log4j/1.2/manual.html

Summery:
1. looking for the log4j.configuration property in system
2. If 1 is not found, then looking for the log4j.xml or log4j.properties in the root
classes directory project/class.
3. If 2 not found, uses BasicConfigurator.configure, PropertyConfigurator.configure
or DOMConfigurator.configure.

Вам также может понравиться