日志还是很重要的,发现大家都推荐logback
,觉得不错分享下
logback
库是默认内置的,不需要添加maven,下面直接使用
配置logback-spring.xml
在resources
目录新建logback-spring.xml
,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| <?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false"> <property name="LOG_PATH" value="/usr/local/log/"/> <property name="APP_NAME" value="Spring Sample"/> <contextName>${APP_NAME}</contextName> <jmxConfigurator/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <encoder> <pattern>%d [%t] %5p %c:%L - %m%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${LOG_PATH}${APP_NAME}-info.log</File> <encoder> <pattern>%d [%t] %5p %c:%L - %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <FileNamePattern>${LOG_PATH}${APP_NAME}/info/%d{yyyy-MM-dd}-%i.log.gz </FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> </appender> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${LOG_PATH}${APP_NAME}-error.log</File> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>error</level> </filter> <encoder> <pattern>%d [%t] %5p %c:%L - %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>256MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <FileNamePattern>${LOG_PATH}${APP_NAME}/error/%d{yyyy-MM-dd}-%i.log.gz </FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> </appender> <logger name="org.springframework.web" level="INFO"/> <logger name="xuk.spring.sample.rest" level="TRACE"/>
<root level="info"> <appender-ref ref="CONSOLE"/> <appender-ref ref="INFO_FILE"/> <appender-ref ref="ERROR_FILE"/> </root> </configuration>
|
应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package xuk.spring.sample.rest;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController public class HelloController { private Logger log = LoggerFactory.getLogger(HelloController.class);
@RequestMapping("/") String index() { log.debug("Hello World!"); return (new Date().toString()) + ", Hello World!"; } }
|
运行输入http://localhost:8080/
之后,在控制台及本地文件都能够看到日志打印了
1
| 2019-11-19 17:33:21,635 [http-nio-8080-exec-1] INFO xuk.spring.sample.rest.HelloController:16 - Hello World!
|
参考
https://juejin.im/post/5dbfe21cf265da4d307f162a
https://mrbird.cc/Spring-Boot-logback.html