日志门面与具体实现
slf4j(http://www.slf4j.org/)、log4j、logback是同一个人开发的
log4j2是apache开发的,只是借了log4j的名字
springboot默认使用的是:slf4j+logback
其他框架统一使用slf4j
1.将其他框架的日志jar包排除掉
2.用中间包来替换原有的日志框架
3.然后导入slf4j和具体的实现jar包
springboot实现方式
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
模式使用上面的实现日志记录,同时spring-boot-starter-logging引入了如下的包
<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.12.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.30</version>
<scope>compile</scope>
</dependency>
</dependencies>
默认使用的是logback,同时引入log4j和jul的转换jar包,使得使用log4j和jul的也是用slf4j来记录日志
假如要使用其他的日志框架,则要先排除spring-boot-starter-logging这个jar包,然后在引入其他日志框架的jar包。
logback配置文件
不同的日志框架对应的不同的配置文件名称
Logging System | Customization |
---|---|
Logback | logback-spring.xml , logback-spring.groovy , logback.xml , or logback.groovy |
Log4j2 | log4j2-spring.xml or log4j2.xml |
JDK (Java Util Logging) | logging.properties |
# 推荐使用-spring.xml的文件,例如,优先使用logback-spring.xml而不是logback.xml
When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.
logback.xml是日志框架自动加载的
logback-spring.xml文件是spring加载的
因此,使用name
属性指定哪个配置文件接受配置。
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<!--dev环境是用下面 -->
<springProfile name="dev">
<FileNamePattern>${LOG_HOME}/upmsApi-dev-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
</springProfile>
<!--非dev环境是用下面 -->
<springProfile name="!dev">
<FileNamePattern>${LOG_HOME}/upmsApi-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
</springProfile>
<!--日志文件保留天数 -->
<MaxHistory>30</MaxHistory>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
</appender>
切换日志框架
1.springboot默认使用的是slf4j+logback,现在切换为slf4j+log4j
<!-- 排除logback和log4j的转换包(因为就是用log4j了,就不用转换了) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupid>ch.qos.logback</groupid>
<artifactid>logback-classic<atifactid>
</exclusion>
<exclusion>
<groupid>org.sif4j</groupid>
<artifactid>1og4j-over-s1f4j<artifactid>
</exclusion>
</exclusions>
</dependency>
<!-- slf4j-1og4j12依赖log4j,所以不用单独引入log4j -->
<dependency>
<groupid>org.s1f4j<groupid>
<artifactid>slf4j-1og4j12</artifactid>
</dependency>
引入log4j的配置文件
2.切换为slf4j+log4j2
<!-- 排除logback和log4j的转换包(因为就是用log4j了,就不用转换了) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-logging<atifactid>
</exclusion>
</exclusions>
</dependency>
<!-- 引入 -->
<dependency>
<groupid>org.springframework.boot<groupid>
<artifactid>spring-boot-starter-log4j2</artifactid>
</dependency>
引入log4j2的配置文件