一、前言
Logback是由log4j创始人设计的又一个开源日记组件,Logback 当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块,logback-classic是log4j的一个改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日记系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日记的功能。
二、引入依赖
引入logback所需要的依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
由于spring-boot已经默认已经集成logback,所以就不需要自己依赖注入了,直接在项目中使用就行了。
如何查看spring-boot中的logback可参照一下操作:
springboot的pom文件都会引一个parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
点进去这个parent,会有一个这个dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
再点进去:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
再进去:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
再点,这些都是原有的日志包,所以,不必再引依赖了
<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>
三、区分开发环境
环境配置,dev开发环境和部署环境连接的数据库地址、debug模式等等都是不同的,为了区分dev开发环境配置和生产环境配置,可以创建两个yml文件,什么环境使用相应的配置文件:
(1).application.yml:公共配置文件,里面可以通过
spring.profiles.active=dev来指定使用哪个配置文件
(2).application-dev.yml:开发环境配置文件
(3).application-prod.yml:生产环境配置文件
注意:
1、当公共配置文件application.yml和dev.yml(或prod.yml)同时存在同一个配置的时候,以dev.yml(prod.yml)配置文件为主,
2、当配置项只在公共application.yml文件中有的时候,以公共配置为主
3、可以把公共配置项放到application.yml中
四、配置logback-spring.xml
官方推荐使用的xml名字的格式为:logback-spring.xml而不是logback.xml,至于为什么,因为带spring后缀的可以使用这个标签。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="TRACE" />
<springProperty scope="context" name="log.path" source="spring.application.log.path" defaultValue="log"/>
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
%logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- 普通日志文件 -->
<appender name="GENERAL_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.path}/general.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${log.path}/general.log.%d{yyyy-MM-dd}.%i
</FileNamePattern>
<!--日志文件最大的大小 -->
<MaxFileSize>100MB</MaxFileSize>
<MaxHistory>10</MaxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
%logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- 错误日志文件 -->
<appender name="ERROR_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.path}/error/error.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${log.path}/error/error.log.%d{yyyy-MM-dd}.%i
</FileNamePattern>
<!--日志文件最大的大小 -->
<MaxFileSize>30MB</MaxFileSize>
<MaxHistory>10</MaxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
%logger{50} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="TRACE" />
<!-- 开发、测试环境 -->
<springProfile name="dev,test">
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="INFO" />
<logger name="io.wgame" level="DEBUG" />
</springProfile>
<!-- 生产环境 -->
<springProfile name="prod">
<logger name="org.springframework.web" level="ERROR"/>
<logger name="org.springboot.sample" level="ERROR" />
<logger name="io.wgame" level="INFO" />
</springProfile>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ERROR_FILE"/>
<appender-ref ref="GENERAL_FILE" />
</root>
</configuration>
参考博文:
https://baijiahao.baidu.com/s?id=1658220323273056065&wfr=spider&for=pc
https://www.cnblogs.com/zhangjianbing/p/8992897.html
更多内容请关注微信公众号