转载请注明出处:http://zl378837964.iteye.com/blog/2359382
现在最新的Log4j 2 是V2.8版本,使用2.x版本需要引入jar如下:
org.apache.logging.log4j log4j-core 2.5 org.apache.logging.log4j log4j-api 2.5
下面两个分别是兼容V1.x 和 实现异步化需要的jar:
(Disruptor它是开源的并发框架,能够在无锁的情况下实现网络的Queue并发操作。)
(注意:require disruptor-3.3.3.jar or higher) org.apache.logging.log4j log4j-1.2-api 2.5 com.lmax disruptor 3.3.4
V2.x版本的配置方式,除了编码式的写入,还有XML, JSON, YAML 和 Properties(注意这个是V2.4之后重新支持的,配置方式类似XML和 JSON不同于以往V1.x版本) 配置文件 on the classpath 即可实现,还有定义变量,过滤设置,定时删除日志,支持JDK8的Lambda等新特性;还能兼容 Log4j 1.2, SLF4J, Commons Logging 和 java.util.logging (JUL) APIs等各个日志工具;也可以定时重载配置文件,只需简单添加如下:
... 单位是s,最小间隔5s
... 共有8个级别,按照从低到高为:All < Trace < Debug < Info < Warn < Error < Fatal < OFF
若想使用YAML格式,需要引入格式化工具如下:
com.fasterxml.jackson.dataformat jackson-dataformat-yaml 2.8.5
若想使用JSON格式,需要引入格式化工具如下:
com.fasterxml.jackson.core jackson-core 2.8.5 com.fasterxml.jackson.core jackson-databind 2.8.5 com.fasterxml.jackson.core jackson-annotations 2.8.5
### Log4j2最突出即支持高效低延迟的异步化写日志 ###
Asynchronous Loggers是一个新增特性在Log4j 2 ,可以实现完全异步也可以和同步混合使用,还可以只异步化Appender,以提升系统性能,官方数据显示混合没有完全异步化效果好(下图)。
1、完全异步化XML配置+参数如下:
%d %p %c{1.} [%t] %m %ex%n
注意啦,注意啦!配置参数!
A.想要loggers完全异步化一定不要忘记设置启动参数: -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
B.或者,建立log4j2.component.properties文件放classpath下面,log4j2会在启动时自动加载,然后日志输出就变成异步输出。
文件内容:Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
2、Mixed同步和异步,由于使用了disruptor,标签化实现不再需要设置启动参数(关于Log4jContextSelector);
混合使用的xml如下:
%d %p %class{1.} [%t] %location %m %ex%n
eg.一个完整项目XML配置实例如下:
/Users/chocolate/logs /Users/chocolate/logs/baseCollection.log /Users/chocolate/logs/baseCollectionError.log
注意点:
有时候会出现同一日志文件中log4j日志打印重复,这是因为如下配置中 Console 被设置成logger和root的appender了,
此时只需要设置additivity="false"即可解决。(the appender associated with logger com.foo.Bar is first used, which writes the first instance to the Console. Next, the parent of com.foo.Bar, which in this case is the root logger, is referenced. The event is then passed to its appender, which is also writes to the Console, resulting in the second instance.)
Log4jV1.x升级V2.x需注意,原项目用的是log4j或者self4j,获取logger方式不同:
Log4j实例:
import org.apache.log4j.Logger;
private static final Logger LOGGER = Logger.getLogger(X.class);
Slf4j实例:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(X.class);
Log4j2的使用:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private static final Logger logger = LogManager.getLogger(X.class);
Log4j V2.x基于上面不同提供了转换包,只需要导入几个转换包就可以透明的使用Log4j2配置,从而避免大工程中的代码修改,耶耶耶!
但是,官方出来几个限制条件:
- They must not access methods and classes internal to the Log4j 1.x implementation such as Appenders, LoggerRepository or Category's callAppenders method.
- They must not programmatically configure Log4j.
- They must not configure by calling the classes DOMConfigurator or PropertyConfigurator.
log4j输出缓存日志以8K为单位,磁盘的一个block为8K,这样可以减少碎片;也就是说假设你设置缓存为20K,log4j在16K(8K*2)的时候就会输出),而不是20K。
附录A:
Appenders 官方文档:http://logging.apache.org/log4j/2.x/manual/appenders.html(23种应有尽有)
Async Low-Latency Loggers 官方文档:http://logging.apache.org/log4j/2.x/manual/async.html
log4j 2 官方配置:http://logging.apache.org/log4j/2.x/manual/configuration.html
附录B:
Properties configuration files support the advertiser, monitorInterval, name, packages, shutdownHook, shutdownTimeout, status, verbose, and dest attrbutes. See Configuration Syntax for the definitions of these attributes.(Props方式在V2.x不通用于各个版本,不建议使用)
status = error dest = err name = PropertiesConfig property.filename = target/rolling/rollingtest.log filter.threshold.type = ThresholdFilter filter.threshold.level = debug appender.console.type = Console appender.console.name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.pattern = %m%n appender.rolling.type = RollingFile appender.rolling.name = RollingFile appender.rolling.fileName = ${filename} appender.rolling.filePattern = target/rolling2/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz appender.rolling.layout.type = PatternLayout appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n appender.rolling.policies.type = Policies appender.rolling.policies.time.type = TimeBasedTriggeringPolicy appender.rolling.policies.time.interval = 2 appender.rolling.policies.time.modulate = true appender.rolling.policies.size.type = SizeBasedTriggeringPolicy appender.rolling.policies.size.size=100MB appender.rolling.strategy.type = DefaultRolloverStrategy appender.rolling.strategy.max = 5 appender.list.type = List appender.list.name = List appender.list.filter.threshold.type = ThresholdFilter appender.list.filter.threshold.level = error logger.rolling.name = com.example.my.app logger.rolling.level = debug logger.rolling.additivity = false logger.rolling.appenderRef.rolling.ref = RollingFile rootLogger.level = info rootLogger.appenderRef.stdout.ref = STDOUT