使用log4j2.xml进行tomcat 7内部日志记录

ffx8fchx  于 2022-11-13  发布在  其他
关注(0)|答案(5)|浏览(234)

我正在尝试使用log4j2配置tomcat 7内部日志记录。我已经按照Logging server classes in Tomcat 6 with log4j2提供的答案进行了操作。
我正在使用tomcat 7.0.54和log4j-core-2.1.jarlog4j-api-2.1.jar。我已经下载了额外的内容并执行了以下所有步骤,但当我启动tomcat时,我收到一个错误:

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

以下是我执行的步骤:

  • log4j2.xml放入$CATALINA_BASE/lib
  • 从“extras”下载tomcat-juli.jartomcat-juli-adapters.jar
  • 将“额外”中的log4j-api-2.1.jarlog4j-core-2.1.jarlog4j-jul-2.1.jartomcat-juli-adapters.jar放入$CATALINA_HOME/lib中。
  • 将“extras”中的$CATALINA_HOME/bin/tomcat-juli.jar替换为tomcat-juli.jar
  • 删除$CATALINA_BASE/conf/logging.properties
  • 设置日志管理器使用来自log4j2-jul桥(log4j-jul-2.1.jar)的管理器。修改catalina.sh以确保类路径包括bin/tomcat-juli.jarlib/log4j-jul-2.1.jarlib/log4j-api-2.1.jarlib/log4j-core-2.1.jar,并且用于启动tomcat的命令包括-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager '

我甚至尝试在catalina.sh中添加这个(LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_HOME/lib/log4j2.xml"),但没有成功。
请让我知道是否有人可以成功配置它。

u7up0aaq

u7up0aaq1#

我采取了以下步骤,它为我工作。

  • 将以下jar文件放入$ Catalina _HOME/lib中
  • log4j 2内核(log4j-core-2.4.1.jar)
  • 日志4j 2应用程序接口(日志4j-ap-2.4.1.jar)
  • 用于log 4 j 1.0的log4j 2桥接器(log4j-1.2-api-2.4.1.jar)
  • tomcat-juli-adapters.jar来自tomcat附加组件
  • 将现有的$ Catalina _HOME/bin/tomcat-juli.jar替换为tomcat extras中的tomcat-juli.jar
  • 删除文件$ Catalina _HOME/conf/logging.properties
  • 将新的log4j 2配置文件(log4j2.xml)放在$ Catalina _HOME/lib中

诀窍是按照官方的tomcat 7 documentation来安装log4J 1.X,而不是使用log4j 2工件catalina.sh。

fhg3lkii

fhg3lkii2#

我的错误是,我需要在类路径中包含$CATALINA_BASE/lib才能选择log4j2.xml

8fsztsew

8fsztsew3#

编辑,我有一个更清晰的例子在这里使用Docker。

根据您的安装调整以下内容:

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

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

  • 文件名:
  • 文件名:
  • 文件名:

(For Windows使用副本)

2.创建/opt/tomcat/配置文件/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.创建或编辑/opt/tomcat/bin/setenv.sh或/opt/tomcat/bin/setenv.bat

如果没有在www.example.com中设置CLASSPATHsetenv.sh/bat,则初始Tomcat Boot 加载程序将无法访问类org.apache.logging.log4j.jul.LogManager和log4j 2类。在启动Tomcat之前设置CLASSPATH不会影响初始Tomcat引导加载程序,因为catalina.sh/bat在运行www.example.com之前,www.example.com unsets CLASSPATHsetenv.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.查看/opt/tomcat/log/ Catalina .out中的日志,并注意log42.xml中的LOGJ 2前缀

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
csga3l58

csga3l584#

添加下面提到的Jar对我很有效
Apache Tomcat/7.0.70版本的操作系统

  1. /tomcat/lib/log4j-core-2.13.3.jar
  2. /tomcat/lib/log4j-api-2.13.3.jar
    1./tomcat/lib/log4j-1.2-api-2.13.3.jar(esapi工作流所必需的)
    /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>
xzv2uavs

xzv2uavs5#

对于Docker,您可以使用以下方法。

1添加Maven插件以将依赖关系复制到目标目录:

<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将www.example.com添加setenv.sh到您的存储库中:

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最后创建一个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然后构建并运行Docker容器。

相关问题