借用博主yutao_Struggle的https://blog.csdn.net/yutao_Struggle/article/details/80538422这篇文章来说明一下:
SpringBoot使用关于日志的starter,SpringBoot默认使用的日志框架是slf4j+logback
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
先说一下使用logback的好处: 这是没有使用logback控制台打印的日志:
springboot服务正在启动......
2019-01-27 00:24:54.194 INFO 12100 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-01-27 00:24:54.194 INFO 12100 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-01-27 00:24:54.198 INFO 12100 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
2019-01-27 00:24:54.209 INFO 12100 --- [nio-9090-exec-1] c.y.s.controller.HelloWorldController : this is a info message
2019-01-27 00:24:54.209 WARN 12100 --- [nio-9090-exec-1] c.y.s.controller.HelloWorldController : this is a warn message
2019-01-27 00:24:54.209 ERROR 12100 --- [nio-9090-exec-1] c.y.s.controller.HelloWorldController : this is a error message
使用logback:
springboot服务正在启动......
[2019-01-27 11:12:02.002] [org.apache.juli.logging.DirectJDKLog] [http-nio-9090-exec-1] [173] [INFO ] Initializing Spring DispatcherServlet 'dispatcherServlet'
[2019-01-27 11:12:02.002] [org.springframework.web.servlet.FrameworkServlet] [http-nio-9090-exec-1] [524] [INFO ] Initializing Servlet 'dispatcherServlet'
[2019-01-27 11:12:02.002] [org.springframework.web.servlet.FrameworkServlet] [http-nio-9090-exec-1] [546] [INFO ] Completed initialization in 2 ms
[2019-01-27 11:12:02.002] [com.yudainxx.springBootDemo.controller.HelloWorldController] [http-nio-9090-exec-1] [24] [INFO ] this is a info message
[2019-01-27 11:12:02.002] [com.yudainxx.springBootDemo.controller.HelloWorldController] [http-nio-9090-exec-1] [25] [WARN ] this is a warn message
[2019-01-27 11:12:02.002] [com.yudainxx.springBootDemo.controller.HelloWorldController] [http-nio-9090-exec-1] [26] [ERROR] this is a error message
以下我本地项目logback.xml的配置:
<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
<!--配置当前log路径,这样写不好管理,每次都要修改xml,写在properties文件比较好-->
<!--<property name="LOG_HOME" value="G://pdf" />-->
<!--引入properties,把LOG_HOME写在logback.properties-->
<property resource="logback.properties" />
<!--控制台日志打印方式-->
<appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MM-dd' 'HH:mm:ss.sss}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名-->
<FileNamePattern>${LOG_HOME}/${logging.file}.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志文件保留天数,超过日期删除之前的旧文件。注意,删除旧文件是,
那些为了归档而创建的目录也会被删除。-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--
日志输出格式:
%d表示日期时间,
%thread表示线程名,
%-5level:级别从左显示5个字符宽度,即INFO、WARN、ERROR等
%logger{50} 表示logger名字最长50个字符,否则按照句点分割
%msg:日志消息
%n是换行符
-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<!--日志文件最大的大小-->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!--日志文件配置info-->
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--如果只是想要 Info 级别的日志,只是过滤 info 还是会输出 Error 日志,因为 Error 的级别高,
所以我们使用下面的策略,可以避免输出 Error 的日志-->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!--过滤 Error-->
<level>ERROR</level>
<!--匹配到就禁止-->
<onMatch>DENY</onMatch>
<!--没有匹配到就允许-->
<onMismatch>ACCEPT</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间-->
<FileNamePattern>${LOG_HOME}/${logging.file}.info.%d.log</FileNamePattern>
</rollingPolicy>
<!--日志输出编码格式化-->
<encoder>
<charset>UTF-8</charset>
<pattern>%d [%thread] %-5level %logger{36} %line - %msg%n</pattern>
</encoder>
</appender>
<!--日志文件配置error-->
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!--设置日志级别,过滤掉info日志,只输入error日志-->
<level>ERROR</level>
</filter>
<encoder>
<pattern>
[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n
</pattern>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_HOME}/${logging.file}.error.%d.log</fileNamePattern>
</rollingPolicy>L
<!--日志输出编码格式化-->
<encoder>
<charset>UTF-8</charset>
<pattern>[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref = "FILE"/>
<appender-ref ref = "STDOUT"/>
<appender-ref ref = "fileInfoLog"/>
<appender-ref ref = "fileErrorLog"/>
</root>
</configuration>
logback.properties文件:
#日志名称
logging.file = mylog.log
#日志路径
LOG_HOME=G://pdf
运行项目,输入http://localhost:9090/
package com.yudainxx.springBootDemo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
private static final Logger logger = LoggerFactory.getLogger(HelloWorldController.class);
@Value("${spring.application.name:test}")
//取application.properties 属性 spring.application.name 的值,找不到的话默认值test
private String name;
@RequestMapping("/hello")
public String index() {
System.out.print("123");
return "Hello World";
}
@RequestMapping("/")
public String index1() {
logger.info("this is a info message");
logger.warn("this is a warn message");
logger.error("this is a error message");
return name;
}
}
最终日志写入本地文件:
mylog.log.error.2019-01-27.log文件只记录error级别的日志:
[2019-01-27T12:09:20.020Z] [com.yudainxx.springBootDemo.controller.HelloWorldController] [http-nio-9090-exec-1] [27] [ERROR] this is a error message
mylog.log.info.2019-01-27.log文件只记录非error级别的日志:
2019-01-27 12:09:20,646 [http-nio-9090-exec-1] INFO c.y.s.c.HelloWorldController 25 - this is a info message
2019-01-27 12:09:20,647 [http-nio-9090-exec-1] WARN c.y.s.c.HelloWorldController 26 - this is a warn message
mylog.log.2019-01-27.log文件记录所有logger日志:
2019-01-27 12:09:20.646 [http-nio-9090-exec-1] INFO c.y.springBootDemo.controller.HelloWorldController - this is a info message
2019-01-27 12:09:20.647 [http-nio-9090-exec-1] WARN c.y.springBootDemo.controller.HelloWorldController - this is a warn message
2019-01-27 12:09:20.647 [http-nio-9090-exec-1] ERROR c.y.springBootDemo.controller.HelloWorldController - this is a error message
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有