升级log4j2.x异步日志

摘要

Log4j 2 包含了基于LMAX 分离库的下一代的异步日志系统,在多线程环境下,异步日志系统比 Log4j 1.x 和Logback提高了10倍性能提升(吞吐量和延迟率 )


正文

升级jar包

1.升级log4j2.x的jar包

log4j-api-2.5.jar
log4j-core-2.5.jar

2.加入log4j异步日志使用的jar包

disruptor-3.3.4.jar

3.加入log4j1.x的适配jar包

log4j-1.2-api-2.5.jar
log4j2.x获取logger的方式已经改变,如果想要兼容以前的方式,需要这个jar包的辅助,建议引用第三方的jar包最好自己封装一层,要不以后jar包升级,会带来无尽的烦恼。

改动前:import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;

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


改动后:iimport org.apache.logging.log4j.LogManager;
      import org.apache.logging.log4j.Logger;

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

4.pom配置

           <log4j-api.version>2.5log4j-api.version>

           <dependency>
              <groupId>org.apache.logging.log4jgroupId>
              <artifactId>log4j-1.2-apiartifactId>
              <version>${log4j-api.version}version>
            dependency>
           <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-apiartifactId>
                <version>${log4j-api.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-coreartifactId>
                <version>${log4j-api.version}version>
            dependency>

          <dependency>
              <groupId>com.lmaxgroupId>
              <artifactId>disruptorartifactId>
              <version>3.3.4version>
            dependency>

升级配置文件

Log4j.xml升级为log4j2.xml



<configuration status="OFF">
    <appenders>
        
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d [%7r] %6p - %30.30c - %m \n" />
            
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
        Console>
        <RollingFile name="FILE-ALL" fileName="${file_path}/${file_name}.log"  
            filePattern="${file_path}/${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">    
            <PatternLayout pattern="%d [%7r] %6p - %30.30c - %m \n"/>    
            <SizeBasedTriggeringPolicy size="1024 MB" />    
        RollingFile>
    appenders>
    <loggers>
        <AsyncLogger name="com.xxx" level="debug" additivity="false">  
            <appender-ref ref="FILE-SQL" />
        AsyncLogger>
        <AsyncLogger name="org.mybatis" level="debug" additivity="false">  
            <appender-ref ref="FILE-SQL" />
        AsyncLogger>
        <AsyncLogger name="org.apache.ibatis" level="debug" additivity="false">  
            <appender-ref ref="FILE-SQL" />
        AsyncLogger>
        <AsyncLogger name="java.sql" level="debug" additivity="false">  
            <appender-ref ref="FILE-SQL" />
        AsyncLogger>
        <AsyncRoot level="info">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE-ALL" />
        AsyncRoot>
     loggers>
configuration>

如果要加上位置信息比如哪个类,第几行,需要在AsyncLogger 加入

includeLocation="true"

你可能感兴趣的:(日志管理)