Skip to content

Instantly share code, notes, and snippets.

@arolfes
Created December 20, 2018 13:48
Show Gist options
  • Save arolfes/ae79dbde52576d1fd4221f58fb893297 to your computer and use it in GitHub Desktop.
Save arolfes/ae79dbde52576d1fd4221f58fb893297 to your computer and use it in GitHub Desktop.
How to configure Tomcat 8.X to use log4j2 as default logging implementation

HowTo configure Tomcat 8.X to use log4j2

I want to change the default logging from tocat to use log4j2 instead of java logging

necessary libs

I needed these 3 libs to simple log into catalia.out

  • log4j-api-2.11.1.jar
  • log4j-core-2.11.1.jar
  • log4j-jul-2.11.1.jar

log4j2.xml

create a log4j2.xml under $CATALINA_HOME/conf/

<Configuration status="warn" name="catalina" packages="">
  <Appenders>
    <RollingFile name="catalina" fileName="${sys:catalina.base}/logs/catalina.out"
                 filePattern="${sys:catalina.base}/logs/catalina.%d{yyyy-MM-dd}.log.gz">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
      </Policies>
    </RollingFile>
    <Async name="Async">
      <AppenderRef ref="catalina"/>
    </Async>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="Async"/>
<!--      <AppenderRef ref="catalina"/> -->
    </Root>
  </Loggers>
</Configuration>

setenv.sh

setenv.sh script are automatic executed during catalina.sh execution. it helps to keep customization seperate.

create the file under $CATALINA_HOME/bin/

#!/bin/sh

CLASSPATH=$CLASSPATH:$CATALINA_HOME/bin/log4j-jul-2.11.1.jar:$CATALINA_HOME/bin/log4j-api-2.11.1.jar:$CATALINA_HOME/bin/log4j-core-2.11.1.jar

LOGGING_CONFIG="-Dlog4j.configurationFile=file://$CATALINA_HOME/conf/log4j2.xml"

JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

CATALINA_OUT=/dev/null

inside eclipse

todo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment