使用log4j2.xml的tomcat 7内部日志记录[英] tomcat 7 internal logging with log4j2.xml

本文是小编为大家收集整理的关于使用log4j2.xml的tomcat 7内部日志记录的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图用log4j2配置tomcat 7内部记录.我遵循了 log4j2 .

我使用的是tomcat 7.0.54,log4j-core-2.1.jar,log4j-api-2.1.jar. 我已经加载了附加功能,并执行了下面的所有步骤,但是当我启动tomcat时,我会发现一个错误:

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

这些是我执行的步骤:

我什至尝试在catalina.sh中添加此(LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_HOME/lib/log4j2.xml"),但不起作用.

请让我知道是否有人可以成功配置.

推荐答案

我采取了以下步骤,对我有用.

  1. 将以下罐子放入$ catalina_home/lib中
    • log4j 2核心(log4j-core-2.4.1.jar)
    • log4j 2 api(log4j-api 2.4.1.jar)
    • log4j 2 log4 j 1.0的桥梁(log4j-1.2-api-2.4.1.jar)
    • Tomcat Extras的Tomcat-Juli-Adapters.jar
  2. 替换现有$ catalina_home/bin/tomcat-juli.jar,用tomcat-juli.
  3. 删除文件$ catalina_home/conf/conf/logging.properties
  4. 将新的log4j 2配置文件(log4j2.xml)放入$ catalina_home/lib

诀窍是关注官方 tomcat 7文档7文档 log4j 1.x,而是使用log4j2伪像.此外,此解决方案不需要任何更改$ catalina_home/bin/catalina.sh或任何其他文件$ catalina_home/bin

其他推荐答案

我的错误,我需要在log4j2.xml的classpath中包括$CATALINA_BASE/lib才能被捡起.

其他推荐答案

在这里使用Docker.

适应以下安装:

我的tomcat 8.5.x位于/opt/tomcat/

1.将log4j2 jar文件复制到/opt/tomcat/lib/

(用于Windows使用复制)

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.创建或编辑/编辑/tomcat/bin/setenv.sh OR/opt/tomcat/bin/setenv.bat

如果没有setenv.sh/bat中的classPath集合,初始tomcat启动加载程序将无法访问类org.apache.logging.log4j.jul.logmanager和log4j2类.启动tomcat之前设置classpath不会影响初始的tomcat启动加载程序,因为catalina.sh/bat unsets classpath 在运行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.查看log42.xml

的logj2前缀in/opt/tomcat/log/catalina.out中的logj2前缀
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

本文地址:https://www.itbaoku.cn/post/1575173.html

问题描述

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.

推荐答案

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

其他推荐答案

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

其他推荐答案

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