目录 [TOC]
参考 https://blog.csdn.net/lingerlan510/article/details/126387015
https://blog.csdn.net/Jackyyl729/article/details/111186055
三种日志框架之间的关系 最先有Log4j,然后因为Log4j有很大的性能问题因此该作者又重写了一个Logback,并且抽象出一个日志门面slf4j。由于之前Log4j的问世,Apache公司就借鉴了Log4j的部分原理,自己重新写了一个日志框架Log4j2并且也实现了slf4j日志门面。
Logback SpringBoot种采用Logback作为默认的日志框架
在SpringBoot中,底层是Spring框架,Spring框架默认使用JCL,而SpringBoot默认集成的日志框架需要的是SLF4j+Logback组合。因为spring-boot-starter-logging是Logback的日志实现,而SpringBoot启动项spring-boot-starter又依赖了spring-boot-starter-logging,所以Spring Boot就默认集成了Logback,因此SpringBoot默认集成了Logback。
默认情况下,SpringBoot内部使用logback作为系统日志实现的框架,将日志输出到控制台,不会写到日志文件。如果在application.properties或application.yml配置,这样只能配置简单的场景,保存路径、日志格式等。复杂的场景(区分 info 和 error 的日志、每天产生一个日志文件等)满足不了,只能自定义配置文件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 89 90 91 92 93 <?xml version="1.0" encoding="UTF-8" ?> <configuration > <property name ="log.path" value ="/home/ruoyi/logs" /> <property name ="log.pattern" value ="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" /> <appender name ="console" class ="ch.qos.logback.core.ConsoleAppender" > <encoder > <pattern > ${log.pattern}</pattern > </encoder > </appender > <appender name ="file_info" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <file > ${log.path}/sys-info.log</file > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern > ${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern > <maxHistory > 60</maxHistory > </rollingPolicy > <encoder > <pattern > ${log.pattern}</pattern > </encoder > <filter class ="ch.qos.logback.classic.filter.LevelFilter" > <level > INFO</level > <onMatch > ACCEPT</onMatch > <onMismatch > DENY</onMismatch > </filter > </appender > <appender name ="file_error" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <file > ${log.path}/sys-error.log</file > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern > ${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern > <maxHistory > 60</maxHistory > </rollingPolicy > <encoder > <pattern > ${log.pattern}</pattern > </encoder > <filter class ="ch.qos.logback.classic.filter.LevelFilter" > <level > ERROR</level > <onMatch > ACCEPT</onMatch > <onMismatch > DENY</onMismatch > </filter > </appender > <appender name ="sys-user" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <file > ${log.path}/sys-user.log</file > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern > ${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern > <maxHistory > 60</maxHistory > </rollingPolicy > <encoder > <pattern > ${log.pattern}</pattern > </encoder > </appender > <logger name ="com.ruoyi" level ="info" /> <logger name ="org.springframework" level ="warn" /> <root level ="info" > <appender-ref ref ="console" /> </root > <root level ="info" > <appender-ref ref ="file_info" /> <appender-ref ref ="file_error" /> </root > <logger name ="sys-user" level ="info" > <appender-ref ref ="sys-user" /> </logger > </configuration >
Log4j2 Log4j