SpringBoot之Logging

简介

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 SystemCustomization
Logbacklogback-spring.xml , logback-spring.groovy , logback.xml or logback.groovy
Log4j2log4j2-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:

  1. 排除相关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>
  1. 引入SLF4J对于Log4j的适配包;
<!-- 增加SLF4J对于Log4J的是适配器依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot日志记录是指在应用程序中记录和管理日志消息的过程。Spring Boot提供了许多日志框架,包括Logback、Log4j2和Java Util Logging等。通过配置日志记录器,可以控制日志消息的输出级别、格式和目标。这些日志消息可以用于调试、故障排除和性能分析等方面。Spring Boot还提供了许多有用的工具和库,如Actuator和Sleuth,可以帮助开发人员更轻松地管理和监控应用程序的日志记录。 ### 回答2: Spring Boot是一个开源的Java框架,可以帮助开发者快速搭建基于Spring的应用程序。Logging(日志记录)是开发应用程序时一个非常重要的组成部分,它可以记录应用程序的运行状态、错误信息以及其他的重要日志信息。Spring Boot提供了一种简单且灵活的机制来配置和使用日志记录。以下是Spring Boot Logging的一些重要特点。 1. 默认日志框架:Spring Boot默认使用SLF4J作为日志记录框架,并通过Logback作为默认的实现。这意味着开发人员可以使用常见的日志记录API(如Log4j、Log4j2和java.util.logging)来记录日志。 2. 自动配置:Spring Boot自动配置了日志记录的核心组件,开发人员无需自己进行复杂的配置。通过在application.properties或application.yml文件中设置相关属性,可以轻松地定制日志记录的行为。 3. 控制台日志输出:Spring Boot默认将日志输出到控制台,可以通过控制台来查看应用程序的运行日志。可以通过设置日志级别来控制控制台日志输出的详细程度。 4. 日志文件输出:除了输出到控制台,Spring Boot还支持将日志输出到文件中。可以通过设置相关属性,将日志记录到指定文件中,并控制日志文件的大小和个数。 5. 统一的日志格式:Spring Boot使用了统一的日志格式,方便开发人员阅读和解析日志信息。可以灵活调整和定制日志格式,以满足自己的需求。 总而言之,Spring Boot Logging提供了简单、灵活且强大的日志记录机制,可以帮助开发人员快速集成和管理应用程序的日志。无论是输出到控制台还是日志文件,都能满足开发人员对日志记录的需求,并且可以根据开发环境和需求进行灵活的配置。 ### 回答3: Spring Boot提供了强大且灵活的日志记录功能,以帮助开发人员调试和监控应用程序的运行。它内置了许多与日志相关的功能和插件,可以方便地进行配置和扩展。 首先,Spring Boot使用了一种灵活的“配置即日志”的方式来管理日志记录。开发人员可以通过在application.properties或application.yml文件中设置相应的属性来配置日志的行为。例如,可以设置日志输出的级别、输出格式、输出位置等。 其次,Spring Boot使用了一种统一的日志抽象层,支持多种常见的日志框架,例如Logback、Log4j2和java.util.logging等。开发人员可以根据自己的喜好和需求选择使用其中的一种日志框架,并进行相应的配置。 此外,Spring Boot还提供了一些特殊的日志记录功能。例如,可以通过@ConfigurationProperties注解将日志配置属性与应用程序的配置文件绑定起来,实现更加灵活和动态的配置。还可以通过使用@Profile注解,根据不同的环境配置不同的日志输出。 在开发过程中,开发人员可以在代码中使用日志记录器进行日志输出。Spring Boot内置了一些常用的日志记录器,如LogFactory和LoggerFactory,提供了方便的日志记录操作。开发人员可以通过LoggerFactory.getLogger()方法获取日志记录器,并使用其提供的方法进行日志输出。 总之,Spring Boot的日志记录功能非常强大且灵活,可以满足开发人员对于监控和调试应用程序的需求。通过灵活的配置和丰富的功能,开发人员可以方便地定制化日志输出,以适应不同的应用场景和需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值