Published 26 Oct, 2022

Java - Logs are not loading according to Log4j2.properties

Category Java
Modified : Dec 01, 2022
56

I am upgrading to Log4j to Log4j2 in my project. I have put log4j2.properties in the classpath, and its loading. Despite that logs are not coming according to properties defined in log4j2.properties.

For instance, I have set the property as

appender.console.layout.pattern = %d %level [%t] [%c] [%M] [%l] - %msg%n

But logs are printing as 2020-05-20 16:12:43.060 DEBUG 58312 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [D:\workspaces\MetricsDailySummaryDao.class]

2020-05-20 16:12:43.061 DEBUG 58312 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [D:\workspaces\MetricsDao.class]

2020-05-20 16:12:43.061 DEBUG 58312 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [D:\workspaces\MetricsHourlySummaryDao.class] We can see.. Although in my property I have removed the month, but it is printing in logs.

Can Anyone suggest solution to this ?

Edit Attaching properties file

status = debug
name= properties_configuration

# Give directory path where log files should get stored


# ConsoleAppender will print logs on console
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout

# Specify the pattern of the logs
appender.console.layout.ConversionPattern = %d %level [%t] [%c] [%M] [%l] - %msg%n

rootLogger.level=debug
rootLogger.appenderRef.stdout.ref= STDOUT
logger.org.springframework.security=DEBUG
logger.org.apache.commons=ERROR
logger.httpclient.wire.header=WARN,OFA
logger.httpclient.wire.content=WARN,OFA
logger.bitronix.tm=ERROR
logger.org.quartz=INFO
logger.org.hibernate=INFO,RFA
logger.org.hibernate.type=INFO,RFA
logger.org.hibernate.transaction.JDBCTransaction=DEBUG,RFA
logger.org.hibernate.engine.StatefulPersistenceContext=INFO,OFA
logger.org.apache.axis=INFO,OFA
logger.org.apache.http=ERROR
logger.com.br.management.dashboard=ERROR,RFA
logger.com.br.management.security=INFO,RFA
logger.com.googlecode.ehcache=ERROR,OFA
logger.com.br.management.datagrid=INFO,OFA
logger.com.br.management.systemConfig=INFO,RFA
logger.com.br.management.policy=DEBUG
logger.com.br.management.monitoring=DEBUG,OFA
logger.com.br.management.billing=ERROR,OFA
logger.com.br.management.autonomics=DEBUG

Answers

There are 1 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Spring, Spring Boot, Log4j, Log4j2. These have been categorized in sections for a clear and precise explanation.

3

Answer relates to comment https://stackoverflow.com/questions/61911283/logs-are-not-loading-according-to-log4j2-properties?noredirect=1comment109537890_61911283

Works here.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-21 10:58:59 INFO [main] [com.example.testlog.TestLogApplication] [logStarting] [org.springframework.boot.StartupInfoLogger.logStarting(StartupInfoLogger.java:55)] - Starting TestLogApplication on LAPTOP-S4VIHOJT with PID 23672 (C:\development\algaworks\workspace\test-log\target\classes started by joaozarate in C:\development\algaworks\workspace\test-log)
2020-21 10:58:59 INFO [main] [com.example.testlog.TestLogApplication] [logStartupProfileInfo] [org.springframework.boot.SpringApplication.logStartupProfileInfo(SpringApplication.java:651)] - No active profile set, falling back to default profiles: default
2020-21 10:59:00 INFO [main] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [initialize] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:108)] - Tomcat initialized with port(s): 8080 (http)
2020-21 10:59:00 INFO [main] [org.apache.coyote.http11.Http11NioProtocol] [log] [org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173)] - Initializing ProtocolHandler ["http-nio-8080"]
2020-21 10:59:00 INFO [main] [org.apache.catalina.core.StandardService] [log] [org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173)] - Starting service [Tomcat]
2020-21 10:59:00 INFO [main] [org.apache.catalina.core.StandardEngine] [log] [org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173)] - Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-21 10:59:00 INFO [main] [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]] [log] [org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173)] - Initializing Spring embedded WebApplicationContext
2020-21 10:59:00 INFO [main] [org.springframework.web.context.ContextLoader] [prepareWebApplicationContext] [org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.prepareWebApplicationContext(ServletWebServerApplicationContext.java:284)] - Root WebApplicationContext: initialization completed in 1144 ms
2020-21 10:59:00 INFO [main] [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] [initialize] [org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.initialize(ExecutorConfigurationSupport.java:181)] - Initializing ExecutorService 'applicationTaskExecutor'
2020-21 10:59:00 INFO [main] [org.apache.coyote.http11.Http11NioProtocol] [log] [org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173)] - Starting ProtocolHandler ["http-nio-8080"]
2020-21 10:59:00 INFO [main] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [start] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:220)] - Tomcat started on port(s): 8080 (http) with context path ''
2020-21 10:59:00 INFO [main] [com.example.testlog.TestLogApplication] [logStarted] [org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:61)] - Started TestLogApplication in 1.739 seconds (JVM running for 2.539)

File /test-log/src/main/resources/application.properties is empty

File: /test-log/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>2.3.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test-log</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test-log</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

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

</project>

File: /test-log/src/main/resources/log4j2.properties

status = error
name = PropertiesConfig

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = info

appenders = console

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d %level [%t] [%c] [%M] [%l] - %msg%n

rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT