The log configurations of the web project and the core project are the same. Therefore, this article uses the core project as an example. Assume that the project depends on the following information:
groupId:
com.alipay.sofaartifactId:
APPNAME
According to the preceding information, the default log configuration file is the src/main/resources/logback-spring.xml in the service module. Example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<! -- Note: If you need to extend profile support with spring, the file name should be logback-spring.xml, not logback.xml. -->
<springProperty scope="context" name="logging.path" source="logging.path"/>
<springProperty scope="context" name="logging.level" source="logging.level.com.alipay.sofa"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss.SSS}%-5level%logger{80}-%msg%n</Pattern>
</encoder>
</appender>
<! -- Generate each log file in days -->
<appender name="ERROR-APPENDER"class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
<! -- Filter, which records only error-level logs -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<! -- Log name -->
<file>${logging.path}/APPNAME/common-error.log</file>
<! -- Generate a log file every day and save the log file for 30 days -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<! -- File name of log file output: rollback by day -->
<FileNamePattern>${logging.path}/APPNAME/common-error.log.%d{yyyy-MM-dd}</FileNamePattern>
<! -- Log file retention days -->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<! -- Formatted output:%d for date,%thread for thread name,%-5level: level display 5 characters width from left %msg: log message,%n is line break -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}[%thread]%-5level%logger{50}-%msg%n</pattern>
<! -- Encoding -->
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="ROOT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>${logging.level}</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${logging.path}/APPNAME/common-default.log</file>
<! -- Generate a log file every day and save the log file for 30 days -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<! -- File name of the log file output: rollback daily by day -->
<FileNamePattern>${logging.path}/APPNAME/common-default.log.%d{yyyy-MM-dd}
</FileNamePattern>
<! -- Log file retention days -->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<! -- Formatted output:%d for date,%thread for thread name,%-5level: level display 5 characters width from left %msg: log message,%n is line break -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}[%thread]%-5level%logger{50}-%msg%n</pattern>
<! -- Encoding -->
<charset>UTF-8</charset>
</encoder>
</appender>
<logger name="com.alipay.sofa" level="${logging.level}" additivity="false">
<appender-ref ref="ROOT-APPENDER"/>
<appender-ref ref="ERROR-APPENDER"/>
</logger>
<root level="${logging.level}">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ROOT-APPENDER"/>
<appender-ref ref="ERROR-APPENDER"/>
</root>
</configuration>
Description:
In the SOFABoot project created by using the Create Project documentation on the official website, three appenders are created in the default configuration.
Standard output (STDOUT): The standard output is used to ensure that startup information or exception information in some applications can be directly printed in the console when running locally. However, the logs of SOFA middleware are not displayed in the console. The logs of middleware are all in the middleware log directory of the specified directory.
error output (ERROR-APPENDER).
info output (ROOT-APPENDER).
This project creates two log-related configuration properties in the
resources/config/application.propertiesby default, which are:logging.level.com.alipay.sofa: indicates the log print level of the application.logging.path: indicates the log storage path of the application.
Configure the mapping relationship: the attributes in the
logback-spring.xmlare mapped to thelogging.pathandlogging.levelin theapplication.propertiesby using attribute keywords. Example:<springProperty scope="context" name="logging.path" source="logging.path"/> <springProperty scope="context" name="logging.level" source="logging.level.com.alipay.sofa"/>