Introduction

During this exercise we will move the logging in Spring Boot from Logback (default setting) to Log4J

Background info

Some definitions
  • Know that SLF4J is the de-facto standard for generating logging with Spring Boot

  • Know that the physical logging is decoupled from SLF4J

  • Know that there are several physical logging frameworks

    • Log4J(2)

    • Logback

    • Java.util.Logging

Exercise

During this exercise we will move from the Logback (default) physical logging to Log4j (physical logging)

Follow the above and read it

There are several bugs or other things which are invalid since the time the article is written by Spring Guru but the most mental parts still hold

Steps

Amend the .gitignore to not add .log files and such to the repo
*.log
*log*
In the POM in the Spring Boot starter actuator dep disable the logback dep
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>

                        <!-- this -->
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
In the POM add the following dep
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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>
Add the file log4j2.xml to src/main/resources
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
    <Properties>
        <Property name="log-path">logs</Property>
        <Property name="archive">${log-path}/archive</Property>
    </Properties>
    <Appenders>
        <Console name="Console-Appender" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>
                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>>
            </PatternLayout>
        </Console>
        <File name="File-Appender" fileName="${log-path}/xmlfilelog.log" >
            <PatternLayout>
                <pattern>
                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
        </File>
        <RollingFile name="RollingFile-Appender"
                     fileName="${log-path}/rollingfile.log"
                     filePattern="${archive}/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="30 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console-Appender"/>
            <AppenderRef ref="File-Appender"/>
        </Root>
        <Logger name="eu.qnh.pretpark" level="debug" additivity="false">
            <AppenderRef ref="File-Appender"/>
            <AppenderRef ref="RollingFile-Appender" />
            <AppenderRef ref="Console-Appender" />
        </Logger>
    </Loggers>
</Configuration>

Restart the application

Logging should now be different and we should be using Log4J2 now

Validating your dependencies

Invoke this to see the dependencies you are using in the project, including transitive deps
$ mvn dependency:tree

$ mvn dependency:tree > dependencies.log

More info