Log4j

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Log4j as PDF for free.

More details

  • Words: 872
  • Pages: 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: <priority value="INFO"/> <priority value="WARN"/>

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.

<priority value ="INFO"/>


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 X X.Y X.Y.Z

Proot Proot Px Px none Px 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, <priority value="WARN"/>

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 Name

Added Additivity Output Appenders Flag Targets not A1 applicable

root

A1

x

A-x1, A-x2 true

Comment The root logger is anonymous but can be accessed with the Logger.getRootLogger() method. There is no default appender attached to root.

A1, A-x1, Appenders of "x" and root. A-x2

x.y

none

true

x.y.z

A-xyz1

true

security

A-sec

false

security.access none

true

A1, A-x1, Appenders of "x" and root. A-x2 A1, A-x1, A-x2, A- Appenders in "x.y.z", "x" and root. xyz1 No appender accumulation since the A-sec additivity flag is set to false. Only appenders of "security" because the additivity flag in "security" is set to A-sec false.

An Example:

<priority value ="INFO"/>



<priority value="INFO"/> <priority value="INFO"/> <param name="File" value="legoBricks.log"/> <param name="Append" value="false"/> <param name="ConversionPattern" value="%d{ISO8601};hostname;application;%c;%-5p;%m%n"/>

According the above rule table for additivity, the example is the following:

Logger Name

Added Appenders

Additivit y Flag

Output Targets

Comment

The root logger is anonymous but can be accessed with the not ASYNC ASYNC root Logger.getRootLogger() applicable method. There is no default appender attached to root. No appender accumulation com.vodafone.germany LEGOBRICK LEGOBRICKS since the additivity flag is false 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 A-xyz1 true d2 , 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.

Related Documents

Log4j
June 2020 1
Log4j
May 2020 2
Log4j
November 2019 10
Log4j User Guide
November 2019 9
Log4j Tutorial En
December 2019 18
Log4j In 30 Minutes Or Less
November 2019 10