Log4j2 快速入门 —— 日志分级别输出到不同文件(二)

Log4j2 日志按级别输出到不同文件

在生产环境中,可以按日志信息级别,输出到不同的文件中,如生成 info.log,warn.log,error.log 等;也可以按照自定义的功能方式进行分类。比如接口API调用日志,定时任务日志等等。

这样的功能可以通过在 log4j2.xml 中配置Filter来实现;

示例代码(一)

log4j2.xml

<Configuration status="WARN" monitorInterval="500">
    <!--定义日志储存文件目录-->
    <properties>
        <property name="LOG_HOME">user/logs</property>
    </properties>
    <Appenders>
        <!--控制台输出所有日志-->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        
        <!--Info级别日志输出-->
        <RollingRandomAccessFile name="InfoFile"
                                 fileName="${LOG_HOME}/info/info.log"
                                 filePattern="${LOG_HOME}/info-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" />
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingRandomAccessFile>
        
        <!--Error级别日志输出-->
        <RollingRandomAccessFile name="ErrorFile"
                                 fileName="${LOG_HOME}/error/error.log"
                                 filePattern="${LOG_HOME}/error-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL" />
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingRandomAccessFile>
        
        <!--Fatal级别日志输出-->
        <RollingRandomAccessFile name="FatalFile"
                                 fileName="${LOG_HOME}/fatal/fatal.log"
                                 filePattern="${LOG_HOME}/fatal-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="fatal" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingRandomAccessFile>
    </Appenders>
    
    <Loggers>
        <Root level="Debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="InfoFile" />
            <AppenderRef ref="ErrorFile" />
            <AppenderRef ref="FatalFile" />
        </Root>
    </Loggers>
</Configuration>

示例二

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">

    <Appenders>
        <!--这个输出控制台的配置-->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout charset="UTF-8" pattern="[%t] %d{yyyy-MM-dd HH:mm:ss,SSS a} [%p] %l - %msg%n"/>
        </Console>

        <!-- 日常info输出-->
        <RollingFile name="XmasLogFile" fileName="${sys:catalina.home}/logs/xmas.log" filePattern="${sys:catalina.home}/logs/xmas-%d{yyyy-MM-dd}.log" append="true">

            <!--日志格式-->
            <PatternLayout charset="UTF-8" pattern="[%t] %d{yyyy-MM-dd HH:mm:ss,SSS a} [%p] %l - %msg%n"/>

            <!-- 设置间隔时间 -->
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>

        <!-- 日常error输出-->
        <RollingFile name="XmasErrorFile" fileName="${sys:catalina.home}/logs/xmas-error.log" filePattern="${sys:catalina.home}/logs/xmas-error-%d{yyyy-MM-dd}.log" append="true">
            <PatternLayout charset="UTF-8" pattern="[%t] %d{yyyy-MM-dd HH:mm:ss,SSS a} [%p] %l - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>


        <!--Quartz定时任务日志-->
        <RollingFile name="XmasQuartzLogFile" fileName="${sys:catalina.home}/logs/xmas-quartz.log" filePattern="${sys:catalina.home}/logs/xmas-quartz-%d{yyyy-MM-dd}.log" append="true">
            <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS z} [%t] %-5level - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>

        <!--api接口请求-->
        <RollingFile name="XmasApiLogFile" fileName="${sys:catalina.home}/logs/xmas-api.log" filePattern="${sys:catalina.home}/logs/xmas-api-%d{yyyy-MM-dd}.log" append="true">
            <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS z} [%t] %-5level - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>

        <!--日志统计-->
        <RollingFile name="XmasAnalyzeLogFile" fileName="${sys:catalina.home}/logs/analyze-xmas.log" filePattern="${sys:catalina.home}/logs/analyze-xmas-%d{yyyy-MM-dd}.log" append="true">
            <PatternLayout charset="UTF-8" pattern="%msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <logger name="com.chengxumiao.xmas.interceptor.TimeInterceptor" level="info" additivity="false">
            <appender-ref ref="XmasApiLogFile"/>
        </logger>

        <logger name="com.chengxumiao.xmas.logAnalyzer.TimelogAnalyzer" level="info" additivity="false">
            <appender-ref ref="XmasAnalyzeLogFile"/>
        </logger>

        <logger name="com.chengxumiao.xmas.quartz" level="info" additivity="false">
            <appender-ref ref="XmasQuartzLogFile"/>
        </logger>

        <Logger name="com.chengxumiao.xmas" level="trace" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="XmasLogFile"/>
            <appender-ref ref="xmasErrorFile"/>
        </Logger>

        <Root level="warn"/>
    </Loggers>

</Configuration>

一般项目中其他jar包引用的是log4j.properties文件,比如tomcat启动时候等一些日志,所以可以配置 log4j2.xml 和 log4j.properties 两个文件共存,这样所有的日志都能记录下来了,我习惯于 log4j2.xml 按照功能,类别拆分,log4j.properties只输出一些控制台打印

log4j.rootLogger=info, stdout

# Console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%t] %d{yyyy-MM-dd HH:mm:ss,SSS} [%p]:%l - %m%n


赞(52) 打赏
未经允许不得转载:优客志 » JAVA开发
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏