<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
在resources目录下新建logback-spring.xml文件
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logscontextName>
<property name="log.path" value="log" />
<property name="log.maxHistory" value="15" />
<property name="log.colorPattern" value="%magenta(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%-5level) %yellow(%thread) %green(%logger) %msg%n"/>
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5level %thread %logger %msg%n"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.colorPattern}pattern>
encoder>
appender>
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/info/info.%d{yyyy-MM-dd}.logfileNamePattern>
<MaxHistory>${log.maxHistory}MaxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/error/error.%d{yyyy-MM-dd}.logfileNamePattern>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<root level="debug">
<appender-ref ref="console" />
root>
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
root>
configuration>
package springboot.demo.log.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Administrator on 2019/10/26.
* @version 1.0
*/
@Slf4j
@RestController
public class TestController {
@GetMapping("/index")
public void index() {
log.info("我记录日志了");
}
}
使用 @Slf4j 注解,然后调用log方法就可以直接生成日志文件
这个例子里我们将com.glmapper.spring.boot.controller中的日志输出到glmapper-controller.log;将com.glmapper.spring.boot.service中的日志输出到glmapper-service.log
<appender name="GLMAPPER-SERVICE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${logging.level}level>
filter>
<file>
${logging.path}/glmapper-spring-boot/glmapper-service.log
file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logging.path}/glmapper-spring-boot/glmapper-service.log.%d{yyyy-MM-dd}FileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
appender>
<appender name="GLMAPPER-CONTROLLER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${logging.level}level>
filter>
<file>
${logging.path}/glmapper-spring-boot/glmapper-controller.log
file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logging.path}/glmapper-spring-boot/glmapper-controller.log.%d{yyyy-MM-dd}FileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
appender>
<logger name="com.glmapper.spring.boot.controller" level="${logging.level}" additivity="false">
<appender-ref ref="GLMAPPER-CONTROLLER" />
<appender-ref ref="GERROR-APPENDER" />
logger>
<logger name="com.glmapper.spring.boot.service" level="${logging.level}" additivity="false">
<appender-ref ref="GLMAPPER-SERVICE" />
<appender-ref ref="GERROR-APPENDER" />
logger>
这个其实也是和上面那个差不过,只不过粒度更细一点,一般情况下比如说我们有个定时任务类需要单独来记录其日志信息,这样我们就可以考虑使用基于类维度来约束打印。
<appender name="SCHEDULERTASKLOCK-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${logging.level}level>
filter>
<file>${logging.path}/glmapper-spring-boot/scheduler-task-lock.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logging.path}/glmapper-spring-boot/scheduler-task-lock.log.%d{yyyy-MM-dd}FileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
appender>
<logger name="com.glmapper.spring.boot.task.TestLogTask" level="${logging.level}" additivity="true">
<appender-ref ref="SCHEDULERTASKLOCK-APPENDER" />
<appender-ref ref="ERROR-APPENDER" />
logger>
logger的name除了类、包等约束之外,当然还可以这样来玩。。。
private static final Logger LOGGER = LoggerFactory.getLogger(TestLogTask.class);
在getLogger中我们是将当前对象的class作为参数的,这个是为了打印时获取其全限定名的
1-2018-07-21 11:15:42.003 [pool-1-thread-1]
2-INFO
3-com.glmapper.spring.boot.task.TestLogTask -
4-com.glmapper.spring.boot.task:info
我们同样是service包下定义一个类TestLogNameServiceImpl
package com.glmapper.spring.boot.service;
@Service("testLogNameService")
public class TestLogNameServiceImpl implements TestLogNameService {
private static final Logger LOGGER = LoggerFactory.getLogger("GLMAPPER-TEST-LOG");
@Override
public void print() {
LOGGER.info("GLMAPPER-TEST-LOG:this is special logger-----info");
LOGGER.error("GLMAPPER-TEST-LOG:this is special logger-------error");
}
}
logback-spring.xml配置:
<appender name="ROOT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${logging.level}level>
filter>
<file>${logging.path}/glmapper-spring-boot/glmapper-test.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logging.path}/glmapper-spring-boot/glmapper-test.log.%d{yyyy-MM-dd}
FileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
appender>
<logger name="GLMAPPER-TEST-LOG" level="${logging.level}" additivity="true">
<appender-ref ref="ROOT-APPENDER" />
<appender-ref ref="ERROR-APPENDER" />
logger>
这里TestLogNameServiceImpl中的日志不打印到glmapper-service.log中,而是打印到glmapper-test.log中。