Spring 日志关系 与 slf4j 默认配置及其修改

依赖关系图




依赖分析

  • Sping-boot-starter 是Spring Boot 启动器,每一个Spring boot 应用都会依赖到它
  • Sping-boot-starter 依赖 Sping-boot-starter-looging
  • Spring Boot 底层也是使用 slf4j+logback 的方式进行日志记录
  • Spring Boot 使用中间替换包把其的日志框架都替换成了slf4j;
  • 所以开发中如果要引入其他框架,则一定要把这个框架的默认日志依赖移除掉,否则会起冲突。如 Spring 框架默认用的是commons-logging;而 Spring Boot 底层用的就是Spring ,所以自己也要移除掉Spring 的日志框架:
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring‐core</artifactId>
  <exclusions>
    <exclusion>
    <groupId>commons‐logging</groupId>
    <artifactId>commons‐logging</artifactId>
  </exclusion>
</exclusion
  • 总而言之:Spring Boot 能自动适配所有的日志框架,且底层使用 slf4j+logback 的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可;
  • 理论知识可以参考《日志框架简述 与 slf4j 详解》

slf4j 默认配置

package com.lct;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CocoApplicationTests {
    /**
     * 获取日志记录器
     */
    private static Logger logger = LoggerFactory.getLogger(CocoApplicationTests.class);

    @Test
    public void contextLoads() {
        System.out.println(new Date());
        /**
         * 日志的级别 由低到高 trace(跟踪) < debug(调试) < info(信息) < warn(警告) < error(错误)
         * 可以在配置文件中调整输出的日志级别,日志就只会在这个级别及以后的高级别生效
         * Spring Boot 默认使用info级别
         */
        logger.trace("这是 trace 日志...");
        logger.debug("这是 debug 日志...");
        logger.info("这是 info 日志...");
        logger.warn("这是 warn 日志...");
        logger.error("这是 error 日志...");
    }
}
  • 控制台输出如下:
2018-07-14 14:34:46.717  INFO 9812 --- [           main] com.lct.CocoApplicationTests             : Started CocoApplicationTests in 3.1 seconds (JVM running for 4.347)
Sat Jul 14 14:34:46 CST 2018
2018-07-14 14:34:46.841  INFO 9812 --- [           main] com.lct.CocoApplicationTests             : 这是info日志...
2018-07-14 14:34:46.842  WARN 9812 --- [           main] com.lct.CocoApplicationTests             : 这是warn日志...
2018-07-14 14:34:46.842 ERROR 9812 --- [           main] com.lct.CocoApplicationTests             : 这是error日志...
2018-07-14 14:34:46.932  INFO 9812 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@4310d43: startup date [Sat Jul 14 14:34:44 CST 2018]; root of context hierarchy

slf4j 修改默认配置

修改配置

  • 同样修改Spring Boot的全局配置文件 “application.properties”或者 “application.yml”
# 表示指定包下面所有类的日志输出级别,未指定的仍然按Spring Boot的默认级别info
logging.level.com.lct=debug
# logging.file=springboot.log 表示在当前项目根目录下生成springboot.log日志文件
# logging.file=E:/springboot.log 表示在E盘下生成springboot.log日志文件
logging.file=springboot.log

# 在项目根路径下创建spring/log目录,然后使用 spring.log 作为默认日志文件
# 可以使用绝对或者相对路径
#  当 logging.path 与 logging.file 同时配置时,则以 logging.file 为准,logging.path不再生效
logging.path=spring/log

# 指定控制台输出的日志格式
# %d{yyyy-MM-dd HH:mm:ss} -- [%thread] %-5level %logger{50} %msg%n
# 1、%d 表示日期时间,
# 2、%thread 表示线程名,
# 3、%‐5level 级别从左显示5个字符宽度
# 4、%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
# 5、%msg 日志消息,
# 6、%n 换行符
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} -- [%thread] %-5level %logger{50} %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm} -- [%thread] %-5level %logger{50} %msg%n
  • 控制台输出:
2018-07-14 15:12:32 -- [main] INFO  com.lct.CocoApplicationTests Started CocoApplicationTests in 3.097 seconds (JVM running for 4.367)
Sat Jul 14 15:12:32 CST 2018
2018-07-14 15:12:32 -- [main] DEBUG com.lct.CocoApplicationTests 这是 debug 日志...
2018-07-14 15:12:32 -- [main] INFO  com.lct.CocoApplicationTests 这是 info 日志...
2018-07-14 15:12:32 -- [main] WARN  com.lct.CocoApplicationTests 这是 warn 日志...
2018-07-14 15:12:32 -- [main] ERROR com.lct.CocoApplicationTests 这是 error 日志...

  • 默认情况下,日志文件超过10M时,会新建文件进行递增,比如 spring.log、spring1.log、spring2.log.....


官方提供的LOGGING所有修改项

# LOGGING
logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback.
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup.
logging.level.*= # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`.
logging.path= # Location of the log file. For instance, `/var/log`.
logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup.
logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS # Appender pattern for log date format. Supported only with the default Logback setup.
logging.pattern.file= # Appender pattern for output to a file. Supported only with the default Logback setup.
logging.pattern.level=%5p # Appender pattern for log level. Supported only with the default Logback setup.
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.




猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/81043135