membersound
membersound

Reputation: 86687

How to prevent duplicate logging to tomcat console in catalina.out?

I'm deploying a war application to a tomcat webserver. Problem: my logging does both to the application logs, and also to the catalina console logs (which are catalina.out).

How can I prevent the duplicate logging into that catalina file?

log4j2-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %p %c{1.}: %m%n"/>
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
        </Console>

        <RollingRandomAccessFile name="APP" fileName="/logs/application.logs" filePattern="/logs/application-%d{yyyy-MM-dd}.log.gz">
            <PatternLayout pattern="%d %p %c{1.}: %m%n"/>
            <Filters>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>

            <Policies>
                <TimeBasedTriggeringPolicy modulate="true"/>
            </Policies>
        </RollingRandomAccessFile>
    </Appenders>

    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="CONSOLE" />
            <AppenderRef ref="APP" />
        </Root>
    </Loggers>
</Configuration>

I also already removed the java.util.logging.ConsoleHandler from /opt/tomcat/conf/logging.properties:

handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler
.handlers = 1catalina.org.apache.juli.AsyncFileHandler

Result: the logging still goes to catalina.out.

Upvotes: 2

Views: 2014

Answers (1)

Stephen C
Stephen C

Reputation: 718728

Remove this from the <Loggers> element.

 <AppenderRef ref="CONSOLE" />

Explanation: the Catalina.out file captures everything written to Tomcat's standard output and standard error streams. Your CONSOLE appender is set up to log to standard output.

It appears that the log4j2-spring.xml configs are taking precedence over logging.properties. Someone would need to take a look at your setup to understand why.

(My guess is that Spring or something is is giving Tomcat's JVM a -D option to set a system property giving the location of the logging config. The mechanisms that log4j2 uses to locate the configs are described in https://logging.apache.org/log4j/2.x/manual/configuration.html)


There is then still a problem for me: in my local dev environment, I'd still like to get any logs to console, for not having to step into the decent logfiles. I'd only want to disable the console logging on the remote/test/production servers.

Depending on where the log4j2-spring.xml file is (in a WAR file?) you could build two variants of the WAR file with different config files.

Alternatively, it should be possible to put an alternative log4j2-spring.xml file into Tomcat's common directory so that it overrides the version in the WAR file.

Upvotes: 2

Related Questions