The spring boot project quickly configures logback to output logs to a file [example]

ps This is an example of small white text, just copy and paste, nothing special

The first step is to create logback-spring.xml

Create logback-spring.xml under classpath:resources

The second step is to copy and paste the configuration below

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <springProperty scope="context" name="APPLICATION_NAME" source="spring.application.name"/>

    <!-- 日志目录   -->
    <property name="LOG_HOME" value="${user.dir}/logs"/>
    <!--    日志文件名-->
    <property name="APP_NAME" value="${APPLICATION_NAME:-.}"/>

    <!--    使用默认的输出格式-->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <appender name="DefaultAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${APP_NAME}.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <!--        基于大小的滚动策略-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <!--    异步输出-->
    <appender name="AsyncFileAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="DefaultAppender"/>
    </appender>

    <root>
        <level value="INFO"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="AsyncFileAppender"/>
    </root>
</configuration>

Complete, the rest can be configured according to your own business.

 

Description

The spring boot default log is output to the console. If you want to output to a specified file, you can also configure it on configuration files such as applicato.yml, as follows:

If you need a more refined control of the log output, or you want to strip off this part of the configuration, you can use the above method; in addition, the file name is logback-spring.xml and you can use some advanced configurations of spring boot.

Guess you like

Origin blog.csdn.net/x763795151/article/details/114737270