Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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:

Figures Page 5

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

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW