SpringBoot构建应用之集成Logback
Posted pangyangqi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot构建应用之集成Logback相关的知识,希望对你有一定的参考价值。
一 依赖关系的梳理
当我们用IDEA简单的构建一个单体的应用时,所依赖的POM中其实已经有集成了logback的依赖;
我们可以打开pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
点击进入spring-boot-starter-parent 里依赖了spring-boot-dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.7.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
再进入spring-boot-dependencies 里找到spring-boot-starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.1.7.RELEASE</version> </dependency>
再进入spring-boot-starter找到spring-boot-starter-logging
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> <version>2.1.7.RELEASE</version> <scope>compile</scope> </dependency>
点击进入就能看到有依赖了logback
<dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.11.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>1.7.26</version> <scope>compile</scope> </dependency> </dependencies>
二 xml配置
首先,官方推荐使用的xml名字的格式为:logback-spring.xml而不是logback.xml,至于为什么,因为带spring后缀的可以使用<springProfile>这个标签
我们在resource的目录下新建一个 logback-spring.xml 内容如下
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <springProperty scope="context" name="logLevel" source="logging.pattern.level"/> <springProperty scope="context" name="logPath" source="logging.path"/> <springProperty scope="context" name="fileName" source="logging.file"/> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %date [%thread] %-5level %logger80 - %msg%n </pattern> </layout> </appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %date [%thread] %-5level %logger80 - %msg%n </pattern> </encoder> <!--滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!--路径--> <fileNamePattern>$logPath/$fileName-%dyyyy-MM-dd-%i.log</fileNamePattern> <maxFileSize>200MB</maxFileSize> <maxHistory>60</maxHistory> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %date [%thread] %-5level %logger80 - %msg%n </pattern> </encoder> <!--滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!--路径--> <fileNamePattern>$logPath/$fileName-%dyyyy-MM-dd-error-%i.log</fileNamePattern> <maxFileSize>200MB</maxFileSize> <maxHistory>60</maxHistory> </rollingPolicy> </appender> <!--此处是针对具体包下面配置日志级别--> <!--配置文件中设置的是项目的日志级别--> <logger name="com.pyq.blog" level="$logLevel" /> <!--自己本地开发想打印sql时打开注释 但切勿提交--> <springProfile name="dev" > <!--此处是针对所有包的日志级别--> <root level="$logLevel"> <appender-ref ref="consoleLog" /> <!-- <appender-ref ref="fileInfoLog" />--> <!--<appender-ref ref="fileErrorLog" />--> </root> </springProfile > <springProfile name="test"> <root level="$logLevel"> <!--此处是针对所有包的日志级别--> <!--<appender-ref ref="consoleLog" />--> <appender-ref ref="fileInfoLog" /> <appender-ref ref="fileErrorLog" /> </root> </springProfile> <springProfile name="prod"> <root level="$logLevel"> <!--此处是针对所有包的日志级别--> <!--<appender-ref ref="consoleLog" />--> <appender-ref ref="fileInfoLog" /> <appender-ref ref="fileErrorLog" /> </root> </springProfile> </configuration>
上面的包名自己替换
三 yml配置日志
logging: path: D:\log file: $spring.application.name pattern: level: INFO
以上是关于SpringBoot构建应用之集成Logback的主要内容,如果未能解决你的问题,请参考以下文章
2RocketMQ 源码解析之 与 Spring Boot 集成