简介
SpringBoot官方文档说明,SpringBoot内部使用Commons Logging作为日志记录门面,但是当使用SpringBoot场景启动器时默认是使用的SLF4J+Logback进行日志记录。
SpringBoot采用日志门面+日志实现框架的优势,本文不在过多描述,可以参考我的其他相关博文。Java系统中常用日志框架
相关依赖
SpringBoot中所有starter都继承了spring-boot-starters,而在其pom中添加了对于spring-boot-starter-logging的引用,如下:
默认配置
在SpringBoot项目中,如果确实Log框架相关配置文件,那么就会采用默认配置:
其相关默认配置项定义:
org.springframework.boot.context.logging.LoggingApplicationListener.java
public class LoggingApplicationListener implements GenericApplicationListener {
private static final ConfigurationPropertyName LOGGING_LEVEL = ConfigurationPropertyName
.of("logging.level");
private static final ConfigurationPropertyName LOGGING_GROUP = ConfigurationPropertyName
.of("logging.group");
private static final Bindable<Map<String, String>> STRING_STRING_MAP = Bindable
.mapOf(String.class, String.class);
private static final Bindable<Map<String, String[]>> STRING_STRINGS_MAP = Bindable
.mapOf(String.class, String[].class);
/**
* The default order for the LoggingApplicationListener.
*/
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 20;
/**
* The name of the Spring property that contains a reference to the logging
* configuration to load.
*/
public static final String CONFIG_PROPERTY = "logging.config";
/**
* The name of the Spring property that controls the registration of a shutdown hook
* to shut down the logging system when the JVM exits.
* @see LoggingSystem#getShutdownHandler
*/
public static final String REGISTER_SHUTDOWN_HOOK_PROPERTY = "logging.register-shutdown-hook";
/**
* The name of the {@link LoggingSystem} bean.
*/
public static final String LOGGING_SYSTEM_BEAN_NAME = "springBootLoggingSystem";
org.springframework.boot.logging.LoggingSystemProperties
public class LoggingSystemProperties {
/**
* The name of the System property that contains the process ID.
*/
public static final String PID_KEY = "PID";
/**
* The name of the System property that contains the exception conversion word.
*/
public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
/**
* The name of the System property that contains the log file.
*/
public static final String LOG_FILE = "LOG_FILE";
/**
* The name of the System property that contains the log path.
*/
public static final String LOG_PATH = "LOG_PATH";
/**
* The name of the System property that contains the console log pattern.
*/
public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
/**
* The name of the System property that contains the file log pattern.
*/
public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN";
/**
* The name of the System property that contains the file log max history.
*/
public static final String FILE_MAX_HISTORY = "LOG_FILE_MAX_HISTORY";
/**
* The name of the System property that contains the file log max size.
*/
public static final String FILE_MAX_SIZE = "LOG_FILE_MAX_SIZE";
/**
* The name of the System property that contains the log level pattern.
*/
public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN";
/**
* The name of the System property that contains the log date-format pattern.
*/
public static final String LOG_DATEFORMAT_PATTERN = "LOG_DATEFORMAT_PATTERN";
我们可以在配置文件中对这些默认配置进行修改:
属性名 | 作用 | 备注 |
---|---|---|
logging.file | 指定日志文件名称 | 至指定文件名称,生成在当前项目路径下;可以指定路径+名称,如:F:\logs\test.log |
logging.path | 指定日志文件生成文件夹 | 默认在该配置路径下生成spring.log |
logging.level | 配置根目录日志输出级别 | 可以指定根目录、指定框架等输出级别 |
logging.group | 定义多出包进行分组,从而配置logging.level统一管理 | 此用发似乎在YML配置文件中有问题,有成功的小伙伴请告诉我下。 |
更过请查看官方文档。
注意:logging.file和logging.path二者不可共用,否则以logging.file为准。
自定义配置
除了这些我们可以通过配置文件进行配置,SpringBoot对于各框架的配置文件:
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 |
通过上表我们可以看到,SpringBoot对于各个日志框架的配置文件名有了一定的扩展支持,增加-spring,根据官方文档描述,推荐使用-spring结尾的配置文件,因为如果使用原生文件名,那么该配置文件会首先被日志框架本身识别并加载,无法使用SpringBoot对其的一些扩展功能。
如对Logback的扩展springProfile标签,这样可以针对不同的运行环境进行针对性的配置:
<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>
切换日志框架
我之前的文章讲过Logback的优势,但是如果我们需要切换日志框架的话怎么做呢?很简单,我们只需要排除对于Logback的依赖,增加对新的日志框架的依赖即可。例如我要将Logback切换为Log4j:
- 排除相关starter中的Logback依赖以及Log4J替换包的依赖;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除对于Logback的依赖-->
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<!-- 排除对于Log4j的替换包依赖 -->
<exclusion>
<artifactId>log4j-to-slf4j</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
- 引入SLF4J对于Log4j的适配包;
<!-- 增加SLF4J对于Log4J的是适配器依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>