Log4j = Log for Java.
author: Ceki Gülcü
license: Apache License V2.0
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、数据库等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
Log4j有7种不同的log级别,按照等级从低到高依次为:TRACE<DEBUG<INFO<WARN<ERROR<FATAL<OFF。如果配置为OFF级别,表示关闭log。
Log4j支持两种格式的配置文件:properties和xml。包含三个主要的组件:Logger、appender、Layout。 Example for log4j 1.2
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<!--an appender is an output destination, such as the console or a file; names of appenders are arbitrarily chosen-->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<!--loggers of category 'org.springframework' will only log messages of level "info" or higher; if you retrieve Loggers by using the class name (e.g. Logger.getLogger(AClass.class)) and if AClass is part of the org.springframework package, it will belong to this category-->
<logger name="org.springframework">
<level value="info"/>
</logger>
<!--everything of spring was set to "info" but for class PropertyEditorRegistrySupport we want "debug" logging-->
<logger name="org.springframework.beans.PropertyEditorRegistrySupport">
<level value="debug"/>
</logger>
<root>
<!--all log messages of level "debug" or higher will be logged, unless defined otherwise all log messages will be logged to the appender "stdout", unless defined otherwise-->
<level value="debug" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>