java-log4j-xml配置 category、logger、boot 标签使用
一、节点说明:
root:基础日志配置、包括使用的输出器、日志级别等;
logger:自定义输出配置;
category:自定义输出配置;
logger、category 用法一致,可以配置通过 additivity 属性标记是否集成 root 配置;
二、maven 依赖:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j 配置:demo/src/main/resources/core/log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- 控制台输出 -->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<!--最小级别-->
<param name="LevelMin" value="INFO"/>
</filter>
</appender>
<!-- 系统启动日志输出文件 -->
<appender name="fileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Encoding" value="UTF-8"/>
<param name="File" value="/logs/test_log4j_logs/system.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<!--最小级别-->
<param name="LevelMin" value="INFO"/>
</filter>
</appender>
<!-- 测试 category 配置节 -->
<appender name="testCategoryAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/test_log4j_logs/test_category_logs.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<!--最小级别-->
<param name="LevelMin" value="INFO" />
</filter>
</appender>
<!-- 测试 logger 配置节 -->
<appender name="testAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/test_log4j_logs/test_logger_logs.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<!--最小级别,小于 error 级别错误将不输出-->
<param name="LevelMin" value="ERROR" />
</filter>
</appender>
<!--自定义日志输出配置,additivity=是否继承 root 配置-->
<category name="testCategoryLogger" additivity="false">
<level value="INFO"></level>
<appender-ref ref="testCategoryAppender"></appender-ref>
<appender-ref ref="stdout"></appender-ref>
</category>
<!--自定义日志输出配置,additivity=是否继承 root 配置-->
<logger name="testLogger" additivity="false">
<!--级别-->
<level value="INFO"></level>
<!--输出器-->
<appender-ref ref="testAppender"></appender-ref>
<appender-ref ref="stdout"></appender-ref>
</logger>
<!--基础日志输出配置-->
<root>
<level value="INFO"/>
<!--输出器-->
<appender-ref ref="stdout"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
三、测试代码:demo/src/test/java/com/example/demo/TestLog4j.java
package com.example.demo;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.Log4jConfigurer;
import java.io.FileNotFoundException;
/**
* @author xiaojing.xie
* @create 2017-09-18
**/
public class TestLog4j {
@Before
public void before(){
try {
Log4jConfigurer.initLogging("classpath:core/log4j.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void test3(){
Logger log = Logger.getLogger("testLogger");
log.info("info");
log.debug("debug");
log.error("error");
log.warn("warn");
log.trace("trace");
}
@Test
public void test2(){
Logger log = Logger.getLogger("testCategoryLogger");
log.info("info");
log.debug("debug");
log.error("error");
log.warn("warn");
log.trace("trace");
}
@Test
public void test1(){
Logger log = Logger.getLogger("testAppender");
log.info("info");
log.debug("debug");
log.error("error");
log.warn("warn");
log.trace("trace");
}
}
四、测试说明:
主要观察 /logs/test_log4j_logs/ 路径下system.log、test_category_logs.log、test_logger_logs.log 三个文件的内容写入。