0

I am trying to set log4j2 to write all logs to a specific file. I've tried various things I found on the internet but still the logs only show up in the console.

I am using IntelliJ IDEA CE. Spring Boot version is 3.1.0. Java version is 17. Maven version is 4.0.0.

My pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>...</name>
    <description>...</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.11.5</version>
        </dependency>

        <!-- Spring WebSocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>3.1.0</version>
        </dependency>

        <!-- JSON support -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.11.5</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.11.5</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

The log4j2.xml located in src/main/resources/log4j2.xml

<Configuration status="info">
    <Appenders>
        <File name="FILE" fileName="app.log" immediateFlush="false" append="true">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="org.hibernate.SQL" level="DEBUG">
            <AppenderRef ref="FILE"/>
        </Logger>
        <Logger name="org.hibernate.type.descriptor.sql" level="TRACE">
            <AppenderRef ref="FILE"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="FILE"/>
        </Root>
    </Loggers>
</Configuration>

I tried the following things:

  • I added exclusions for logback-classic in spring-boot-starter-security and spring-boot-starter-web
  • I removed unused Logging Configurations
  • I verified Log4j2 Configuration
  • Log4j2 Internal Status Logger
  • There is no application's logs and console output for any error messages or warnings related to logging
  • I tried a minimal metup
  • I added LogManager.getContext(false); in public static void main(String[] args)

Can someone help me solve this problem? Thanks

akicay
  • 31
  • 5

2 Answers2

0

Does Log file (in your case app.log) exit ?

In case not, on the configuration file in Appender tag make the property append to false, see more in here.

<Appenders>
    <File name="FILE" fileName="app.log" immediateFlush="false" append="false">
        <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</Appenders>

Are you getting the proper logger from LoggerFactory?

Make sure you getting logger by the name you crated the configuration.

Logger logger = LoggerFactory.getLogger("FILE");
Lunatic
  • 1,519
  • 8
  • 24
0

Excluding Logback is not enough. The spring-boot-starter-logging takes care of connecting all major logging APIs to Logback (cf. What is Spring Boot Starter Log4J2 and Why does spring-boot-starter-logging require a dependency on log4-to-slf4j for details). In particular it contains log4j-to-slf4j, which is an implementation of the Log4j 2 API that forwards everything to SLF4J.

You need to remove spring-boot-starter-logging from your dependencies and replace it with spring-boot-starter-log4j2. The usual trick is to:

  • explicitly add spring-boot-starter to your dependencies: it is the only starter that depends on spring-boot-starter-logging, all other starters depends on spring-boot-starter,
  • exclude spring-boot-starter-logging from it:
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
      </exclusions>
    </dependency>
    
  • add spring-boot-starter-log4j2 and the logging API of your choice:
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
      <scope>runtime</scope>
    </dependency>
    
  • remove all other explicit log4j-* dependencies.
Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43