Centralize tomcat logs with log4j2 on Tomcat7 - fbacchella/loghublog4j2 GitHub Wiki

Tomcat uses many log components, but it's mainly based on java.util.logging and deployed web app can uses others. But it's possible change that, thanks to log4j2 bridging.

Some hook are needed and a custom tomcat-juli.jar must be provided. This jar will contains all the necessary bridges. Some can be removed, like slf4j if no web app uses that.

The pom.xml needed to build it is (for a RHEL7 default tomcat) :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fr.loghub</groupId>
    <artifactId>log4j2shade</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>log4j2shade</name>
    <url>http://maven.apache.org</url>

    <properties>
        <!-- The project should have a source encoding set. -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <jdk.compile.home>${java.home}</jdk.compile.home>
        <dependency.log4j2.version>2.11.2</dependency.log4j2.version>
        <dependency.tomcat.version>7.0.76</dependency.tomcat.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>fr.loghub</groupId>
            <artifactId>loghublog4j2</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${dependency.log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${dependency.log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>${dependency.log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jul</artifactId>
            <version>${dependency.log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>${dependency.log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j18-impl</artifactId>
            <version>${dependency.log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.extras</groupId>
            <artifactId>tomcat-extras-juli-adapters</artifactId>
            <version>${dependency.tomcat.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <id>full</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <minimizeJar>false</minimizeJar>
                            <finalName>tomcat-juli</finalName>
                            <filters>
                                <filter>
                                    <!-- Shading signed JARs will fail without this. http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer" />
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
                    <dependencies>
                        <dependency>
                            <groupId>com.github.edwgiz</groupId>
                            <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
                            <version>2.8.1</version>
                        </dependency>
                    </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

This one contains all the bridge, some can be removed depending of the webapp deployed. A mvn build will create a target/tomcat-juli.jar that needs to replace the one provided one in bin/tomcat-juli.jar, /usr/share/tomcat/bin/tomcat-juli.jar in RHEL7.

It's also necessary to define the jul factory to org.apache.logging.log4j.jul.LogManager with:

-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

It's defined in the variable $LOGGING_MANAGER if you use the original catalina.sh. In RHEL7, the file /usr/libexec/tomcat/server needs to be edited. Don't forget to define -Dlog4j.configurationFile=/etc/tomcat/log4j2.xml

As log4j2 and slf4j auto-detected what kind of logging engine it uses, it better to remove all unwanted one, to avoid class loader bug that can be difficult to identify. So clean war must be provided, and RHEL7 install a /usr/share/java/tomcat/log4j.jar that needs to be removed.

⚠️ **GitHub.com Fallback** ⚠️