%d{yyyy-MM-dd HH:mm:ss.SSS" /> %d{yyyy-MM-dd HH:mm:ss.SSS" /> %d{yyyy-MM-dd HH:mm:ss.SSS"/>

Logs file getting created but logs are not getting written to it in springboot 3 war deployed on Tomcat 10.1.19

41 Views Asked by At

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration  status="DEBUG">
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %c:%L - %m%n
        </Property>
        <Property name="LOG_DIR">${bundle:application:LOG_DIR}</Property>
    </Properties>

    <Appenders>
        <Console name="console_log">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Console>

        <RollingFile name="success_log"
            fileName="${LOG_DIR}/success.log"
            filePattern="${LOG_DIR}/success-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <Pattern>${LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>

        <RollingFile name="error_log"
            fileName="${LOG_DIR}/error.log"
            filePattern="${LOG_DIR}/error-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <Pattern>${LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger name="Success" level="info">
            <AppenderRef ref="success_log" />
        </Logger>

        <Logger name="Error" level="error">
            <AppenderRef ref="error_log" />
        </Logger>

        <Root level="info">
            <AppenderRef ref="console_log" />
        </Root>
    </Loggers>

</Configuration>

pom.xml:

<?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.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>pkg</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging> 
    <name>davinci</name>
    <description>Desc</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <exclusions>
                 <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <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>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>jakarta.mail</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.7.5</version>
            <exclusions>
                 <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    
        <!-- Addition in Edit 2 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jakarta-web</artifactId>
        </dependency>

        <dependency>
            <groupId>third.party.lib</groupId>
            <artifactId>third-party-lib</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

com.pkg.LogService:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
public class LogService {
...
    private static final Logger LOG = LoggerFactory.getLogger("Error");
...
}

I have added status="DEBUG" to the logback configuration and it indicates that all the appenders I have decalred are being shut down. The following lines are from the tomcat console:

DEBUG StatusConsoleListener Appender console_log stopped with status true
DEBUG StatusConsoleListener Appender error_log stopped with status true
DEBUG StatusConsoleListener Appender success_log stopped with status true

...

DEBUG StatusConsoleListener Registering MBean org.apache.logging.log4j2:type=6853425f,component=Loggers,name=
DEBUG StatusConsoleListener Registering MBean org.apache.logging.log4j2:type=6853425f,component=Loggers,name=PredictionSuccess
DEBUG StatusConsoleListener Registering MBean org.apache.logging.log4j2:type=6853425f,component=Loggers,name=PredictionError
DEBUG StatusConsoleListener Registering MBean org.apache.logging.log4j2:type=6853425f,component=Appenders,name=success_log
DEBUG StatusConsoleListener Registering MBean org.apache.logging.log4j2:type=6853425f,component=Appenders,name=error_log
DEBUG StatusConsoleListener Registering MBean org.apache.logging.log4j2:type=6853425f,component=Appenders,name=console_log

The messages are printed in this format:

[http-nio-8081-exec-3] INFO org.springframework.web.servlet.DispatcherServlet - Completed initialization in 1 ms
[http-nio-8081-exec-3] ERROR <MY ERROR>

The WAR runs on a Tomcat 10.1.19 container running at localhost:8080. Every apsect of the application but logging works as intended. Any help would be appreciated.

I need to use the third party library. It was built with ant in such a way that it does not have a pom in META-INF but somehow all the subfolders of its dependencies in META-INF/maven do. I need the exclusions to be made as without them the third party library causes this error:

java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.SimpleLoggerFactory loaded from file:/C:/<TOMCAT_HOME>/webapps/appName/WEB-INF/lib/third-party-lib-0.0.1-SNAPSHOT.jar)

I have no choice but to use the third party dependency as it is...

After reviewing the Tomcat 10.1.19 logs, it seems that the context loads succesfully but reconfiguration is then triggered for some reason and that is what causes the issue. It is subsequent to this that the appenders are turned off

DEBUG StatusConsoleListener LoggerContext[name=/davinci, org.apache.logging.log4j.core.LoggerContext@22edca96] started OK with configuration XmlConfiguration[location=C:\Program Files\Apache Software Foundation\Tomcat 10.1\webapps\davinci\WEB-INF\classes\log4j2.xml].
**** ^This is where I would like it to stop. ****

**** \/This is where things go wrong... ****
DEBUG StatusConsoleListener Reconfiguration started for context[name=/davinci] at URI null (org.apache.logging.log4j.core.LoggerContext@22edca96) with optional ClassLoader: null
DEBUG StatusConsoleListener Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@78fd9a11

The issue persists even after the apppenders keep running. I noticed these lines in the Tomcat 10.1.19 console, they are absent while running in STS. Could they indicate what is going wrong?

INFO StatusConsoleListener Log4jServletContextListener triggered a Log4j context initialization.
DEBUG StatusConsoleListener Log4jServletFilter initialized.

Edit:- formatting.

Edit2:- pom.xml change.

Edit3:- Tomcat Log4J logs.

Edit4:- Appenders do not get stopped now but the issue persists.

0

There are 0 best solutions below