Log4j 2 包含了基于LMAX 分离库的下一代的异步日志系统,在多线程环境下,异步日志系统比 Log4j 1.x 和Logback 提高了10倍性能提升(吞吐量和延迟率 )。原文如下:
Log4j 2 contains next-generation Asynchronous Loggers based on the LMAX Disruptor library. In multi-threaded scenarios Asynchronous Loggers have 10 times higher throughput and orders of magnitude lower latency than Log4j 1.x and Logback.
Log4j2目前已经出到了beta8版本,估计beta9在最近一周将放出,试用了下,感觉还不错,做如下总结!
我是从logback迁移到log4j2,
删除掉原有的包 log4j-over-slf4j-1.6.4.jar logback-classic-1.0.7.jar logback-core-1.0.7.jar slf4j-api-1.6.4.jar
增添以下的包 log4j-over-slf4j-1.7.5.jar log4j-api-2.0-beta8.jar log4j-core-2.0-beta8.jar log4j-slf4j-impl-2.0-beta8.jar
由于作者推荐,为了性能考虑,尽量不要使用slf4j,但我依赖的第三方框架比如spring有对slf4j使用,所以对JAR包做了以上的取舍,所以原有代码中要做如下的改动
改动前:import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(OOXX.class);
改动后:iimport org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private static final Logger logger = LogManager.getLogger(OOXX.class);
配置文件log4j2.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- status=debug 可以查看log4j的装配过程 -->
<configuration status="off">
<properties>
<property name="LOG_HOME">/log/fish</property>
<!-- 日志备份目录 -->
<property name="BACKUP_HOME">{LOG_HOME}/backup</property>
<property name="STAT_NAME">stat</property>
<property name="SERVER_NAME">global</property>
</properties>
<appenders>
<!-- 定义控制台输出 -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
</Console>
<!-- 程序员调试日志 -->
<FastRollingFile name="DevLog" fileName="${LOG_HOME}/${SERVER_NAME}"
filePattern="${LOG_HOME}/${SERVER_NAME}.%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</FastRollingFile>
<!-- 游戏产品数据分析日志 -->
<FastRollingFile name="ProductLog"
fileName="${LOG_HOME}/${SERVER_NAME}_${STAT_NAME}"
filePattern="${LOG_HOME}/${SERVER_NAME}_${STAT_NAME}.%d{yyyy-MM-dd-HH}.log">
<PatternLayout
pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</FastRollingFile>
</appenders>
<loggers>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core" level="info">
</logger>
<logger name="org.springframework.beans" level="info">
</logger>
<logger name="org.springframework.context" level="info">
</logger>
<logger name="org.springframework.web" level="info">
</logger>
<logger name="org.jboss.netty" level="warn">
</logger>
<logger name="org.apache.http" level="warn">
</logger>
<!-- Game Stat logger -->
<logger name="com.u9.global.service.log" level="info"
additivity="false">
<appender-ref ref="ProductLog" />
</logger>
<!-- Root Logger -->
<root level="DEBUG">
<appender-ref ref="DevLog" />
<appender-ref ref="Console" />
</root>
</loggers>
</configuration>
存在问题:在Eclipse下一切运行正常,如果把应用打包成jar包发布后,cmd命令行模式下,即使Console开着的情况下,也没法输出,文件输出也没有任何日志。