log4j2日志输出XML文件配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <properties>
        <property name="APP_NAME">应用名称</property>
        <!-- 文件输出格式 -->
        <property name="PATTERN">[%d{yyyy-MM-dd HH:mm:ss:SSS z}] [traceId:%X{X-B3-TraceId}] %5p %l: %m%n</property>
        <!-- 文件输出路径 -->
        <Property name="FILE_PATH">/service/logs/app/prod/crm/${APP_NAME}</Property>
    </properties>

    <appenders>
        <!--控制台上打印的日志为json格式 -->
        <Console name="CONSOLE" target="system_out">
            <PatternLayout pattern="${PATTERN}" />
        </Console>
        <!-- error -->
        <RollingFile name="errorRollingFile" fileName="${FILE_PATH}/error.log"
                     filePattern="${FILE_PATH}/error-%i.log">
            <SizeBasedTriggeringPolicy size="500MB" />
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了10 -->
            <DefaultRolloverStrategy max="10" />
            <ThresholdFilter level="error" onMatch="ACCEPT"
                             onMismatch="DENY" />
            <JsonLayout locationinfo="true" eventEol="true" compact="true"
                        stacktraceAsString="true" includeStacktrace="true">
                <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS}" />
                <KeyValuePair key="project_name" value="${APP_NAME}" />
                <KeyValuePair key="trace_id" value="${ctx:X-B3-TraceId}" />
                <KeyValuePair key="span_id" value="${ctx:spanId}" />
            </JsonLayout>
        </RollingFile>
        <!-- warn -->
        <RollingFile name="warnRollingFile" fileName="${FILE_PATH}/warn.log"
                     filePattern="${FILE_PATH}/warn-%i.log">
            <SizeBasedTriggeringPolicy size="500MB" />
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了10 -->
            <DefaultRolloverStrategy max="10" />
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
                <ThresholdFilter level="warn" onMatch="ACCEPT"
                                 onMismatch="DENY" />
            </Filters>
            <JsonLayout locationinfo="true" eventEol="true" compact="true"
                        stacktraceAsString="true" includeStacktrace="true">
                <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS}" />
                <KeyValuePair key="project_name" value="${APP_NAME}" />
                <KeyValuePair key="trace_id" value="${ctx:X-B3-TraceId}" />
                <KeyValuePair key="span_id" value="${ctx:spanId}" />
            </JsonLayout>
        </RollingFile>
        <!-- info -->
        <RollingFile name="infoRollingFile" fileName="${FILE_PATH}/info.log"
                     filePattern="${FILE_PATH}/info-%i.log">
            <SizeBasedTriggeringPolicy size="500MB" />
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了10 -->
            <DefaultRolloverStrategy max="10" />
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
                <ThresholdFilter level="warn" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
                <ThresholdFilter level="info" onMatch="ACCEPT"
                                 onMismatch="DENY" />
            </Filters>
            <JsonLayout locationinfo="true" eventEol="true" compact="true"
                        stacktraceAsString="true" includeStacktrace="true">
                <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS}" />
                <KeyValuePair key="project_name" value="${APP_NAME}" />
                <KeyValuePair key="trace_id" value="${ctx:X-B3-TraceId}" />
                <KeyValuePair key="span_id" value="${ctx:spanId}" />
            </JsonLayout>
        </RollingFile>
        <!-- debug -->
        <RollingFile name="debugRollingFile" fileName="${FILE_PATH}/debug.log"
                     filePattern="${FILE_PATH}/debug-%i.log">
            <SizeBasedTriggeringPolicy size="100KB" />
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了10 -->
            <DefaultRolloverStrategy max="10" />
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
                <ThresholdFilter level="warn" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
                <ThresholdFilter level="info" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
                <ThresholdFilter level="debug" onMatch="ACCEPT"
                                 onMismatch="DENY" />
            </Filters>
            <JsonLayout locationinfo="true" eventEol="true" compact="true"
                        stacktraceAsString="true" includeStacktrace="true">
                <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS}" />
                <KeyValuePair key="project_name" value="${APP_NAME}" />
                <KeyValuePair key="trace_id" value="${ctx:X-B3-TraceId}" />
                <KeyValuePair key="span_id" value="${ctx:spanId}" />
            </JsonLayout>
        </RollingFile>
    </appenders>
    <loggers>
        <root level="DEBUG">
            <appenderref ref="CONSOLE" />
            <appenderref ref="errorRollingFile" />
            <appenderref ref="warnRollingFile" />
            <appenderref ref="infoRollingFile" />
            <appenderref ref="debugRollingFile" />
        </root>
    </loggers>

</configuration>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值