Logging in Spring Boot – 在Spring Boot中进行日志记录

最后修改: 2018年 7月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’re going to explore the main logging options available in Spring Boot.

在这个简短的教程中,我们将探讨Spring Boot中可用的主要日志选项。

Deeper information about Logback is available in A Guide to Logback, while Log4j2 is introduced in Intro to Log4j2 – Appenders, Layouts and Filters.

关于Logback的更深层次的信息可在Logback指南中找到,而Log4j2则在Intro to Log4j2 – Appenders, Layouts and Filters中介绍。

2. Initial Setup

2.初始设置

Let’s first create a Spring Boot module. The recommended way to do so is using Spring Initializr, which we cover in our Spring Boot Tutorial.

让我们首先创建一个Spring Boot模块。推荐的方法是使用Spring Initializr,我们在Spring Boot教程中介绍了该方法。

Now let’s create our only class file, LoggingController:

现在让我们创建我们唯一的类文件,LoggingController

@RestController
public class LoggingController {

    Logger logger = LoggerFactory.getLogger(LoggingController.class);

    @RequestMapping("/")
    public String index() {
        logger.trace("A TRACE Message");
        logger.debug("A DEBUG Message");
        logger.info("An INFO Message");
        logger.warn("A WARN Message");
        logger.error("An ERROR Message");

        return "Howdy! Check out the Logs to see the output...";
    }
}

Once we’ve loaded the web application, we’ll be able to trigger those logging lines by simply visiting http://localhost:8080/.

一旦我们加载了网络应用,我们将能够通过简单地访问http://localhost:8080/来触发这些日志行。

3. Zero Configuration Logging

3.零配置记录

Spring Boot is a very helpful framework. It allows us to forget about the majority of the configuration settings, many of which it opinionatedly auto-tunes.

Spring Boot是一个非常有用的框架。它允许我们忘记大部分的配置设置,其中许多配置是它有主见地自动调整的。

In the case of logging, the only mandatory dependency is Apache Commons Logging.

在日志方面,唯一必须依赖的是Apache Commons Logging.

We need to import it only when using Spring 4.x (Spring Boot 1.x) since it’s provided by Spring Framework’s spring-jcl module in Spring 5 (Spring Boot 2.x).

我们只有在使用Spring 4.x(Spring Boot 1.x)时才需要导入它,因为在Spring 5(Spring Boot 2.x)中,它是由Spring Framework的spring-jcl模块提供。

We shouldn’t worry about importing spring-jcl at all if we’re using a Spring Boot Starter (which we almost always are). That’s because every starter, like our spring-boot-starter-web, depends on spring-boot-starter-logging, which already pulls in spring-jcl for us.

如果我们使用的是Spring Boot启动器,我们根本不需要担心导入spring-jcl(我们几乎总是这样)。这是因为每个启动器,如我们的spring-boot-starter-web,都依赖于spring-boot-starter-logging,它已经为我们拉入了spring-jcl

3.1. Default Logback Logging

3.1.默认的Logback日志记录

When using starters, Logback is used for logging by default.

当使用启动器时,默认使用Logback进行日志记录。

Spring Boot preconfigures it with patterns and ANSI colors to make the standard output more readable.

Spring Boot用模式和ANSI颜色对其进行了预配置,以使标准输出更加可读。

Let’s now run the application and visit the http://localhost:8080/ page, and see what happens in the console:

现在让我们运行应用程序并访问http://localhost:8080/页面,看看控制台中发生了什么。

logback default logging

As we can see, the default logging level of the Logger is preset to INFO, meaning that TRACE and DEBUG messages are not visible.

正如我们所看到的,记录器的默认日志级别被预设为INFO,这意味着TRACEDEBUG信息不可见。

In order to activate them without changing the configuration, we can pass the –debug or –trace arguments on the command line:

为了在不改变配置的情况下激活它们,我们可以在命令行上传递-debug-trace参数

java -jar target/spring-boot-logging-0.0.1-SNAPSHOT.jar --trace

3.2. Log Levels

3.2 日志级别

Spring Boot also gives us access to a more fine-grained log level setting via environment variables. There are several ways we can accomplish this.

Spring Boot还让我们可以通过环境变量获得更精细的日志级别设置。我们有几种方法可以实现这一点。

First, we can set our logging level within our VM Options:

首先,我们可以在虚拟机选项中设置我们的日志记录级别。

-Dlogging.level.org.springframework=TRACE 
-Dlogging.level.com.baeldung=TRACE

Alternatively, if we’re using Maven, we can define our log settings via the command line:

另外,如果我们使用Maven,我们可以通过命令行定义我们的日志设置

mvn spring-boot:run 
  -Dspring-boot.run.arguments=--logging.level.org.springframework=TRACE,--logging.level.com.baeldung=TRACE

When working with Gradle, we can pass log settings through the command line. This will require setting the bootRun task.

在使用Gradle工作时,我们可以通过命令行传递日志设置。这将需要设置bootRun任务

Once that’s done, we run the application:

一旦完成,我们就运行该应用程序。

./gradlew bootRun -Pargs=--logging.level.org.springframework=TRACE,--logging.level.com.baeldung=TRACE

If we want to change the verbosity permanently, we can do so in the application.properties file as described here:

如果我们想永久地改变口令,我们可以在application.properties文件中这样做,如这里所述。

logging.level.root=WARN
logging.level.com.baeldung=TRACE

Finally, we can change the logging level permanently by using our logging framework configuration file.

最后,我们可以通过使用我们的日志框架配置文件永久性地改变日志级别。

We mentioned that Spring Boot Starter uses Logback by default. Let’s see how to define a fragment of a Logback configuration file in which we set the level for two separate packages:

我们提到,Spring Boot Starter默认使用Logback。让我们看看如何定义Logback配置文件的一个片段,在该文件中,我们为两个独立的包设置级别。

<logger name="org.springframework" level="INFO" />
<logger name="com.baeldung" level="INFO" />

Remember that if the log level for a package is defined multiple times using the different options mentioned above, but with different log levels, the lowest level will be used.

请记住,如果一个软件包的日志级别被多次定义,使用上述不同的选项,但具有不同的日志级别,将使用最低的级别

So, if we set the logging levels using Logback, Spring Boot, and environment variables at the same time, the log level will be TRACE, as it is the lowest among the requested levels.

因此,如果我们同时使用Logback、Spring Boot和环境变量来设置日志级别,日志级别将是TRACE,因为它是所要求的级别中最低的。

4. Logback Configuration Logging

4.Logback配置日志

Even though the default configuration is useful (for example, to get started in zero time during POCs or quick experiments), it’s most likely not enough for our daily needs.

即使默认配置是有用的(例如,在POCs或快速实验期间零时间开始),但它很可能不足以满足我们的日常需求。

Let’s see how to include a Logback configuration with a different color and logging pattern, with separate specifications for console and file output, and with a decent rolling policy to avoid generating huge log files.

让我们看看如何包含一个具有不同颜色和日志模式的Logback配置,为consolefile输出提供单独的规范,以及一个体面的rolling policy以避免产生巨大的日志文件。

First, we should find a solution that allows for handling our logging settings alone instead of polluting application.properties, which is commonly used for many other application settings.

首先,我们应该找到一个解决方案,允许单独处理我们的日志设置,而不是污染application.properties,,后者通常用于许多其他应用程序设置。

When a file in the classpath has one of the following names, Spring Boot will automatically load it over the default configuration:

当classpath中的文件具有以下名称之一时,Spring Boot将自动加载它,而不是默认配置。

  • logback-spring.xml
  • logback.xml
  • logback-spring.groovy
  • logback.groovy

Spring recommends using the -spring variant over the plain ones whenever possible, as described here.

Spring建议尽可能使用-spring变体而不是普通的,如这里所述。

Let’s write a simple logback-spring.xml:

让我们写一个简单的logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="LOGS" value="./logs" />

    <appender name="Console"
        class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/spring-boot-logger.log</file>
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    
    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <!-- LOG "com.baeldung*" at TRACE level -->
    <logger name="com.baeldung" level="trace" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

</configuration>

And when we run the application, here’s the output:

当我们运行该应用程序时,这里是输出结果。

 

logback custom logging

As we can see, it now logs TRACE and DEBUG messages, and the overall console pattern is both textually and chromatically different than before.

我们可以看到,它现在记录了TRACEDEBUG信息,而且整个控制台模式在文本和色度上都与以前不同。

It also now logs on a file in a /logs folder created under the current path and archives it through a rolling policy.

它现在还在当前路径下创建的/logs文件夹中记录一个文件,并通过滚动策略将其存档。

5. Log4j2 Configuration Logging

5.Log4j2配置日志

While Apache Commons Logging is at the core, and Logback is the reference implementation provided, all the routings to the other logging libraries are already included to make it easy to switch to them.

虽然Apache Commons Logging是核心,Logback是提供的参考实现,但所有到其他日志库的路由都已经包括在内,以便于切换到它们。

In order to use any logging library other than Logback, though, we need to exclude it from our dependencies.

为了使用除Logback以外的任何日志库,我们需要将其从我们的依赖中排除。

For every starter like this one (it’s the only one in our example, but we could have many of them):

对于每一个像这样的启动器(这是我们例子中唯一的一个,但我们可以有许多这样的启动器)。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

we need to turn it into a skinny version, and (only once) add our alternative library, here through a starter itself:

我们需要把它变成一个瘦小的版本,并(只需一次)加入我们的替代库,这里通过一个启动器本身。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

At this point, we need to place in the classpath a file named one of the following:

在这一点上,我们需要在classpath中放置一个名为下列之一的文件。

  • log4j2-spring.xml
  • log4j2.xml

We’ll print through Log4j2 (over SLF4J) without further modifications.

我们将通过Log4j2(在SLF4J之上)进行打印,无需进一步修改。

Let’s write a simple log4j2-spring.xml:

让我们写一个简单的log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
        </Console>

        <RollingFile name="RollingFile"
            fileName="./logs/spring-boot-logger-log4j2.log"
            filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <!-- rollover on startup, daily and when the file reaches 
                    10 MegaBytes -->
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy
                    size="10 MB" />
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <!-- LOG everything at INFO level -->
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFile" />
        </Root>

        <!-- LOG "com.baeldung*" at TRACE level -->
        <Logger name="com.baeldung" level="trace"></Logger>
    </Loggers>

</Configuration>

And when we run the application, here’s the output:

当我们运行该应用程序时,这里是输出结果。

 

log4j2 custom logging

As we can see, the output is quite different from the Logback one — a proof that we’re fully using Log4j2 now.

我们可以看到,输出与Logback的输出有很大的不同–这证明我们现在完全使用了Log4j2。

In addition to the XML configuration, Log4j2 allows us to use also a YAML or JSON configuration, described here.

除了XML配置外,Log4j2还允许我们使用YAML或JSON配置,在这里描述

6. Log4j2 Without SLF4J

6.没有SLF4J的Log4j2

We can also use Log4j2 natively, without passing through SLF4J.

我们也可以直接使用Log4j2,而不通过SLF4J。

In order to do that, we simply use the native classes:

为了做到这一点,我们只需使用本地类。

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
// [...]
Logger logger = LogManager.getLogger(LoggingController.class);

We don’t need to perform any other modification to the standard Log4j2 Spring Boot configuration.

我们不需要对标准的Log4j2 Spring Boot配置进行任何其他修改。

We can now exploit the brand-new features of Log4j2 without getting stuck with the old SLF4J interface. But we’re also tied to this implementation, and we’ll need to rewrite our code when switching to another logging framework.

我们现在可以利用Log4j2的全新特性,而不必拘泥于旧的SLF4J接口。但我们也被这个实现束缚住了,当切换到另一个日志框架时,我们需要重写我们的代码。

7. Logging With Lombok

7.与Lombok一起伐木

In the examples we’ve seen so far, we’ve had to declare an instance of a logger from our logging framework.

在我们到目前为止看到的例子中,我们不得不从我们的日志框架中声明一个日志器的实例

This boilerplate code can be annoying. We can avoid it using various annotations introduced by Lombok.

这种模板代码可能会很烦人。我们可以使用Lombok引入的各种注解来避免它。

We’ll first need to add the Lombok dependency in our build script to work with it:

我们首先需要在我们的构建脚本中添加Lombok的依赖关系,以便与它一起工作。

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
    <scope>provided</scope>
</dependency>

7.1. @Slf4j and @CommonsLog

7.1.@Slf4j@CommonsLog

SLF4J and Apache Commons Logging APIs allow us the flexibility to change our logging framework with no impact on our code.

SLF4J和Apache Commons Logging APIs使我们能够灵活地改变我们的日志框架,而不影响我们的代码。

And we can use Lombok’s @Slf4j and @CommonsLog annotations to add the right logger instance into our class: org.slf4j.Logger for SLF4J and org.apache.commons.logging.Log for Apache Commons Logging.

而我们可以使用Lombok的@Slf4j@CommonsLog注解来在我们的类中添加正确的日志器实例。org.slf4j.Logger用于SLF4J,org.apache.commons.logging.Log用于Apache Commons Logging。

To see these annotations in action, let’s create a class similar to LoggingController but without a logger instance. We name it as LombokLoggingController and annotate it with @Slf4j:

为了看到这些注解的作用,让我们创建一个类似于LoggingController的类,但没有记录器实例。我们把它命名为LombokLoggingController,并用@Slf4j来注解它。

@RestController
@Slf4j
public class LombokLoggingController {
 
    @RequestMapping("/lombok")
    public String index() {
        log.trace("A TRACE Message");
        log.debug("A DEBUG Message");
        log.info("An INFO Message");
        log.warn("A WARN Message");
        log.error("An ERROR Message");
 
        return "Howdy! Check out the Logs to see the output...";
    }
}

Note that we’ve adjusted the snippet just a bit, using log as our logger instance. This is because adding the annotation @Slf4j automatically adds a field named log.

请注意,我们稍微调整了一下这个片段,使用log作为我们的记录器实例。这是因为添加注解@Slf4j会自动添加一个名为log的域。

With Zero-Configuration Logging, the application will use underlying logging implementation Logback for logging. Similarly, Log4j2 implementation is used for logging with Log4j2-Configuration Logging.

使用Zero-Configuration Logging应用程序将使用底层日志实现Logback进行日志记录。同样地,使用Log4j2-Configuration Logging时,Log4j2的实现也被用于记录。

We get the same behavior when we replace the annotation @Slf4j with @CommonsLog.

当我们将注解@Slf4j替换为@CommonsLog.时,我们得到了同样的行为。

7.2. @Log4j2

7.2. @Log4j2.

We can use the annotation @Log4j2 to use Log4j2 directly. So, we make a simple change to LombokLoggingController to use @Log4j2 instead of @Slf4j or @CommonsLog:

我们可以使用注解@Log4j2来直接使用Log4j2。因此,我们对LombokLoggingController做一个简单的修改,使用@Log4j2而不是@Slf4j@CommonsLog

@RestController
@Log4j2
public class LombokLoggingController {
 
    @RequestMapping("/lombok")
    public String index() {
        log.trace("A TRACE Message");
        log.debug("A DEBUG Message");
        log.info("An INFO Message");
        log.warn("A WARN Message");
        log.error("An ERROR Message");
 
        return "Howdy! Check out the Logs to see the output...";
    }
}   

Other than logging, there are other annotations from Lombok that help in keeping our code clean and tidy. More information about them is available in Introduction to Project Lombok, and we also have a tutorial on Setting Up Lombok With Eclipse and IntelliJ.

除了日志之外,还有来自Lombok的其他注释,它们有助于保持我们的代码干净和整洁。关于它们的更多信息可在Project Lombok简介中找到,我们也有一个通过Eclipse和IntelliJ设置Lombok的教程

8. Beware of Java Util Logging

8.警惕Java Util日志

Spring Boot also supports JDK logging, through the logging.properties configuration file.

Spring Boot还支持JDK日志,通过logging.properties配置文件。

There are cases when it’s not a good idea to use it, though. From the documentation:

不过,在有些情况下,使用它并不是一个好主意。来自文档

There are known classloading issues with Java Util Logging that cause problems when running from an ‘executable jar’. We recommend that you avoid it when running from an ‘executable jar’ if at all possible.

当从 “可执行的jar “中运行时,Java Util Logging有一些已知的类加载问题,会导致问题。如果可能的话,我们建议你在从 “可执行的jar “中运行时避免它。

It’s also a good practice when using Spring 4 to manually exclude commons-logging in pom.xml, to avoid potential clashes between the logging libraries. Spring 5 instead handles it automatically, so we don’t need to do anything when using Spring Boot 2.

在使用Spring 4时,在pom.xml中手动排除commons-logging也是一个好的做法,以避免日志库之间的潜在冲突。而Spring 5会自动处理,所以我们在使用Spring Boot 2时不需要做任何事情。

9. JANSI on Windows

9.Windows上的JANSI

While Unix-based operating systems such as Linux and Mac OS X support ANSI color codes by default, on a Windows console, everything will be sadly monochromatic.

虽然基于Unix的操作系统,如Linux和Mac OS X,默认支持ANSI颜色代码,但在Windows控制台,一切都将是可悲的单色的。

Windows can obtain ANSI colors through a library called JANSI.

Windows可以通过一个名为JANSI的库获得ANSI颜色。

We should pay attention to the possible class loading drawbacks, though.

尽管如此,我们应该注意可能出现的类加载缺点。

We must import and explicitly activate it in the configuration as follows:

我们必须导入并在配置中明确激活它,如下所示。

Logback:

Logback

<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>true</withJansi>
        <encoder>
            <pattern>[%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
        </encoder>
    </appender>
    <!-- more stuff -->
</configuration>

Log4j2:

Log4j2

ANSI escape sequences are supported natively on many platforms but are not by default on Windows. To enable ANSI support, add the Jansi jar to our application and set property log4j.skipJansi to false. This allows Log4j to use Jansi to add ANSI escape codes when writing to the console.

许多平台上都支持ANSI转义序列,但在Windows上默认不支持。要启用ANSI支持,请将Jansi jar添加到我们的应用程序中,并将属性log4j.skipJansi设置为false这允许Log4j在向控制台写入时使用Jansi来添加ANSI转码。

Note: Prior to Log4j 2.10, Jansi was enabled by default. The fact that Jansi requires native code means that Jansi can only be loaded by a single class loader. For web applications, this means the Jansi jar has to be in the web container’s classpath. To avoid causing problems for web applications, Log4j no longer automatically tries to load Jansi without explicit configuration from Log4j 2.10 onward.

注意:在Log4j 2.10之前,Jansi是默认启用的。Jansi需要本地代码,这意味着Jansi只能由一个类加载器加载。对于Web应用程序,这意味着Jansi jar必须在Web容器的classpath中。为了避免给Web应用程序带来问题,从Log4j 2.10开始,Log4j不再自动尝试加载Jansi而不进行明确的配置。

It’s also worth noting:

还值得注意的是。

  • The layout documentation page contains useful Log4j2 JANSI informations in the highlight{pattern}{style} section.
  • While JANSI can color the output, Spring Boot’s Banner (native or customized through the banner.txt file) will stay monochromatic.

10. Conclusion

10.结论

We’ve seen the main ways to interface with the major logging frameworks from within a Spring Boot project.

我们已经看到了在Spring Boot项目中与主要日志框架对接的主要方式。

We also explored the main advantages and pitfalls of each solution.

我们还探讨了每种解决方案的主要优势和隐患。

As always, the full source code is available over on GitHub.

一如既往,完整的源代码可在GitHub上获得