log4j升级log4j2

依赖问题

  • log4j2目前最新的相关依赖是
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.3.7</version>
</dependency>
  • 几个比较重要的依赖
    log4j-1.2-api用于适配使用log4j接口,log4j-slf4j-impl用于桥接slf4j,disruptor是log4j2高性能的核心元素,用于async logger
  • 需要ban掉的依赖
<exclusion>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
</exclusion>
<exclusion>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
</exclusion>

包括且不限于引入了log4j相关接口的包

配置问题

log4j2的配置相较于log4j在精简上有比较大的改进,而且还可以支持一些比较皮的操作,比如动态配置,详细内容请看这里

在升级过程中,最简单的方式直接复制一份log4j2.xml到classpath路径下

举个例子:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Properties>
        <Property name="service">api</Property>
        <Property name="logFileRoot">/data/soft/log</Property>
        <Property name="pattern">[%p %d] %c{1.} [%t] %m%n</Property>
    </Properties>
    <Appenders>
        <RollingRandomAccessFile name="fileAppender" fileName="${logFileRoot}/${service}.log"
                                 filePattern="${logFileRoot}/bk/${service}-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <Pattern>${pattern}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="otherAppender" fileName="${logFileRoot}/${service}-other.log"
                                 filePattern="${logFileRoot}/bk/${service}-other-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <Pattern>${pattern}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="paodingAppender" fileName="${logFileRoot}/${service}-paoding.log"
                                 filePattern="${logFileRoot}/bk/${service}-paoding-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <Pattern>${pattern}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="passportAppender" fileName="${logFileRoot}/${service}-passport.log"
                                 filePattern="${logFileRoot}/bk/${service}-passport-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <Pattern>${pattern}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="rootAppender" fileName="${logFileRoot}/${service}-root.log"
                                 filePattern="${logFileRoot}/bk/${service}-root-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <Pattern>${pattern}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingRandomAccessFile>
        <!-- lcs 数据流配置 -->
        <RollingFile name="XLOGGER_SELF_APPENDER" fileName="${logFileRoot}/xlogger-self.log"
                     filePattern="${logFileRoot}/bk/xlogger-self-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <pattern>${pattern}</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="100MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了10 -->
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <!--记录测试数据-->
        <RollingFile name="trace-log" fileName="${logFileRoot}/xlogger-trace.log"
                     filePattern="${logFileRoot}/bk/xlogger-trace-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <pattern>${pattern}</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
        <!--Xlogger使用的appender-->
        <ScribeXLogger name="Scribe" cluster="${xlogger-cluster}" blockWhenBufferFull="false" />
    </Appenders>
    <Loggers>
        <AsyncLogger name="com.xxx.vip" level="${log4j_level}" additivity="false">
            <AppenderRef ref="fileAppender"/>
        </AsyncLogger>
        <AsyncLogger name="net.paoding.rose" level="${log4j_other_level}" additivity="false">
            <AppenderRef ref="paodingAppender"/>
        </AsyncLogger>
        <AsyncLogger name="com.xxx.passport" level="${log4j_other_level}" additivity="false">
            <AppenderRef ref="passportAppender"/>
        </AsyncLogger>
        <AsyncLogger name="com.xxx" level="${log4j_other_level}" additivity="false">
            <AppenderRef ref="otherAppender"/>
        </AsyncLogger>

        <!-- lcs 数据流配置 -->
        <AsyncLogger name="com.xxx.common.logger.v2.XLogger" level="${log4j_level}" additivity="false">
            <AppenderRef ref="Scribe"/>
        </AsyncLogger>
        <!--level设置为debug ,将xlogger写入的数据写入到文件中,用于测试使用-->
        <AsyncLogger name="com.xxx.common.logger.v2.XLoggerJson" level="${log4j_level}" additivity="false">
            <AppenderRef ref="trace-log"/>
        </AsyncLogger>
        <AsyncLogger name="com.xxx.common.logger.v2.ThriftScribeAppender" level="${log4j_level}" additivity="false" >
            <AppenderRef ref="XLOGGER_SELF_APPENDER"/>
        </AsyncLogger>

        <AsyncRoot level="${log4j_level}">
            <AppenderRef ref="rootAppender"/>
        </AsyncRoot>
    </Loggers>
</Configuration>
  • 使用的时候,修改一下service和logFileRoot为需要的参数即可

推荐阅读更多精彩内容