• SpringBoot中的日志使用


    springboot框架在企业中的使用越来越普遍,springboot日志也是开发中常用的日志系统。springboot默认就是使用SLF4J作为日志门面,logback作为日志实现来记录日志。

    1、SpringBoot中的日志设计

    springboot中的日志

    1. <dependency>
    2. <artifactId>spring-boot-starter-loggingartifactId>
    3. <groupId>org.springframework.bootgroupId>
    4. dependency>

    依赖关系图:

    总结: 

    1. springboot 底层默认使用logback作为日志实现。
    2. 使用了SLF4J作为日志门面
    3. 将JUL也转换成slf4j
    4. 也可以使用log4j2作为日志门面,但是最终也是通过slf4j调用logback

    2、SpringBoot日志使用

    在springboot中测试打印日志

    1. @SpringBootTest
    2. class SpringbootLogApplicationTests {
    3. // 声明日志记录器对象
    4. public static final Logger LOGGER = LoggerFactory.getLogger(SpringbootLogApplicationTests.class);
    5. @Test
    6. void contextLoads() {
    7. // 打印日志信息
    8. LOGGER.error("error");
    9. LOGGER.warn("warn");
    10. LOGGER.info("info"); // 默认日志级别
    11. LOGGER.debug("debug");
    12. LOGGER.trace("trace");
    13. // 使用log4j2 使用桥接器切换为 slf4j 门面和 logback 日志实现
    14. org.apache.logging.log4j.Logger logger = LogManager.getLogger(SpringbootLogApplicationTests.class);
    15. logger.info("log4j2 info");
    16. }
    17. }

    修改默认日志配置(在application.properties中配置)

    1. # 指定自定义 logger 对象日志级别
    2. logging.level.com.itheima=trace
    3. # 指定控制台输出消息格式
    4. logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg %n
    5. # 指定存放日志文件的具体路径,这个已过时了
    6. # logging.file=/logs/springboot.log
    7. # 指定日志文件存放的目录,默认的文件名 spring.log
    8. # 这个不能与上面file同时使用
    9. logging.file.path=/logs/springboot/
    10. # 指定日志文件消息格式
    11. logging.pattern.file=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg %n
    12. # 指定项目使用的具体环境
    13. spring.profiles.active=pro

    指定配置

            给类路径下放上每个日志框架自己的配置文件;SpringBoot就不使用默认配置的了

    日志框架配置文件
    Logbacklogback-spring.xml , logback.xml
    Log4j2log4j2-spring.xml , log4j2.xml
    JULlogging.properties

    logback.xml:直接就被日志框架识别了

    使用SpringBoot解析日志配置

            logback-spring.xml:由SpringBoot解析日志配置

    1. <configuration>
    2. <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] -------- %m %n">property>
    3. <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    4. <target>System.errtarget>
    5. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    6. <springProfile name="dev">
    7. <pattern>${pattern}pattern>
    8. springProfile>
    9. <springProfile name="pro">
    10. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] xxxxxxxx %m %npattern>
    11. springProfile>
    12. encoder>
    13. appender>
    14. <logger name="com.itheima" level="info" additivity="false">
    15. <appender-ref ref="console"/>
    16. logger>
    17. configuration>

    application.properties,配置指定那种生产环境

    1. # 指定环境为开发环境,这时候我们在xml定义的日志格式则会启动开发环境定义
    2. spring.profiles.active=dev

    将日志切换为log4j2

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-webartifactId>
    4. <exclusions>
    5. <exclusion>
    6. <artifactId>spring-boot-starter-loggingartifactId>
    7. <groupId>org.springframework.bootgroupId>
    8. exclusion>
    9. exclusions>
    10. dependency>
    11. <dependency>
    12. <groupId>org.springframework.bootgroupId>
    13. <artifactId>spring-boot-starter-log4j2artifactId>
    14. dependency>
  • 相关阅读:
    公共方法~~
    这是一个很废的人的比赛经历
    redis 哨兵,高可用的执行者
    报错The chromedriver version cannot be discovered以及下载chromedriver.exe和查看其版本的命令
    Python基于OpenCV监控老鼠蟑螂检测系统[完整源码&部署教程]
    English语法_副词 - ever / once
    阴影进阶,实现更加的立体的阴影效果!
    深度学习100例 | 第31天-卷积神经网络(DenseNet)识别生活物品
    电脑重装系统c盘如何备份资料
    笔试强训未触及题目(个人向)
  • 原文地址:https://blog.csdn.net/weixin_52851967/article/details/126082422