Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Log4j2 with SpringBoot (configuration)

I did read other questions, also I’ve read configuration and I came up with a solution but I don’t know if it’s okay.

Problem: I have SpringBoot application and I was trying to use log4j2 as logging framework but I couldn’t configure it properly. I had correct xml configuration, configuration was on a classpath but I was able to log only these 4 log levels:
INFO, WARN, ERROR and FATAL

By log4j2 documentation, if configuration is not found, default configuration is used and that configuration display only ERROR and FATAL, but mine app was showing 4 log levels as I wrote earlier. I found that behavior bizarre so I kept reading articles and I came up with this solution.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I replaced these 3 dependencies:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2.version}</version>
</dependency>
<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j2.version}</version>
</dependency>

With these 2:

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

And it works, but it feels wrong. By excluding spring-boot-starter-logging my log4j2.xml has power again.

This is my (log4j2) XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
  <Properties>
    <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
    <Property name="APP_LOG_ROOT">c:/temp</Property>
  </Properties>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT" follow="true">
      <PatternLayout pattern="${LOG_PATTERN}" />
    </Console>
 
    <RollingFile name="appLog"
      fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
      filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${LOG_PATTERN}" />
      <Policies>
        <SizeBasedTriggeringPolicy size="19500KB" />
      </Policies>
      <DefaultRolloverStrategy max="1" />
    </RollingFile>
 
  </Appenders>
  <Loggers>
 
    <Logger name="com.howtodoinjava.app" additivity="false">
      <AppenderRef ref="appLog" />
      <AppenderRef ref="Console" />
    </Logger>
 
    <Root level="debug">
      <AppenderRef ref="Console" />
    </Root>
  </Loggers>
</Configuration>

And java class:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApp{
    private static final Logger logger = LogManager.getLogger(MyApp.class);
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
         
         logger.fatal("HoHoHo FATAL");
         logger.debug("HoHoHo DEBUG");
         logger.trace("HoHoHo TRACE");
         logger.info("HoHoHo INFO");
         logger.warn("HoHoHo WARN");
         logger.error("HoHoHo ERROR");
    }
}

So now if I set Root level="trace" in my log4j2.xml file, I will see all these HoHoHo logs in a console which is what I want. If I set Root level="error" I will see only error and fatal which is also okay. However, I noticed that by changing these levels logs, my application shows a bit different logs while starting so I’m not sure if that is okay or not.. I’m wondering if I configured everything the way it should be configured.

These 2 images are showing what is different when I change log levels between trace and error.

enter image description here
enter image description here
enter image description here

So in one example I have ~1150 lines and with other approach I have ~1200 and they’re mostly identical besides this. And as you can see, when I use trace as logging level, my application doesn’t start with Spring drawing on a first line in a console.

Sorry if post is too long or unclear, so I’ll wrap my question up once again in one sentence. Did I configure my log4j2 correctly (check 2 dependencies I’m using instead of 3 that I removed). Thanks!

>Solution :

Everything is configured correctly.

Also check out https://www.baeldung.com/spring-boot-logging#log4j2-configuration-logging

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading