= Log4j2 Configuration with TomEE
:index-group: Configuration
:jbake-date: 2019-07-09
:jbake-type: page
:jbake-status: published
:jbake-tomeepdf:

Out of the box, TomEE is uses a Java-Util-Logging (JUL) based logging system, which is configured using conf/logging.properties.

Occasionally, users may wish to swap over to using Log4j2. These instructions detail how to do this with the latest TomEE versions.
These instructions have been tested with TomEE 7.x and TomEE 8.x (master).

== Setup

You'll need to obtain the following jars: log4j-core, log4j-api and log4j-jul. These instructions were tested with the 2.17.1 versions:

https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-core/2.17.1/log4j-core-2.17.1.jar
https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-api/2.17.1/log4j-api-2.17.1.jar
https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-jul/2.17.1/log4j-jul-2.17.1.jar

Add these to the TomEE bin directory. Add the following to setenv.sh on *nix:

[source]
----
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
LOGGING_CONFIG="-DnoOp"
LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
CLASSPATH=".:$CATALINA_BASE/bin:$CATALINA_BASE/bin/log4j-core-2.17.1.jar:$CATALINA_BASE/bin/log4j-api-2.17.1.jar:$CATALINA_BASE/bin/log4j-jul-2.17.1.jar"
----

or add the following to setenv.bat on Windows:

[source]
----
@echo off
set "JAVA_OPTS=%JAVA_OPTS% -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
set LOGGING_CONFIG=-DnoOpp
set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
set "CLASSPATH=.;%CATALINA_BASE%\bin;%CATALINA_BASE%\bin\log4j-core-2.17.1.jar;%CATALINA_BASE%\bin\log4j-api-2.17.1.jar;%CATALINA_BASE%\bin\log4j-jul-2.17.1.jar"
----

If you are using the TomEE Maven Plugin, it can be configured as follows:

[source]
----
<plugin>
    <groupId>org.apache.tomee.maven</groupId>
    <artifactId>tomee-maven-plugin</artifactId>
    <version>${tomee.plugin.version}</version>
    <configuration>
        <tomeeVersion>${tomee.version}</tomeeVersion>
        <tomeeClassifier>plus</tomeeClassifier>
        <classpaths>
            <classpath>org.apache.logging.log4j:log4j-api:2.17.1</classpath>
            <classpath>org.apache.logging.log4j:log4j-jul:2.17.1</classpath>
            <classpath>org.apache.logging.log4j:log4j-core:2.17.1</classpath>
        </classpaths>
        <systemVariables>
            <log4j.configurationFile>${project.basedir}/src/test/tomee/conf/log4j2.xml</log4j.configurationFile>
            <java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
        </systemVariables>
    </configuration>
</plugin>
----

Take care to match the jar filenames if you have downloaded jars for a slightly different version of log4j2.

== Configuration

Add your log4j2.xml config in the `bin` directory.  Here's a simple config you can use to help you get started:

[source]
----
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="warn" name="catalina" packages="">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
        </Console>
        <File name="catalina" fileName="${sys:catalina.base}/logs/catalina.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
        <Async name="Async">
            <AppenderRef ref="catalina" />
        </Async>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Async" />
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>
----