• 新的log4j2.xml


    <?xml version="1.0" encoding="UTF-8"?>
    <!-- OFF < FATAL < ERROR < WARN < INFO < DEBUG < TRACE < ALL -->
    <configuration status="DEBUG">
        <Properties>
            <Property name="LOG_HOME">log</Property>
            <Property name="LOG_NAME">app_name</Property>
        </Properties>
        <appenders>
            <!-- 控制台 -->
            <Console name="console" target="SYSTEM_OUT">
                <ThresholdFilter level="info" onMatch="ACCEPT"
                    onMismatch="DENY" />
                <PatternLayout charset="UTF-8"
                    pattern="%d{HH:mm:ss} %-5level [%thread][%file:%line] - %msg%n" />
            </Console>
            <!-- warn日志文件 -->
            <RollingFile name="warnFile" fileName="${LOG_HOME}/${LOG_NAME}-WARN.log"
                filePattern="${LOG_HOME}/${LOG_NAME}-WARN.%d{yyyy-MM-dd}.log"
                append="true">
                <Filters>
                    <ThresholdFilter level="error" onMatch="DENY"
                        onMismatch="NEUTRAL" />
                    <ThresholdFilter level="warn" onMatch="ACCEPT"
                        onMismatch="DENY" />
                </Filters>
                <PatternLayout charset="UTF-8"
                    pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread][%file:%line] - %msg%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy
                        modulate="true" interval="1" />
                </Policies>
                <DefaultRolloverStrategy max="180" />
            </RollingFile>
            <!-- error日志文件 -->
            <RollingFile name="errorFile"
                fileName="${LOG_HOME}/${LOG_NAME}-ERROR.log" filePattern="${LOG_HOME}/${LOG_NAME}-ERROR.%d{yyyy-MM-dd}.log"
                append="true">
                <ThresholdFilter level="error" onMatch="ACCEPT"
                    onMismatch="DENY" />
                <PatternLayout charset="UTF-8"
                    pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread][%file:%line] - %msg%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy
                        modulate="true" interval="1" />
                </Policies>
                <DefaultRolloverStrategy max="180" />
            </RollingFile>
            <!-- info以上日志文件 -->
            <RollingFile name="infoFile"
                fileName="${LOG_HOME}/${LOG_NAME}-INFO+.log" filePattern="${LOG_HOME}/${LOG_NAME}-INFO+.%d{yyyy-MM-dd}.log"
                append="true">
                <ThresholdFilter level="info" onMatch="ACCEPT"
                    onMismatch="DENY" />
                <PatternLayout charset="UTF-8"
                    pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread][%file:%line] - %msg%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy
                        modulate="true" interval="1" />
                </Policies>
                <DefaultRolloverStrategy max="180" />
            </RollingFile>
            <!-- debug以上日志文件 -->
            <RollingFile name="debugFile"
                fileName="${LOG_HOME}/${LOG_NAME}-DEBUG+.log" filePattern="${LOG_HOME}/${LOG_NAME}-DEBUG+.%d{yyyy-MM-dd}.log"
                append="true">
                <ThresholdFilter level="debug" onMatch="ACCEPT"
                    onMismatch="DENY" />
                <PatternLayout charset="UTF-8"
                    pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread][%file:%line] - %msg%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy
                        modulate="true" interval="1" />
                </Policies>
                <DefaultRolloverStrategy max="180" />
            </RollingFile>
        </appenders>
        <loggers>
            <root level="all">
                <appender-ref ref="console" />
                <appender-ref ref="debugFile" />
                <appender-ref ref="infoFile" />
                <appender-ref ref="warnFile" />
                <appender-ref ref="errorFile" />
            </root>
        </loggers>
    </configuration>

    主要是定义了4个RollingFile,使项目运行时能产生4个日志文件,分别代表“只记录warn级日志”、“只记录error级日志”、“记录info及其以上级别的日志”、“记录debug及其以上级别的日志”,既有完整日志,又有分类细化日志。

    首先需要明确日志的重要级。

    FATAL  ERROR  WARN  INFO  DEBUG  TRACE

    越左边的越重要。

    不同的RollingFile显示不同级别的日志是通过ThresholdFilter标签配置的,以name为warnFile的RollingFile为例:

    <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" />

    代表比error(level)或比error更重要的日志(onMatch),在这个RollingFile中不显示(DENY);比error不重要的日志(onMismatch),在这个RollingFile中是否显示交给其他ThresholdFilter标签来决定(NEUTRAL)。

    <ThresholdFilter level="warn" onMatch="ACCEPT"  onMismatch="DENY" />

    代表比warn(level)或比warn更重要的日志(onMatch),在这个RollingFile中显示(ACCEPT);比warn不重要的日志(onMismatch),在这个RollingFile中不显示(DENY)。

    结合两个ThresholdFilter,最终的结果是只显示warn级的日志。(Deolin开发时一般用不到FATAL和TRACE)

    需要感谢两篇博客:

    https://www.cnblogs.com/hafiz/p/6170702.html

    http://bglmmz.iteye.com/blog/2154490

  • 相关阅读:
    [考试]20150811
    [考试]20150810
    [随笔]暑假过了,暑假来了
    [考试]20150808
    动态规划大合集II
    [知识点][旧版]C++中的运算符
    NOIP动态规划大合集
    [考试]20150729
    [考试]20150728
    /=============分隔线=============/
  • 原文地址:https://www.cnblogs.com/deolin/p/7877483.html
Copyright © 2020-2023  润新知