温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot中如何自定义日志 log4j2

发布时间:2021-07-29 15:33:59 来源:亿速云 阅读:160 作者:Leah 栏目:大数据

spring boot中如何自定义日志 log4j2,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1 修改spring-boot-starter的dependency

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
  </dependency>

添加我们需要自定义的logging的dependency,这里用的是log4j2

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
2 自定义配置文件log4j2.xml
  • Spring boot对自定义配置文件的名称是有要求的,对Login4j2而言必须为log4j2-spring.xml or log4j2.xml

  • 关于配置文件中的参数,详细参考官方文档

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appenders>
        <!-- 控制台输出 -->
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class %L %M - %msg%n"/>
        </console>

        <!-- fileName:输出路径  filePattern:命名规则 -->
        <RollingFile name="all" fileName="logs/allOut.log"
                     filePattern="logs/$${date:yyyy-MM-dd}/allOut-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="all" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <!-- 输出格式 -->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%n"/>
            <Policies>
                <!-- SizeBasedTriggeringPolicy单个文件的大小限制 -->
                <SizeBasedTriggeringPolicy size="2 MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy同一个文件下的最大文件数 -->
            <DefaultRolloverStrategy max="50"/>
        </RollingFile>

        <RollingFile name="err" fileName="logs/err.log"
                     filePattern="logs/$${date:yyyy-MM-dd}/err-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <!-- 输出格式 -->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <Policies>
                <!-- SizeBasedTriggeringPolicy单个文件的大小限制 -->
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy同一个文件下的最大文件数 -->
            <DefaultRolloverStrategy max="50"/>
        </RollingFile>
    </appenders>

    <loggers>
        <!--过滤掉spring无用的debug信息-->
        <logger name="org.springframework" level="error"></logger>

        <root level="debug">
            <appender-ref ref="Console"/>
            <appender-ref ref="all"/>
            <appender-ref ref="err"/>
        </root>
    </loggers>

</configuration>

关于spring boot中如何自定义日志 log4j2问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI