logback->log4j2迁移

步骤一 剔除旧的依赖,引入新的依赖
主pom文件更改如下:

<log4j2.version>2.3log4j2.version>
<disruptor.version>3.3.2disruptor.version>

<dependency>
   <groupId>org.apache.logging.log4jgroupId>
   <artifactId>log4j-apiartifactId>
   <version>${log4j2.version}version>
dependency>
<dependency>
   <groupId>org.apache.logging.log4jgroupId>
   <artifactId>log4j-coreartifactId>
   <version>${log4j2.version}version>
dependency>
<dependency>
   <groupId>org.apache.logging.log4jgroupId>
   <artifactId>log4j-slf4j-implartifactId>
   <version>${log4j2.version}version>
dependency>
<dependency>
   <groupId>org.apache.logging.log4jgroupId>
   <artifactId>log4j-webartifactId>
   <version>${log4j2.version}version>
dependency>

<dependency>
   <groupId>com.lmaxgroupId>
   <artifactId>disruptorartifactId>
   <version>${disruptor.version}version>
dependency>

如果不是web项目,可以无需添加log4j-web
另外,要使用异步写日志功能需添加disruptor,官方建议一般程序员查看的日志改成异步方式,一些运营日志改成同步,详情请见官方说明:
http://logging.apache.org/log4j/2.x/manual/async.html#UnderTheHood
原有logback依赖不要物理删除,用exclusion

org.slf4j
slf4j-api


ch.qos.logback
logback-classic

步骤二 删除logback.xml 新增log4j2.xml
以IAPS为例

log4j2.xml配置如下


<configuration status="WARN">
   <Properties>
      <Property name="LOG_HOME">/wls/applogs/rtlog Property>
      <Property name="FILE_SIZE">300MProperty>
      <Property name="log_pattern">%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%nProperty>
      <Property name="rolling_file_name">-%d{yyyy-MM-dd}.%i.zipProperty>
      <Property name="rollover_strategy_max">30Property>

      <Property name="LOG_HOME_BIZ">${LOG_HOME}/iapslogs/iaps-web/bizProperty>
      <Property name="LOG_HOME_ERROR">${LOG_HOME}/iapslogs/iaps-web/errorProperty>
      <Property name="LOG_HOME_SERVICE">${LOG_HOME}/iapslogs/iaps-web/serviceProperty>
      <Property name="LOG_HOME_COMMON">${LOG_HOME}/iapslogs/iaps-web/commonProperty>
        Properties>
   <appenders>
      <Console name="Console" target="SYSTEM_OUT">
         <PatternLayout pattern="${log_pattern}" />
      Console>
      
      
         
      
      <RollingRandomAccessFile  name="bizRolling"
         fileName="${LOG_HOME_BIZ}/iaps-biz.txt"
         filePattern="${LOG_HOME_BIZ}/iaps-biz${rolling_file_name}"
         immediateFlush="false" append="true">
         <PatternLayout>
            <Pattern>${log_pattern}Pattern>
            <Charset>UTF-8Charset>
         PatternLayout>
         <Policies>
            <SizeBasedTriggeringPolicy size="${FILE_SIZE}"/>
         Policies>
         <DefaultRolloverStrategy max="${rollover_strategy_max}" />
      RollingRandomAccessFile>

      
      <RollingRandomAccessFile name="serviceRolling"
         fileName="${LOG_HOME_SERVICE}/iaps-service.txt"
         filePattern="${LOG_HOME_SERVICE}/iaps-service${rolling_file_name}"
         immediateFlush="false" append="true">
         <Filters>
            <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT" />
         Filters>
         <PatternLayout>
            <Pattern>${log_pattern}Pattern>
            <Charset>UTF-8Charset>
         PatternLayout>
         <Policies>
            <SizeBasedTriggeringPolicy size="${FILE_SIZE}" />
         Policies>
         <DefaultRolloverStrategy max="${rollover_strategy_max}" />
      RollingRandomAccessFile>

      
      <RollingRandomAccessFile name="errorRolling"
         fileName="${LOG_HOME_ERROR}/iaps-error.txt"
         filePattern="${LOG_HOME_ERROR}/iaps-error${rolling_file_name}"
         immediateFlush="false" append="true">
         <Filters>
            <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT" />
         Filters>
         <PatternLayout>
            <Pattern>${log_pattern}Pattern>
            <Charset>UTF-8Charset>
         PatternLayout>
         <Policies>
            <SizeBasedTriggeringPolicy size="${FILE_SIZE}" />
         Policies>
         <DefaultRolloverStrategy max="${rollover_strategy_max}" />
      RollingRandomAccessFile>
      
      <RollingRandomAccessFile name="iapsCommonRolling"
         fileName="${LOG_HOME_COMMON}/iaps-common.txt"
         filePattern="${LOG_HOME_COMMON}/iaps-common${rolling_file_name}"
         immediateFlush="false" append="true">
         <Filters>
            <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT" />
         Filters>
         <PatternLayout>
            <Pattern>${log_pattern}Pattern>
            <Charset>UTF-8Charset>
         PatternLayout>
         <Policies>
            <SizeBasedTriggeringPolicy size="${FILE_SIZE}" />
         Policies>
         <DefaultRolloverStrategy max="${rollover_strategy_max}" />
      RollingRandomAccessFile>
   appenders>

   <loggers>
      <AsyncLogger  name="paff.common" level="error"/>
      <AsyncLogger name="paff.aop" level="error"/>
      <AsyncLogger name="paff.sms" level="error"/>
      <AsyncLogger name="paff.email" level="error"/>
      <AsyncLogger name="com.pinganfu.pingamq" level="error"/>

<AsyncRoot  level="info">
         <appender-ref ref="Console"/>
         <appender-ref ref="bizRolling" />
         <appender-ref ref="errorRolling" />
      AsyncRoot>
   loggers>
configuration>

具体异步配置请参见官方文档
http://logging.apache.org/log4j/2.x/manual/async.html
步骤三 在web.xml配置log4j2

<param-name>log4jConfigurationparam-name>
<param-value>log4j2.xmlparam-value>

步骤四 修改LogUtil
以IAPS为例
Util的pom中添加

<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
dependency>
<dependency>
   <groupId>org.apache.logging.log4jgroupId>
   <artifactId>log4j-coreartifactId>
dependency>
<dependency>
   <groupId>org.apache.logging.log4jgroupId>
   <artifactId>log4j-slf4j-implartifactId>
dependency>
<dependency>
   <groupId>com.lmaxgroupId>
   <artifactId>disruptorartifactId>
dependency>

然后修改LogUtil

至此,迁移基本完成。

你可能感兴趣的:(java)