欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot项目快速集成日志文件。利用resources\logback-spring.xml文件配置log日志的打印包括info,debug和error分开动态保留最近日志

程序员文章站 2022-03-05 08:13:23
...

比较完整配置文件如下:

当新建一个基于springboot的单项目时只需要把如下配置复制到项目根目录下\src\main\resources\logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <contextName>sms</contextName>
    <!--设置系统日志目录-->
    <property name="log.path" value="log" />
    <!--最多保留15天log-->
    <property name="log.maxHistory" value="15" />
    <property name="log.pattern" value="%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n" />

    <!--输出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>

    <!--输出到文件-->
    <!-- 日志记录器,日期滚动记录 -->
    <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 正在记录的日志文件的路径及文件名 -->
            <fileNamePattern>${log.path}/info/info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <MaxHistory>${log.maxHistory}</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <!-- 此日志文件只记录info级别的 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/error/error.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="file_debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/debug/debug.%d{yyyy-MM-dd}.log</fileNamePattern>
            <MaxHistory>${log.maxHistory}</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <root level="debug">
        <!--debug级别输出到文件和控制台-->
        <appender-ref ref="console" />
        <appender-ref ref="file_debug" />
    </root>

    <root level="info">
        <!--info和error级别输出到文件-->
        <appender-ref ref="file_info" />
        <appender-ref ref="file_error" />
    </root>
</configuration>

最近日志生成效果如下

[[email protected] qiweb]# tree log
log
├── debug
│   └── debug.2018-10-25.log
├── error
│   ├── error.2018-10-10.log
│   ├── error.2018-10-11.log
│   ├── error.2018-10-12.log
│   ├── error.2018-10-13.log
│   ├── error.2018-10-15.log
│   ├── error.2018-10-16.log
│   ├── error.2018-10-18.log
│   ├── error.2018-10-19.log
│   ├── error.2018-10-20.log
│   ├── error.2018-10-21.log
│   ├── error.2018-10-22.log
│   ├── error.2018-10-23.log
│   ├── error.2018-10-24.log
│   └── error.2018-10-25.log
└── info
    ├── info.2018-10-10.log
    ├── info.2018-10-11.log
    ├── info.2018-10-12.log
    ├── info.2018-10-13.log
    ├── info.2018-10-14.log
    ├── info.2018-10-15.log
    ├── info.2018-10-16.log
    ├── info.2018-10-17.log
    ├── info.2018-10-18.log
    ├── info.2018-10-19.log
    ├── info.2018-10-20.log
    ├── info.2018-10-21.log
    ├── info.2018-10-22.log
    ├── info.2018-10-23.log
    ├── info.2018-10-24.log
    └── info.2018-10-25.log

 

相关标签: logback

上一篇: logback 配置

下一篇: Swap Nodes in Pairs