log4j2的高並發死鎖問題配置優化方式

log4j2高並發死鎖問題配置優化

Maven中pom.xml引用

<log4j2.version>2.7</log4j2.version>

版本以上

<!-- log4j2 高並發造成死鎖 -->
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-api</artifactId>
   <version>${log4j2.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>${log4j2.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-web</artifactId>
   <version>${log4j2.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-slf4j-impl</artifactId>
   <version>${log4j2.version}</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
  <dependency>
   <groupId>com.lmax</groupId>
   <artifactId>disruptor</artifactId>
   <version>3.3.6</version>
  </dependency>

配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!--日志級別以及優先級排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration後面的status,這個用於設置log4j2自身內部的信息輸出,可以不設置,當設置成trace時,你會看到log4j2內部各種詳細輸出-->
<!--monitorInterval:Log4j能夠自動檢測修改配置 文件和重新配置本身,設置間隔秒數-->
 <configuration status="WARN" monitorInterval="30">
     <!--先定義所有的appender-->
     <appenders>
     <!--這個輸出控制臺的配置-->
       <!--   <console name="Console" target="SYSTEM_OUT">
         輸出日志的格式
             <PatternLayout charset="GBK" pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
         </console> -->
     <!--文件會打印出所有信息,這個log每次運行程序會自動清空,由append屬性決定,這個也挺有用的,適合臨時測試用-->
    <!--  <File name="log" fileName="log/test.log" append="false">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
     </File> -->
     <!-- 這個會打印出所有的info及以下級別的信息,每次大小超過size,則這size大小的日志會自動存入按年份-月份建立的文件夾下面並進行壓縮,作為存檔-->
          <!-- ${sys:user.home} 當前用戶目錄,但創建文件夾沒權限失敗的情況 -->
          <RollingRandomAccessFile  name="RollingRandomAccessFile-RcsDataSys-Debug" fileName="/logs/RcsDataSys/RcsDataSys-debug.log"
                      filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-debug-%d{yyyy-MM-dd}-%i.log" immediateFlush="true" Append="true">
             <!--控制臺隻輸出level及以上級別的信息(onMatch),其他的直接拒絕(onMismatch)-->     
                
              <Filters>
               <!-- 禁止error日志進入 純的info日志-->
              <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
              <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
             </Filters>
             <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="50 MB"/>
             </Policies>
             <DefaultRolloverStrategy max="15">  
                <Delete basePath="/logs/RcsDataSys" maxDepth="2">  
                  <IfFileName glob="*/RcsDataSys-debug*.log" />  
                  <IfLastModified age="3d" />  
                </Delete>  
            </DefaultRolloverStrategy> 
         </RollingRandomAccessFile >
         <RollingRandomAccessFile  name="RollingRandomAccessFile-RcsDataSys-Info" fileName="/logs/RcsDataSys/RcsDataSys-info.log"
                      filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-info-%d{yyyy-MM-dd}-%i.log" immediateFlush="true" >
             <!--控制臺隻輸出level及以上級別的信息(onMatch),其他的直接拒絕(onMismatch)-->        
              <Filters>
               <!-- 禁止error日志進入 純的info日志-->
              <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
              <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
             </Filters>
             <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
             <DefaultRolloverStrategy max="3">  
                <Delete basePath="/logs/RcsDataSys" maxDepth="2">  
                  <IfFileName glob="*/RcsDataSys-info*.log" />  
                  <IfLastModified age="7d" />  
                </Delete>  
            </DefaultRolloverStrategy> 
         </RollingRandomAccessFile >
         <RollingRandomAccessFile  name="RollingRandomAccessFile-RcsDataSys-Warn" fileName="/logs/RcsDataSys/RcsDataSys-warn.log"
                      filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-warn-%d{yyyy-MM-dd}-%i.log" immediateFlush="true">
             <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
         <!-- DefaultRolloverStrategy屬性如不設置,則默認為最多同一文件夾下7個文件,這裡設置瞭20 -->
             <DefaultRolloverStrategy max="2"/>
         </RollingRandomAccessFile >
         <RollingRandomAccessFile  name="RollingRandomAccessFile-RcsDataSys-Error" fileName="/logs/RcsDataSys/RcsDataSys-error.log"
                      filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-error-%d{yyyy-MM-dd}-%i.log" immediateFlush="true">
             <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="50 MB"/>
             </Policies>
              <DefaultRolloverStrategy max="3">  
                <Delete basePath="/logs/RcsDataSys" maxDepth="2">  
                  <IfFileName glob="*/RcsDataSys-error*.log" />  
                  <IfLastModified age="3d" />  
                </Delete>  
            </DefaultRolloverStrategy> 
         </RollingRandomAccessFile >
     </appenders>
     <!--然後定義logger,隻有定義瞭logger並引入的appender,appender才會生效-->
     <loggers>
         <!--過濾掉spring和mybatis的一些無用的DEBUG信息-->
         <logger name="org.springframework" level="INFO"></logger>
         <logger name="org.mybatis" level="INFO"></logger>
         <logger name="org.apache.ibatis" level="INFO"></logger>
         <logger name="org.apache.ibatis.logging.jdbc.BaseJdbcLogger" additivity="false"></logger>
        <asyncRoot level="DEBUG" includeLocation="true">  
             <appender-ref ref="RollingRandomAccessFile-RcsDataSys-Debug"/>
          <appender-ref ref="RollingRandomAccessFile-RcsDataSys-Info"/>
          <appender-ref ref="RollingRandomAccessFile-RcsDataSys-Error"/>
     </asyncRoot>   
       <logger name="druid.sql.Statement" level="info" additivity="false">
        </logger>
        <logger name="druid.sql.ResultSet" level="info" additivity="false">
        </logger>
        <logger name="com.alibaba.druid" level="info" additivity="false">
        </logger>
        <logger name="org.quartz" level="info" additivity="false">
        </logger>
        
     </loggers>
</configuration>

log4j2.x簡單使用文檔

簡單總結log4j2.x的使用過程。

1.項目中引入兩個jar包

  • log4j-core-2.0-beta9.jar
  • log4j-api-2.0-beta9.jar

2.指定日志配置文件位置

//指定日志的配置器文件  
    static{  
        System.setProperty("log4j.configurationFile", "./log4j2.xml");  
    } 

3.簡單的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <!--這個會打印出所有的信息,每次大小超過size,則這size大小的日志會自動存入按年份-月份建立的文件夾下面並進行壓縮,作為存檔-->
        <RollingFile name="RollingFile" fileName="logs/app.log"
                     filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <SizeBasedTriggeringPolicy size="50MB"/>
        </RollingFile>
    </appenders>
    <loggers>
        <logger name="com.klaus.tcp.synchronous.TcpSocketN" level="debug" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFile"/>
        </logger>
        <root level="error">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFile"/>
        </root>
    </loggers>
</configuration>

4.獲取Logger對象後即可寫日志瞭

log = LogManager.getLogger(TcpSocketN.class.getName());
log.error(e.getMessage());

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: