1. Overview

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:

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

2. Log4j2

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

2.1. Maven Dependency

Let's start by adding the spring-boot-starter-log4j2 dependency to our 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:

<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

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:

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

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:

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

The Syslog appender has many attributes:

  • 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

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.

3.1. Syslog Configuration

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:

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

sudo service rsyslog restart

Now the configuration is ready.

3.2. Testing

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

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

tail -f /var/log/syslog

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

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

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.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments