Writing Log Data to Syslog Using Log4j2 – 使用Log4j2将日志数据写入Syslog

最后修改: 2021年 7月 16日

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

1. Overview

1.概述

Logging is a vital component in every application. When we use a logging mechanism in our application, we can store our logs in a file or a database. Additionally, we can send the logging data to a centralized logging management application like Graylog or Syslog:

日志是每个应用程序中的一个重要组成部分。当我们在应用程序中使用日志机制时,我们可以将日志存储在一个文件或数据库中。此外,我们可以将日志数据发送到一个集中的日志管理应用程序,如GraylogSyslog

In this tutorial, we’ll describe how to send logging information to a Syslog server using Log4j2 in a Spring Boot application.

在本教程中,我们将介绍如何在Log4j2应用程序中使用Spring Boot发送日志信息到Syslog服务器。

2. Log4j2

2.ǞǞǞ

Log4j2 is the latest version of Log4j. It is a common choice for high-performance logging and is used in many production applications.

Log4j2是Log4j的最新版本。它是高性能日志的一个常见选择,并被用于许多生产应用中。

2.1. Maven Dependency

2.1.Maven的依赖性

Let’s start by adding the spring-boot-starter-log4j2 dependency to our pom.xml:

让我们先把spring-boot-starter-log4j2依赖性添加到我们的pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>2.5.2</version>
</dependency>

For configuring Log4j2 in a Spring Boot application, we’ll need to exclude the default Logback logging framework from any starter library in pom.xml. In our project, there’s only the spring-boot-starter-web starter dependency. Let’s exclude the default logging from it:

为了在Spring Boot应用程序中配置Log4j2,我们需要将默认的Logback日志框架从pom.xml的任何启动库中排除。在我们的项目中,只有spring-boot-starter-web启动依赖。让我们排除其中的默认日志。

<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>

2.2. Log4j2 Configuration

2.2.Log4j2配置

Now, we’ll create the Log4j2 configuration file. The Spring Boot project searches for either the log4j2-spring.xml or log4j2.xml files in the classpath. Let’s configure a simple log4j2-spring.xml in the resource directory:

现在,我们将创建Log4j2配置文件。Spring Boot项目会在classpath中搜索log4j2-spring.xmllog4j2.xml文件。让我们在resource目录下配置一个简单的log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%style{%date{DEFAULT}}{yellow} %highlight{%-5level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green} %message"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="ConsoleAppender"/>
        </Root>
    </Loggers>
</Configuration>

The configuration has a Console appender for displaying logging data to the console.

该配置有一个Consoleappender,用于向控制台显示日志数据。

2.3. Syslog Appender

2.3 Syslog Appender

Appenders are the main component in logging frameworks that deliver logging data to a destination. Log4j2 supports many appenders, such as the Syslog appender. Let’s update our log4j2-spring.xml file to add the Syslog appender for sending the logging data to the Syslog server:

应用者是日志框架中的主要组件,它将日志数据传递到一个目的地。Log4j2支持许多应用者,例如Syslog应用者。让我们更新我们的log4j2-spring.xml文件,添加Syslog appender,以便将日志数据发送到Syslog服务器。

<Syslog name="Syslog" format="RFC5424" host="localhost" port="514"
    protocol="UDP" appName="baeldung" facility="LOCAL0" />

The Syslog appender has many attributes:

Syslog appender有许多属性。

  • name: the name of the appender
  • format: it can be either set to BSD or RFC5424
  • host: the address of the Syslog server
  • port: the port of the Syslog server
  • protocol: whether to use TCP or UPD
  • appName: the name of the application that is logging
  • facility: the category of the message

3. Syslog Server

3.系统日志服务器

Now, let’s set up the Syslog server. In many Linux distributions, rsyslog is the main logging mechanism. It’s included in most Linux distributions, such as Ubuntu and CentOS.

现在,让我们来设置Syslog服务器。在许多Linux发行版中,rsyslog是主要的日志机制。它包含在大多数Linux发行版中,如Ubuntu和CentOS。

3.1. Syslog Configuration

3.1 Syslog配置

Our rsyslog configuration should match the Log4j2 setting. The rsyslog configuration is defined in the /etc/rsyslog.conf file. We’re using UDP and port 514 for protocol and port in Log4j2 configuration, respectively. Therefore, we’ll add, or uncomment, the following lines to rsyslog.conf file:

我们的rsyslog配置应该与Log4j2的设置相符。rsyslog配置是在/etc/rsyslog.conf文件中定义。我们在Log4j2配置中分别使用UDP和端口514作为protocolport。因此,我们将在rsyslog.conf文件中添加或取消注释以下几行。

# provides UDP syslog reception
module(load="imudp")
input(type="imudp" port="514")

In this case, we’re setting module(load=”imudp”) to load imudp module to receive Syslog messages via UDP. Then, we set the port to 514 using input configuration. The input assigns the port to the module. After that, we should restart the rsyslog server:

在这种情况下,我们设置module(load=”imudp”)来加载imudp模块以通过UDP接收Syslog消息。然后,我们使用输入配置将端口设置为514。input将端口分配给模块。之后,我们应该重新启动rsyslog服务器。

sudo service rsyslog restart

Now the configuration is ready.

现在,配置已经准备好了。

3.2. Testing

3.2.测试

Let’s create a simple Spring Boot application that logs a few messages:

让我们创建一个简单的Spring Boot应用程序,记录一些信息。

@SpringBootApplication
public class SpringBootSyslogApplication {

    private static final Logger logger = LogManager.getLogger(SpringBootSyslogApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SpringBootSyslogApplication.class, args);

        logger.debug("Debug log message");
        logger.info("Info log message");
        logger.error("Error log message");
        logger.warn("Warn log message");
        logger.fatal("Fatal log message");
        logger.trace("Trace log message");
    }
}

The logging information is stored in the /var/log/ directory. Let’s check the syslog file:

日志信息存储在/var/log/目录中。让我们检查一下syslog文件。

tail -f /var/log/syslog

When we run our Spring Boot application, we’ll see our log messages:

当我们运行我们的Spring Boot应用程序时,我们会看到我们的日志信息。

Jun 30 19:49:35 baeldung[16841] Info log message
Jun 30 19:49:35 baeldung[16841] Error log message
Jun 30 19:49:35 baeldung[16841] Warn log message
Jun 30 19:49:35 baeldung[16841] Fatal log message

4. Conclusion

4.总结

In this tutorial, we described the Syslog configuration in Log4j2 in a Spring Boot application. Also, we configured the rsyslog server as the Syslog server. As usual, the full source code can be found over on GitHub.

在本教程中,我们描述了Spring Boot应用程序中Log4j2的Syslog配置。同时,我们将rsyslog服务器配置为Syslog服务器。像往常一样,完整的源代码可以在GitHub上找到over。