一. springboot中使用日志并不是用传统的log4j。
需要的依赖包如下:
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.1.11version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.1.11version>
二. 配置文件在resources根目录下:
记住,名字一定要取成logback-spring.xml,否则log加载顺序会在yml文件之前,而log的输出日志目录是在yml文件中配置的,这样就导致会加载错误。
三. logback-spring.xml的内容:
xmlversion="1.0"encoding="UTF-8"?>
<configuration>
<springPropertyscope="context"name="log.path"source="log.path"
defaultValue="app-logs"/>
<propertyname="log.pattern"value="%d{HH:mm:ss.SSS}[%thread]%-5level%logger{20}-[%method,%line]-%msg%n"/>
<appendername="console"class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}pattern>
encoder>
appender>
<appendername="file_info"class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-info.logfile>
<rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filterclass="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appendername="file_error"class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-error.logfile>
<rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filterclass="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appendername="sys-user"class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-user.logfile>
<rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
appender>
<loggername="com.sanss"level="info"/>
<loggername="org.springframework"level="warn"/>
<rootlevel="info">
<appender-refref="console"/>
root>
<rootlevel="info">
<appender-refref="file_info"/>
<appender-refref="file_error"/>
root>
<loggername="sys-user"level="info">
<appender-refref="sys-user"/>
logger>
configuration>
四. yml文件中log目录的配置:
这里的log: path 对应logback-spring.xml里的: