一、Log4j2 Maven配置
Log4j2的性能高于log4j与logback,在项目中建议使用Log4j2,其依赖如下(目前最新版本为2.6.2):
<dependency> <groupId>org.apache.logging.log4jgroupId> <artifactId>log4j-apiartifactId> <version>2.6.2version> dependency> <dependency> <groupId>org.apache.logging.log4jgroupId> <artifactId>log4j-coreartifactId> <version>2.6.2version> dependency>
二、log4j2.xml文件配置
log4j2.xml的配置文件放在classpath下就会被自动加载,其配置如下:
xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="600"> <Properties> <Property name="LOG_HOME">/Users/hanhan.zhang/logsProperty> Properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> Console> <RollingRandomAccessFile name="service_appender" immediateFlush="false" fileName="${LOG_HOME}/service.log" filePattern="${LOG_HOME}/service.log.%d{yyyy-MM-dd}.log.gz"> <PatternLayout> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M -- %msg%xEx%npattern> PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> Policies> RollingRandomAccessFile> <RollingRandomAccessFile name="service_debug_appender" immediateFlush="false" fileName="${LOG_HOME}/service.log" filePattern="${LOG_HOME}/service.log.%d{yyyy-MM-dd}.debug.gz"> <PatternLayout> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M -- %msg%xEx%npattern> PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> Policies> RollingRandomAccessFile> Appenders> <Loggers> <root level="debug"> <appender-ref ref="Console"/> root> <logger name="org.springframework.core" level="info"/> <logger name="org.springframework.beans" level="info"/> <logger name="org.springframework.context" level="info"/> <logger name="org.springframework.web" level="info"/> <logger name="org.jboss.netty" level="warn"/> <logger name="org.apache.http" level="warn"/> <logger name="service_log" level="info" includeLocation="true" additivity="true"> <appender-ref ref="service_appender"/> logger> <logger name="service_log" level="debug" includeLocation="true" additivity="false"> <appender-ref ref="service_debug_appender"/> logger> Loggers> Configuration>
在配置文件中:
1、根节点configuration下有两个子节点:appenders和loggers(可定义多个appender和logger了)
2、appenders:定义输出,如定义的Console,是将日志输出到控制台,并对输出设置属性(通过PatternLayout设置输出格式)
3、loggers:定义多个logger,且logger通过name进行区分,对不同的logger配置不同的输出可appender-ref引用配置的appender的name
4、logger name机制
logger name类似于java package一样,比如有个包:cn.lsw.base.log4j2,而且生成Logger对象的时候通常通过Hello.class.getName()匹配定义的logger name,这是因为Logger也有继承关系。比如给cn.lsw.base定义了一个logger,那么它也适用于cn.lsw.base.lgo4j2这个logger。名称的继承是通过点“.”分隔的。此外,在上面的loggers配置中有子节点不是logger而是root,而且这个root没有name属性。这个root相当于根节点,所有的logger都适用与这个logger,所以即使在很多类里面通过类名.class.getName()得到很多的logger,而且没有在配置文件的loggers下面做配置也都能够输出,因为都继承了root的配置
三、log4j2与slf4j集成
log4j与slf4j集成还需添加另外的两个Maven Jar包依赖,配置如下:
<dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>1.7.21version> dependency> <dependency> <groupId>org.apache.logging.log4jgroupId> <artifactId>log4j-apiartifactId> <version>2.6.2version> dependency> <dependency> <groupId>org.apache.logging.log4jgroupId> <artifactId>log4j-coreartifactId> <version>2.6.2version> dependency> <dependency> <groupId>org.apache.logging.log4jgroupId> <artifactId>log4j-slf4j-implartifactId> <version>2.6.2version> dependency>
获取Logger对象方式:
// log4j2方式 private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(Logstrap.class); // slf4j方式 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(LoggerFactory.class);