How to disable log messages when spring boot application run?

Viewed 442

This is my log4j2.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="warn">
        <Properties>
            <Property name="basePath">/folderName/logs/</Property>
        </Properties>
    
        <Appenders>
            <RollingFile name="fileLogger" fileName="${basePath}/fileName.log"
                         filePattern="${basePath}/edf-info-%d{MM-yyyy-dd}.log">
                <PatternLayout>
                    <pattern>%d{MM-yyyy-dd} [%-5level] %l - %msg%n</pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                </Policies>
            </RollingFile>
            <Console name="console" target="SYSTEM_OUT">
                <PatternLayout>
                    <pattern>%d{MM-yyyy-dd} [%-5level] %l - %msg%n</pattern>
                </PatternLayout>
            </Console>
        </Appenders>
        <Loggers>
            <Logger name="com.company.projectName" level="info" additivity="true">
                <appender-ref ref="fileLogger" level="debug"/>
            </Logger>
            <Root level="info" additivity="false">
                <appender-ref ref="console"/>
            </Root>
        </Loggers>
    </Configuration>

When I used previous versions of Log4j2, only requests sent by my application were being written to my log file in my log folder on my computer. For example:

12-2021-27 [INFO ] com.company.projectname.controller.ClassController.methodName(ClassController.java:21) - paramName1, paramname2
12-2021-27 [INFO ] com.company.projectname.service.ClassServiceImpl.methodName(ClassServiceImpl.java:21) - paramName1, paramname2

But when I switched to the new version of Log4j, because of the vulnerability, before the messages I showed above, when my spring boot project runs, its messages also written. I mean focus on following.

12-2021-22 [INFO ] org.springframework.boot.StartupInfoLogger.logStarting(StartupInfoLogger.java:55) - Starting ProjectNameApplication using Java 1.8.0_111 on pc-name with PID 00000 (C:\Workspace\project-name\target\classes started by user.namein C:\Workspace\project-name)
12-2021-22 [INFO ] org.springframework.boot.SpringApplication.logStartupProfileInfo(SpringApplication.java:635) - No active profile set, falling back to default profiles: default
12-2021-22 [INFO ] org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:61) - Started ProjectNameApplication in 8.858 seconds (JVM running for 12.962)

but I don't want to see these messages in my log file. I just want to write messages when request and response are generated, like before my configuration. How to solve this? I mean I want to only messages to the log file which my requests and responses.

1 Answers

Those unwanted messages use SpringApplication#getApplicationLog() to retrieve a logger, whose name is the fully qualified class name of your main class. The logger's name is probably a child of "com.company.projectName", hence it is sent to the file appender. You can confirm it by adding %c to your pattern layout.

Assuming your main class is called com.company.projectName.Application, you can:

  • either configure a com.company.projectName.Application logger that uses only the console appender:

    <Logger name="com.company.projectName.Application" level="INFO" additivity="false">
        <AppenderRef ref="console"/>
    </Logger>
    
  • or replace the com.company.projectName logger with more specific ones:

    <Logger name="com.company.projectName.controller" level="DEBUG">
        <AppenderRef ref="fileLogger" />
    </Logger>
    <Logger name="com.company.projectName.controller" level="DEBUG">
        <AppenderRef ref="fileLogger" />
    </Logger>
    
Related