log4j升级为log4j2(不需要改动代码)

http://blog.csdn.net/zouxucong/article/details/56013053

公司的项目决定升级log4j,因为log4j2有一个自动删除日志的功能,这样可以减轻运维的一些工作,而且在多线程环境下,log4j2的异步日志系统比log4j和logback提高了十倍的性能(吞吐量和延迟率),官方原文如下:

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. 

因为是升级,原项目用的是log4j或者self4j打印日志,获取logger的方式不同

比如log4j:

import org.apache.log4j.Logger;

private static final Logger LOGGER = Logger.getLogger(xxxx.class);

比如self4j:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private static final Logger logger = LoggerFactory.getLogger(xxxx.class);

而如果用log4j2的话,API将有所不同:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

private static final Logger logger = LogManager.getLogger(xxxx.class);

如果在升级的过程中,我们要去改代码的话,那将是一项很大的繁琐工程,log4j2架构已经考虑到这一点,给我们提供了转换的包,我们只需要导入几个转换包就可以使用log4j2,而且还不需要修改代码。

官方原文:

Perhaps the simplest way to convert to using Log4j 2 is to replace the log4j 1.x jar file with Log4j 2's log4j-1.2-api.jar. However, to use this successfully applications must meet the following requirements:

  1. They must not access methods and classes internal to the Log4j 1.x implementation such as Appenders, LoggerRepository or Category's callAppendersmethod.
  2. They must not programmatically configure Log4j.
  3. They must not configure by calling the classes DOMConfigurator or PropertyConfigurator
第一步:我这里是maven项目,就以maven项目为例子,进行讲解了

删掉pom.xml文件中关于log4j的依赖,添加新依赖

[html] view plain copy
  1.  <dependency>  
  2.     <groupId>org.apache.logging.log4j</groupId>  
  3.     <artifactId>log4j-api</artifactId>  
  4.     <version>2.7</version>  
  5.     <!-- <scope>provided</scope> -->  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>org.apache.logging.log4j</groupId>  
  9.     <artifactId>log4j-1.2-api</artifactId>  
  10.     <version>2.7</version>  
  11. </dependency>  
  12. <dependency>  
  13.     <groupId>org.apache.logging.log4j</groupId>  
  14.     <artifactId>log4j-core</artifactId>  
  15.     <version>2.7</version>  
  16.     <!-- <scope>provided</scope> -->  
  17. </dependency>  
  18. <dependency>  
  19.     <groupId>org.apache.logging.log4j</groupId>  
  20.     <artifactId>log4j-web</artifactId>  
  21.     <version>2.7</version>  
  22.     <scope>runtime</scope>  
  23. </dependency>  
log4j-1.2-api为log4j和log4j2的连接包。


第二步:删掉以前的log4j.properties,改为log4j2.xml,这里起名字的时候,不要写错了

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <configuration status="info">  
  3.     <Properties>  
  4.         <Property name="LOG_HOME">${sys:catalina.base}/var/logs</Property>  
  5.         <Property name="APP_LOG_HOME">${LOG_HOME}/uas</Property>  
  6.     </Properties>  
  7.   
  8.     <Appenders>  
  9.         <Console name="Console" target="SYSTEM_OUT">  
  10.             <!-- 控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->  
  11.             <!-- <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"   
  12.                 /> -->  
  13.             <!-- 输出日志的格式 -->  
  14.             <PatternLayout  
  15.                 pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />  
  16.         </Console>  
  17.         <RollingRandomAccessFile name="uas_appender"  
  18.             immediateFlush="true" fileName="${APP_LOG_HOME}/uas.log"  
  19.             filePattern="${APP_LOG_HOME}/uas.log.%d{yyyy-MM-dd}.gz">  
  20.             <!-- %d{yyyy-MM-dd HH:mm:ss, SSS} : 日志生产时间 %p : 日志输出格式 %c : logger的名称   
  21.                 %m : 日志内容,即 logger.info("message") %n : 换行符 %C : Java类名 %L : 日志输出所在行数 %M   
  22.                 : 日志输出所在方法名 hostName : 本地机器名 hostAddress : 本地ip地址 -->  
  23.             <PatternLayout>  
  24.                 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M --  
  25.                     %msg%xEx%n</pattern>  
  26.             </PatternLayout>  
  27.             <Policies>  
  28.                 <TimeBasedTriggeringPolicy interval="1"  
  29.                     modulate="true" />  
  30.             </Policies>  
  31.             <!-- 最多备份30天以内的日志,此处为策略限制,Delete中可以按自己需要用正则表达式编写 -->  
  32.             <!-- DefaultRolloverStrategy字段中加入max=“30”经测试是配合SizeBasedTriggeringPolicy限制%i的存在数量,并没有发现是网上流传的是最多保存多少个文件的限制,也或许是我写的有问题 -->  
  33.             <DefaultRolloverStrategy>  
  34.                 <Delete basePath="${APP_LOG_HOME}" maxDepth="1">  
  35.                     <IfFileName glob="uas.log.*.gz" />  
  36.                     <IfLastModified age="30d" />  
  37.                 </Delete>  
  38.             </DefaultRolloverStrategy>  
  39.         </RollingRandomAccessFile>  
  40.     </Appenders>  
  41.   
  42.     <Loggers>  
  43.         <logger name="org.spring" level="error" additivity="false">  
  44.             <AppenderRef ref="Console" />  
  45.             <AppenderRef ref="uas_appender" />  
  46.         </logger>  
  47.         <logger name="org.springframework" level="error" additivity="false">  
  48.             <AppenderRef ref="Console" />  
  49.             <AppenderRef ref="uas_appender" />  
  50.         </logger>  
  51.         <logger name="org.apache.zookeeper" level="error" additivity="false">  
  52.             <AppenderRef ref="Console" />  
  53.             <AppenderRef ref="uas_appender" />  
  54.         </logger>  
  55.         <logger name="com.alibaba.dubbo" level="error" additivity="false">  
  56.             <AppenderRef ref="Console" />  
  57.             <AppenderRef ref="uas_appender" />  
  58.         </logger>  
  59.         <logger name="xx.xx.xx" level="info" additivity="false">  
  60.             <AppenderRef ref="Console" />  
  61.             <appender-ref ref="uas_appender" />  
  62.         </logger>  
  63.         <root level="info">  
  64.             <appender-ref ref="Console" />  
  65.             <appender-ref ref="uas_appender" />  
  66.         </root>  
  67.     </Loggers>  
  68.   
  69. </configuration>  
这里需要注意的有以下几点:

1.log4j2.*版本中获取home路径是${sys:catalina.base}
2.要在<root>标签中添加<appender-ref ref="Console"/>,<appender-ref ref="log"/>才能在控制台、和文件中打印日志


第三步:在web.xml中配置

注释掉以前关于加载log4j的配置,重新配置,这里我的log4j2.xml放在classpath/conf下

[html] view plain copy
  1. <context-param>  
  2.         <param-name>log4jConfiguration</param-name>  
  3.         <param-value>classpath:conf/log4j2.xml</param-value>  
  4.     </context-param>  
配置放在web.xml的<display-name/>下方


如此log4j升级log4j2升级成功。


扩展:如果原项目中用的是self4j打印日志,那么导包的时候需要导入:

[html] view plain copy
  1. <dependency>  
  2.     <groupId>org.slf4j</groupId>  
  3.     <artifactId>slf4j-api</artifactId>  
  4.     <version>${slf4j.version}</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>org.slf4j</groupId>  
  8.     <artifactId>slf4j-log4j12</artifactId>  
  9.     <version>${slf4j.version}</version>  
  10. </dependency>  
也是不需要修改代码,关于self4j继承log4j2的详细解决方案,可参考:http://blog.csdn.net/zouxucong/article/details/56005725
展开阅读全文

没有更多推荐了,返回首页