tomcat 7 internal logging with log4j2.xml
Asked Answered
C

5

16

I am trying to configure tomcat 7 internal logging with log4j2. I have followed the answer provided at Logging server classes in Tomcat 6 with log4j2.

I am using tomcat 7.0.54, and log4j-core-2.1.jar, log4j-api-2.1.jar. I have down loaded the extras and did all the steps below, but when I start tomcat, I get an error:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

These are the steps I performed:

  • put log4j2.xml in $CATALINA_BASE/lib
  • download tomcat-juli.jar and tomcat-juli-adapters.jar from "extras"
  • put log4j-api-2.1.jar, log4j-core-2.1.jar, log4j-jul-2.1.jar, and tomcat-juli-adapters.jar from "extras" into $CATALINA_HOME/lib.
  • replace $CATALINA_HOME/bin/tomcat-juli.jar with tomcat-juli.jar from "extras".
  • delete $CATALINA_BASE/conf/logging.properties
  • set the logging manager to use the manager from the log4j2-jul bridge (log4j-jul-2.1.jar). Alter catalina.sh to ensure that the classpath includes bin/tomcat-juli.jar, lib/log4j-jul-2.1.jar, lib/log4j-api-2.1.jar and lib/log4j-core-2.1.jar, and the command used to start tomcat includes -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager`

I even tried adding this (LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_HOME/lib/log4j2.xml") in catalina.sh but didn't work.

Please let me know if anyone could configure it successfully.

Cheap answered 11/2, 2015 at 3:27 Comment(0)
M
15

I took the following steps and it worked for me.

  1. Put the following jars in $CATALINA_HOME/lib
    • log4j 2 core (log4j-core-2.4.1.jar)
    • log4j 2 api (log4j-api-2.4.1.jar)
    • log4j 2 bridge for log4 j 1.0 (log4j-1.2-api-2.4.1.jar)
    • tomcat-juli-adapters.jar from tomcat extras
  2. Replace existing $CATALINA_HOME/bin/tomcat-juli.jar with the tomcat-juli.jar from tomcat extras
  3. Delete the file $CATALINA_HOME/conf/logging.properties
  4. Put the new log4j 2 config file (log4j2.xml) in $CATALINA_HOME/lib

The trick is to follow the official tomcat 7 documentation to setup log4J 1.X but instead use log4j2 artifacts instead. Also this solution does not require any changes in $CATALINA_HOME/bin/catalina.sh or any other files $CATALINA_HOME/bin

Missing answered 25/2, 2016 at 4:31 Comment(5)
Perfect! I used these steps in conjunction with this log4j2.xml: gist.github.com/bmaupin/475a0cd6e8b374d876f5085846761fb6Recoil
@Recoil Where do i need to place the mentioned XML file??Likeness
@Likeness See step #4 in the answer aboveRecoil
Hi @Recoil Thank you for that file it is great.Slim
Why do we have to replace the original tomcat-juli.jar ?Empurple
C
6

My mistake, I needed to include $CATALINA_BASE/lib in classpath for log4j2.xml to be picked up.

Cheap answered 11/2, 2015 at 6:38 Comment(2)
Why did you have to add $CATALINA_BASE/lib in your classpath? As per the documentation, libraries in $CATALINA_BASE/lib or $CATALINA_BASE/bin are supposed to take precedence on those in $CATALINA_HOME/lib or $CATALINA_HOME/bin. So, there should be no need to add anything to the classpath, the classpath is supposed to be properly built depending on the definition of $CATALINA_BASE. This being said, I have the same problem with Tomcat 8. I am getting the following error: Could not load Logmanager "org.apache.logging.log4j.jul.LogManager" java.lang.ClassNotFoundException: ...Tamekia
In fact, instead of setting the CLASSPATH you can just define the LOGGING_CONFIG variable as follow for log4j: LOGGING_CONFIG="-Dlog4j.configurationFile=${CATALINA_BASE}/conf/log4j2.xml" if your configuration file is ${CATALINA_BASE}/conf/log4j2.xml.Tamekia
F
5

EDIT, I have a much clearer example here using Docker.

Adapt the following for your installation:

My Tomcat 8.5.x is located at /opt/tomcat/

1. Copy log4j2 jar files to /opt/tomcat/lib/

(For Windows use copy)

2. Create /opt/tomcat/conf/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="LOGJ2 %d [%-6p] %c{1} – %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="*" level="trace">
            <AppenderRef ref="Console"/>
        </Logger>
        
        <Root level="info">
            <appenderRef ref="Console" level="info"/>
        </Root>
    </Loggers>
</Configuration>

3. Create or edit /opt/tomcat/bin/setenv.sh or /opt/tomcat/bin/setenv.bat

Without the CLASSPATH set in setenv.sh/bat the initial Tomcat boot loader will not be able to access the class org.apache.logging.log4j.jul.LogManager and log4j2 classes. Setting CLASSPATH before starting Tomcat does not effect the initial Tomcat boot loader since catalina.sh/bat unsets CLASSPATH before running setenv.sh/bat

#The environment variable CLASSPATH is unset in catalina.sh/catalina.bat
CLASSPATH=/opt/tomcat/lib/log4j-api-2.9.1.jar:/opt/tomcat/lib/log4j-core-2.9.1.jar:/opt/tomcat/lib/log4j-jul-2.9.1.jar
JAVA_OPTS=-Dlog4j.configurationFile=/opt/tomcat/conf/log4j2.xml
LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

4. View logs in /opt/tomcat/log/catalina.out and notice the LOGJ2 prefix from the log42.xml

LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – Server built:          Sep 28 2017 10:30:11 UTC
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – Server number:         8.5.23.0
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – OS Name:               Linux
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – OS Version:            4.4.0-93-generic
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – Architecture:          amd64
Fennel answered 12/10, 2017 at 6:58 Comment(0)
H
1

Adding following Jars as mentioned worked for me

Apache Tomcat/7.0.70

:

  1. /tomcat/lib/log4j-core-2.13.3.jar
  2. /tomcat/lib/log4j-api-2.13.3.jar
  3. /tomcat/lib/log4j-1.2-api-2.13.3.jar (required for esapi to work flow)

/tomcat/bin/setenv.sh

LOG_PARAMS="-Dlog4j.configurationFile=file:///tomcat/conf/log4j2.xml"

/tomcat/conf/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<Configuration>

   <!-- ================================= -->
   <!-- Preserve messages in a local file -->
   <!-- ================================= -->

   <Appenders>
      <!-- A time/date based rolling appender -->
      <RollingFile name="FILE" fileName="../logs/server.log" 
               filePattern="../logs/server.log.%d{yyyy-MM-dd}" append="true">
         <PatternLayout>
            <pattern>%d %-5p [%c] [%t] [%x] %m%n</pattern>
         </PatternLayout>
         <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
         </Policies>
      </RollingFile>

   </Appenders>

   <Loggers>

   <!-- ======================= -->
   <!-- Setup the Root category -->
   <!-- ======================= -->

      <Root>
         <AppenderRef ref="FILE" level="debug"/>
      </Root>

   </Loggers>
</Configuration>
Henna answered 8/10, 2020 at 10:31 Comment(0)
F
1

For Docker you can use the following approach.

1 Add a Maven plugin to copy the dependancies to the target directory:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.3.0</version>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <artifactItems>
            <artifactItem>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j.version}</version>
                <destFileName>log4j-api.jar</destFileName>
            </artifactItem>
            <artifactItem>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.version}</version>
                <destFileName>log4j-core.jar</destFileName>
            </artifactItem>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-jul</artifactId>
                <version>${log4j.version}</version>
                <destFileName>log4j-jul.jar</destFileName>
            </dependency>
            <dependency>
                <groupId>com.lmax</groupId>
                <artifactId>disruptor</artifactId>
                <version>3.4.2</version>
                <destFileName>disruptor.jar</destFileName>
            </dependency>
        </artifactItems>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>true</overWriteSnapshots>
    </configuration>
</plugin>

2 Add a setenv.sh to your repo:

CLASSPATH=/usr/local/tomcat/lib/log4j-api.jar:/usr/local/tomcat/lib/log4j-core.jar:/usr/local/tomcat/lib/log4j-jul.jar:/usr/local/tomcat/lib/disruptor.jar
LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

3 Finally create a Dockerfile

FROM tomcat:8.5-jre8-openjdk-slim

# Copy log4j2.
COPY --chmod=0755 setenv.sh /usr/local/tomcat/bin/setenv.sh
COPY target/log4j-api.jar /usr/local/tomcat/lib/log4j-api.jar
COPY target/log4j-core.jar /usr/local/tomcat/lib/log4j-core.jar
COPY target/log4j-jul.jar /usr/local/tomcat/lib/log4j-jul.jar
COPY target/disruptor.jar /usr/local/tomcat/lib/disruptor.jar


COPY log4j2.xml /usr/local/tomcat/lib/log4j2.xml

COPY target/yourapp.war /usr/local/tomcat/webapps/ROOT.war

ENV JAVA_OPTS="-Dlog4j2.configurationFile=/usr/local/tomcat/lib/log4j2.xml"
EXPOSE 8080

CMD ["catalina.sh", "run"]

4 Then build and run your Docker container.

Fennel answered 16/3, 2022 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.