springboot configuration log file

Posted by golfinggod on Fri, 17 Dec 2021 16:59:46 +0100

Logback Manualhttp://logback.qos.ch/manual/index.html To create a new springboot project, configure the log output file logback XML to control log output through custom configuration files. Usually, we configure three log components:

<configuration scan="true" scanPeriod="2 seconds">
    <!--Define the storage address of the log file-->
    <property name="LOG_PATH" value="/home/iot/logs" />
    <!-- console output  -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Format output:%d Represents the date, thread,%-5level: The level is 5 characters wide from the left,%t Represents the thread name,%msg: Log messages,%n Is a newline character-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level ${PID:-} --- [%t] %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- info Level log file output -->
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The file name of the log file output -->
        <File>${LOG_PATH}/Project name-info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- Daily generated log file or file name template output after the log file size exceeds the limit -->
            <fileNamePattern>${LOG_PATH}/Project name-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!-- Log file retention days -->
            <maxHistory>30</maxHistory>
            <!-- Maximum log file size: 100 MB -->
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level ${PID:-} --- [%t] %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- error Level log file output -->
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- Log output level, priority > '<root level>' -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <File>${LOG_PATH}/Project name-error.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/Project name-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level ${PID:-} --- [%t] %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Default log output level -->
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="INFO_FILE" />
        <appender-ref ref="ERROR_FILE" />
    </root>

</configuration>

logback parameter configuration description

  • configuration contains the following three attributes:

scan: overload when the configuration file is changed. The default value is true

scanPeriod: the time interval for monitoring whether the configuration file is modified. The default value is 6000 and the default unit is milliseconds

debug: print the internal log information of logback and check the running status of logback in real time. The default value is false

  • Child node property (you can make ${} use variables):
    Define variable values, including the following two properties

Name: variable name

Value: the value defined by the variable

  • Child node appender:

It is a component responsible for writing logs. It controls the log output through the custom log component, including the following two properties:

Name: component name
Class: the class name specified by the component class

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

</appender>
ch.qos.logback.core.ConsoleAppender  #The log will be output to the console

ch.qos.logback.core.rolling.RollingFileAppender #Output the log contents to the specified file
  • File node: the file name of the log file output
  • Filter node: a filter used to specify the log output level of the log component, which is higher than the level of the root node.
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>ERROR</level>
</filter>
  • rollingPolicy node:

Rolling log file configuration involves moving and renaming log files. There is only one class attribute to specify the rolling policy

ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy

ch.qos.logback.core.rolling.TimeBasedRollingPolicy

Contains the following three attributes:

<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <fileNamePattern>${LOG_PATH}/Project name-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    <maxHistory>30</maxHistory>
    <maxFileSize>100MB</maxFileSize>
</rollingPolicy>

fileNamePattern: how the log is named when scrolling occurs
maxHistory: the maximum retention time of the log file. It will be automatically deleted after exceeding the set time
maxFileSize: the maximum limit of each log file. If the limit is exceeded, it will be regenerated, and the old log file will be named according to the log naming method set by fileNamePattern

  • Child node encoder

Formatting recorded events is responsible for two things: one is to convert the log information into a byte array, and the other is to write the byte array to the output stream.
PatternLayoutEncoder is the only useful and default encoder (can not be written). It has a node to set the input format of the log. Use '%' plus' Converter '. If you want to output'% ', you must escape'% 'with' '.

  • Child node root

The root logger is the highest level of all loger s and has only one level node. It is used to set the level of printing logs. The default values are debug, info, trace, warn, error, all and off. The ref attribute of the child node appender ref specifies the name of the log component, that is, the value of the appender's name attribute.

Topics: Java Spring Boot Back-end