javaspringlog4jlog4j2

How to avoid outputting a lot of useless logs in log4j2?


I am moving a project from Weblogic into Tomcat, and I found that there is a problem of mixing log4j and log4j2 when the project is using Weblogic, but it runs normally in Weblogic and outputs the required logs normally, including logs from org.apache.logging.log4j.LogManager and logs from org.slf4j.LoggerFactory

However, when I use Tomcat, the console can only output the logs from org.apache.logging.log4j.LogManager but not the logs from org.slf4j.LoggerFactory.

Logger using org.apache.logging.log4j.LogManager:

private static final Logger logger = LogManager.getLogger(WebConfig.class);

Logger using org.slf4j.LoggerFactory:

private final Logger logger = LoggerFactory.getLogger(LoginController.class);

Config in Weblogic:

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="TEST_WebLog">
    
    <ThresholdFilter level="debug"/>
 
    <Appenders>
        <Console name="Console">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{sessionId}] [%X{userId}] [%-5p] [Thread ID:%tid] [%c{1}:%L] - %msg%n"/>
        </Console>
        <RollingFile name="ApplicationLog" fileName="${sys:user.home}/A/B/logs/Applogs.log" filePattern="${sys:user.home}/A/B/logs/Applogs.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{sessionId}] [%X{userId}] [%-5p] [Thread ID:%tid] [%c{1}:%L] - %msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="30 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <RollingFile name="DatabaseLog" fileName="${sys:user.home}/A/B/logs/Dblogs.log" filePattern="${sys:user.home}/A/B/logs/Dblogs.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <RollingFile name="XaiLog" fileName="${sys:user.home}/A/B/logs/xai.log" filePattern="${sys:user.home}/A/B/logs/xai.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <RollingFile name="AbstractBaseDaoLog" fileName="${sys:user.home}/A/B/logs/AbstractBaseDao.log" filePattern="${sys:user.home}/A/B/logs/AbstractBaseDao.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="2000 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>              
    </Appenders>

    <Loggers>
        <Logger name="org.hibernate" level="debug" additivity="false">
            <AppenderRef ref="DatabaseLog" level="debug"/>
            <AppenderRef ref="Console" level="info"/>
        </Logger>
        <Logger name="com.A.common.dao.AbstractBaseDao" level="debug" additivity="false">
            <AppenderRef ref="AbstractBaseDaoLog" level="debug"/>
        </Logger>
        <Logger name="com.A.xai" level="debug" additivity="false">
            <AppenderRef ref="XaiLog" level="debug"/>
        </Logger>
        <Logger name="com.A" level="debug" additivity="false">
            <AppenderRef ref="ApplicationLog" level="debug"/>
            <AppenderRef ref="Console" level="info"/>
        </Logger>
        <Root level="all">
            <AppenderRef ref="ApplicationLog"/>
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
 
</Configuration>

Next, I removed the slf4j-log4j12 dependency and added the log4j-slf4j-impl dependency to resolve the issue of mixing log4j and log4j2 in Tomcat.

Config in Tomcat:

<!--        <dependency>-->
<!--            <groupId>org.slf4j</groupId>-->
<!--            <artifactId>slf4j-log4j12</artifactId>-->
<!--            <version>1.7.25</version>-->
<!--            <scope>compile</scope>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="TEST_WebLog">
    
    <ThresholdFilter level="debug"/>
 
    <Appenders>
        <Console name="Console">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{sessionId}] [%X{userId}] [%-5p] [Thread ID:%tid] [%c{1}:%L] - %msg%n"/>
        </Console>
        <RollingFile name="ApplicationLog" fileName="${sys:user.home}/A/B/logs/Applogs.log" filePattern="${sys:user.home}/A/B/logs/Applogs.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{sessionId}] [%X{userId}] [%-5p] [Thread ID:%tid] [%c{1}:%L] - %msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="30 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <RollingFile name="DatabaseLog" fileName="${sys:user.home}/A/B/logs/Dblogs.log" filePattern="${sys:user.home}/A/B/logs/Dblogs.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <RollingFile name="XaiLog" fileName="${sys:user.home}/A/B/logs/xai.log" filePattern="${sys:user.home}/A/B/logs/xai.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <RollingFile name="AbstractBaseDaoLog" fileName="${sys:user.home}/A/B/logs/AbstractBaseDao.log" filePattern="${sys:user.home}/A/B/logs/AbstractBaseDao.log.%i">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="2000 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>              
    </Appenders>

    <Loggers>
        <Logger name="org.hibernate" level="debug" additivity="false">
            <AppenderRef ref="DatabaseLog" level="debug"/>
            <AppenderRef ref="Console" level="info"/>
        </Logger>
        <Logger name="com.A.common.dao.AbstractBaseDao" level="debug" additivity="false">
            <AppenderRef ref="AbstractBaseDaoLog" level="debug"/>
        </Logger>
        <Logger name="com.A.xai" level="debug" additivity="false">
            <AppenderRef ref="XaiLog" level="debug"/>
        </Logger>
        <Logger name="com.A" level="debug" additivity="false">
            <AppenderRef ref="ApplicationLog" level="debug"/>
            <AppenderRef ref="Console" level="info"/>
        </Logger>
        <Root level="all">
            <AppenderRef ref="ApplicationLog"/>
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
 
</Configuration>

At this time, it runs normally in Tomcat and outputs the required logs normally, including logs from org.apache.logging.log4j.LogManager and logs from org.slf4j.LoggerFactory.

But the console output a lot of logs that were useless to me (this does not happen in WebLogic with a mix of log4j and log4j2)

Part of the useless log in Console:

2024-07-03 17:36:47.525 [] [] [DEBUG] [Thread ID:72] [Segment:777] - fault added 399 on disk
2024-07-03 17:36:47.525 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 832 on heap
2024-07-03 17:36:47.525 [] [] [DEBUG] [Thread ID:72] [Segment:764] - fault removed -696 from heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 848 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:777] - fault added 399 on disk
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 832 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:764] - fault removed -672 from heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 848 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:777] - fault added 386 on disk
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 832 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:764] - fault removed -672 from heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 848 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:777] - fault added 388 on disk
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 832 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:764] - fault removed -672 from heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 832 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:777] - fault added 388 on disk
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 840 on heap
2024-07-03 17:36:47.526 [] [] [DEBUG] [Thread ID:72] [Segment:764] - fault removed -704 from heap
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:72] [Segment:777] - fault added 404 on disk
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 848 on heap
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:72] [Segment:764] - fault removed -720 from heap
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 856 on heap
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:72] [Segment:777] - fault added 411 on disk
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 864 on heap
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:72] [Segment:764] - fault removed -720 from heap
2024-07-03 17:36:47.535 [] [] [DEBUG] [Thread ID:78] [Segment:434] - put added 840 on heap

Expectation: How can I avoid these large amounts of useless log output while resolving the problem of mixing log4j and log4j2 in Tomcat, just like I did with WebLogic with a mix of log4j and log4j2?


Solution

  • This is because your log level is configured to DEBUG. There are 5 log levels in Log4j, read about them here.

    Try changing it to INFO level you will see less such logs.