作者:手机用户2502910491 | 来源:互联网 | 2023-09-14 23:38
https:www.cnblogs.comflying607p7827460.html以下是springboot的一个局部依赖关系:可以看到,javautillo
https://www.cnblogs.com/flying607/p/7827460.html
以下是springboot的一个局部依赖关系:
可以看到,java util logging(jul)、common-logging(jcl)、log4j,都被桥接到了slf4j-api上,slf4j-api只提供了slf4j的接口,并无实现,图中的logback-classic则直接对slf4j进行了实现,而logback-core则支撑了logback-classic
我们可以得出springboot默认日志的最终实现者是logback,即你使用了spring-boot-starter则会引入该日志框架,同时你还可以使用common-logging,log4j,slf4j,java util logging的写法。
依赖关系分析结束,我们直接上logback的配置文件——logback.xml:
配置示例如下:
xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <property name="LOG_HOME" value="/home" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %green(%-5level) %blue(%property{PID}) --- [%-20thread] %cyan(%-70logger{50}) : %msg%npattern> encoder> appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/eureka-server.log.%d{yyyy-MM-dd}.logFileNamePattern> <MaxHistory>30MaxHistory> rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern> encoder> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MBMaxFileSize> triggeringPolicy> appender> <appender name="liuyx" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/liuyx.log.%d{yyyy-MM-dd}.logFileNamePattern> <MaxHistory>30MaxHistory> rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern> encoder> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MBMaxFileSize> triggeringPolicy> appender>
注释很明白了,把它放在maven项目的resources目录下即可生效。
2018年7月10日追加:
spring-boot中对logback做了一些加工,如果我们使用文件名logback-spring.xml,这些加工便会生效,比如,从配置文件中读取一个参数,以下是一个示例配置:
xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <springProperty scope="context" name="springAppName" source="spring.application.name"/> <property name="LOG_HOME" value="/log/${springAppName}" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %green(%-5level) %blue(%property{PID}) --- [%-20thread] %cyan(%-70logger{50}) : %msg%npattern> encoder> appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}/log-%d{yyyy-MM-dd}.%i.logfileNamePattern> <maxHistory>30maxHistory> <maxFileSize>30MBmaxFileSize> <totalSizeCap>10GBtotalSizeCap> rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%npattern> encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFOlevel> filter> appender> <appender name ="ASYNC" class= "ch.qos.logback.classic.AsyncAppender"> <queueSize>512queueSize> <appender-ref ref ="FILE"/> appender>