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
    评论
Spring Boot 配置 Log4j2 日志可以通过以下步骤实现: 1. 在 pom.xml 文件中添加 Log4j2 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> ``` 2. 在 src/main/resources 目录下创建 log4j2.xml 文件,配置日志输出格式、级别、文件路径等信息。例如: ```xml <?xml version="1." encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="10MB"/> </Policies> </RollingFile> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> ``` 3. 在 Spring Boot 的配置文件 application.properties 或 application.yml 中添加以下配置: ```properties logging.config=classpath:log4j2.xml ``` 或 ```yaml logging: config: classpath:log4j2.xml ``` 这样就完成了 Spring Boot 配置 Log4j2 日志的过程。在代码中使用 Log4j2 日志可以直接通过注入 Logger 对象来实现,例如: ```java import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @Service public class MyService { private static final Logger logger = LogManager.getLogger(MyService.class); public void doSomething() { logger.info("doing something..."); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值