Spring boot project notes a log file configuration


[b] Description: [/b] I have already written a project with spring boot, and the second project still has the advantages when building the framework. Look up information everywhere, especially the data link. So I still have time to record it now, even if I look for it next time, I will only look for my own blog.
If it is too basic, I won't say it. You must use maven to build the springboot project framework, unless you want to toss yourself to death.

Before building springboot, it is still necessary to understand the framework of springMVC. Springboot actually encapsulates springMVC. This is the foundation, otherwise it will be very difficult.

The construction is simple - go to the springboot official website to build a springboot dome, and then import the IDE,

I like to use the yml file format, so change the application file to the yml file format.

Add the port to start the service in the application.yml file: the default is 8080 if not added

server:
    port: 8090

The structure is shown in the figure:


 Convenience can't be in convenience anymore. Next we talk about the configuration of the log file:

1. Add the configuration of the log file to application.yml: I like to use the yml file format here, and you can also use perpertites.

logging:
  config: classpath:logback-spring.xml
  path: /applogs
  level:
    root: info

 illustrate:

logback-spring.xml is the file to be created under resource to configure the log configuration file, similar to the previous log4j configuration file

path: directory where log files are generated

level: log level

2. Looking at the content of the logback-spring.xml file, paste my content directly:

Among them, I specially printed out the process ID PID of the service running in the log permission format, so that it is convenient to directly KILL the service.

In fact, you can take a look at the original configuration file under the springboot jar package, as shown in the figure:



 
logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">

    <property name="LOG_FILE" value="${LOG_PATH}/spring.log"/>
    <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] ${PID:- } %-5level %logger{50} - %msg%n"/>

    < appender name ="CONSOLE" class ="ch.qos.logback.core.ConsoleAppender" > 
        < encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > 
            <!-- Formatted output: %d means date, %thread is thread name, PID : process ID %-5level : level is displayed 5 characters from left width %msg : log message, %n is newline -->
 < pattern > ${LOG_PATTERN} </ pattern > 
        < / encoder > 
    </ appender > 
    <!--file-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Append>true</Append>
        <File>${LOG_FILE}</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
<FileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender><root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326272636&siteId=291194637