Spring Boot+Logback outputs logs to the specified path

Spring Boot projects are generally packaged in jar or war format. At this time, you must hope that the log will be output to a specified location, which is convenient for later system log analysis.
 
We also hope that the log can be generated according to our own rules. For example, we want to generate logs in days, and we also hope that when the log is larger than the specified size, it will be automatically split (in other words, log files of tens or hundreds of megabytes are opened. Time is also very annoying).
 
How do we do it?
 
Use Logback+lombok to meet the requirements! Powerful configuration and easy to use! 

1. Configure the dependency of lombok in pom.xml

 <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
 </dependency>

2. Install the lombok plugin (in IDEA)

Spring Boot+Logback outputs logs to the specified path

3. Increase the configuration of logback

Add a configuration file logback-spring.xml to Spring Boot (location and yml should be in one place for easy management), as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, -->
    <!-- appender是configuration的子节点,是负责写日志的组件。 -->
    <!-- ConsoleAppender:把日志输出到控制台 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %p (%file:%line\)- %m%n</pattern>
            <!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <!-- RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
    <!-- 以下的大概意思是:1.先按日期存日志,日期变了,将前一天的日志文件名重命名为XXX%日期%索引,新的日志仍然是sys.log -->
    <!--             2.如果日期没有发生变化,但是当前日志的文件大小超过1KB时,对当前日志进行分割 重命名-->
    <appender name="syslog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>D:systemlog\sys.log</File>
        <!-- rollingPolicy:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。 -->
        <!-- TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 -->
            <!-- 文件名:sys.2017-12-05.0.log -->
            <fileNamePattern>D:\systemlog\sys.%d.%i.log</fileNamePattern>
            <!-- 每产生一个日志文件,该日志文件的保存期限为30天 -->
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy  class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为2MB-->
                <maxFileSize>2MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <!-- pattern节点,用来设置日志的输入格式 -->
            <pattern>
                %d %p (%file:%line\)- %m%n
            </pattern>
            <!-- 记录日志的编码 -->
            <charset>UTF-8</charset> <!-- 此处设置字符集 -->
        </encoder>
    </appender>
    <!-- 控制台输出日志级别,并且存入日志文件 -->
    <root level="info">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="syslog" />
    </root>
    <!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
    <!-- com.appley为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
    <!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE
    <logger name="org.springframework" level="warn">
        <appender-ref ref="syslog" />
    </logger>-->
</configuration>

The above comments have been written in place, you can copy and use them directly, the focus is only to pay attention to the two places shown in the figure below, as follows:
Spring Boot+Logback outputs logs to the specified path

4. Introduce logback-spring.xml in the yml configuration file in Spring Boot

Spring Boot+Logback outputs logs to the specified path

5. Log application

Add the annotation @Slf4j to the class that you want to output the log, and then use log.info, log.error, log.warn to output the log.
Spring Boot+Logback outputs logs to the specified path

Guess you like

Origin blog.51cto.com/3058076/2657441