转换log4j.properties文件以使Tomcat与Log4j2一起使用 [英] Converting log4j.properties file for Tomcat to work with Log4j2

查看:129
本文介绍了转换log4j.properties文件以使Tomcat与Log4j2一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Tomcat 8配置为使用Log4j2进行日志记录.

I'm trying to configure Tomcat 8 to use Log4j2 for logging.

我找到了使用Log4j登录Tomcat的参考.它提供了一个示例log4j.properties文件,该文件将Log4j配置为与Tomcat的内部日志记录匹配.对于Log4j2而言,大多数转换看起来非常简单,但是最后将记录器映射到附加器的部分让我很困惑:

I've found this reference for Logging in Tomcat using Log4j. It provides a sample log4j.properties file that configures Log4j to match Tomcat's internal logging. Most of this looks pretty straightforward to convert for Log4j2, but the section at the end that maps loggers to appenders has me stumped:

# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] =\
  INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] =\
  INFO, HOST-MANAGER

有人有没有将此配置转换为可与Log4j2一起使用?我一直在使用Log4j2配置文档,并已阅读 Log4j2体系结构页面,但是在Log4j2中如何进行这种容器映射方面我没有发现很多资料.

Has anyone converted this configuration to work with Log4j2? I've been working from the Log4j2 configuration documentation and have read through the Log4j2 Architecture page, but I haven't found much material on how to do this sort of container mapping in Log4j2.

我想我可以为每个容器做一个单独的配置,但是我更喜欢将它放在一个地方,就像示例Log4j配置一样.

I suppose I could do a separate configuration for each container, but I'd prefer to keep it in one place, as in the sample Log4j configuration.

推荐答案

问了这个问题之后,我花了更多时间来设置log4j2,这就是 我想出的log4j2.xml文件.它模仿配置 在使用Log4j登录Tomcat 中进行了介绍.它使用多个记录器来路由消息 分隔日志文件.

After asking this question, I spent some more time with setting up log4j2, and this is the log4j2.xml file I came up with. It mimics the configuration described in Logging in Tomcat using Log4j. It uses multiple loggers to route messages to separate log files.

<?xml version="1.0" encoding="utf-8"?>
<Configuration status="info">
  <Properties>
    <Property name="logdir">${sys:catalina.base}/logs</Property>
    <Property name="layout">%d [%t] %-5p %c- %m%n</Property>
  </Properties>
  <Appenders>
    <Console name="CONSOLE" target="SYSTEM_OUT">
      <PatternLayout pattern="${layout}"/>
    </Console>
    <RollingFile name="CATALINA"
        fileName="${logdir}/catalina.log"
        filePattern="${logdir}/catalina.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
    <RollingFile name="LOCALHOST"
        fileName="${logdir}/localhost.log"
        filePattern="${logdir}/localhost.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
    <RollingFile name="MANAGER"
        fileName="${logdir}/manager.log"
        filePattern="${logdir}/manager.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
    <RollingFile name="HOST-MANAGER"
        fileName="${logdir}/host-manager.log"
        filePattern="${logdir}/host-manager.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="CATALINA"/>
    </Root>
    <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]"
        level="info" additivity="false">
      <AppenderRef ref="LOCALHOST"/>
    </Logger>
    <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]"
        level="info" additivity="false">
      <AppenderRef ref="MANAGER"/>
    </Logger>
    <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]"
        level="info" additivity="false">
      <AppenderRef ref="HOST-MANAGER"/>
    </Logger>
  </Loggers>
</Configuration>

但是,要使其正常工作,必须先获得 一堆其他文件 配置正确. 使用log4j2.xml的tomcat 7内部日志记录 来自Paramita Banerjee的帖子对此很有帮助.

However, getting this to work properly required getting a bunch of other files configured correctly. The tomcat 7 internal logging with log4j2.xml posting from Paramita Banerjee was helpful with this.

此文件放入CATALINA_HOME/bin/:

  • tomcat-juli.jar

如果要从Maven存储库中提取文件,则会得到一个文件 命名为tomcat-extras-juli-8.0.15.jar之类的名称(当前 版本,当我写这个).但是,它需要重命名为 tomcat-juli.jar– Tomcat安装脚本在其中使用该名称 设置CLASSPATH.

If you're pulling this from the Maven repository, you'll get a file named something like tomcat-extras-juli-8.0.15.jar (the current version when I wrote this). However, it needs to be renamed to tomcat-juli.jar – the Tomcat setup scripts use that name in setting up the CLASSPATH.

这些文件放在CATALINA_HOME/lib/中:

  • commons-logging-1.2.jar
  • log4j-1.2-api-2.1.jar
  • log4j-api-2.1.jar
  • log4j-core-2.1.jar
  • log4j-jcl-2.1.jar
  • log4j-jul-2.1.jar
  • log4j-web-2.1.jar
  • tomcat-juli-adapters-8.0.15.jar
  • commons-logging-1.2.jar
  • log4j-1.2-api-2.1.jar
  • log4j-api-2.1.jar
  • log4j-core-2.1.jar
  • log4j-jcl-2.1.jar
  • log4j-jul-2.1.jar
  • log4j-web-2.1.jar
  • tomcat-juli-adapters-8.0.15.jar

log4j-web-2.1.jar(可能只需要与您的Web应用程序一起部署)即可.其用法在 在Web应用程序中使用Log4j 2 . 仅当具有使用以下内容的应用程序时才需要log4j-1.2-api-2.1.jar 旧的log4j 1.2界面.

log4j-web-2.1.jar may not be needed here (it may just need to be deployed with your web application) – its use is described in Using Log4j 2 in Web Applications. log4j-1.2-api-2.1.jar is needed only if you have applications that use the older log4j 1.2 interface.

CATALINA_BASE/conf中,我禁用了logging.properties.

我使用以下setenv.sh脚本定义了CLASSPATHLOGGING_MANAGER环境变量对于Tomcat是正确的.它要么进入 CATALINA_BASE/bin或进入CATALINA_HOME/bin. (我把它放进去了 CATALINA_HOME/bin.)如果是,则由Tomcat的启动脚本执行. 当下.您可能更喜欢一些简单的方法,但这与 启动脚本的样式.

I used the following setenv.sh script to define the CLASSPATH and LOGGING_MANAGER environment variables correctly for Tomcat. It goes in either CATALINA_BASE/bin or into CATALINA_HOME/bin. (I put it in CATALINA_HOME/bin.) It is executed by Tomcat's startup scripts if it's present. You might prefer something simpler, but this is in keeping with the style of the startup scripts.

LOG4J_JARS="log4j-core-2.1.jar log4j-api-2.1.jar log4j-jul-2.1.jar"

# make log4j2.xml available
if [ ! -z "$CLASSPATH" ] ; then CLASSPATH="$CLASSPATH": ; fi
CLASSPATH="$CLASSPATH""$CATALINA_BASE"/lib

# Add log4j2 jar files to CLASSPATH
for jar in $LOG4J_JARS ; do
  if [ -r "$CATALINA_HOME"/lib/"$jar" ] ; then
    CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/lib/"$jar"
  else
    echo "Cannot find $CATALINA_HOME/lib/$jar"
    echo "This file is needed to properly configure log4j2 for this program"
    exit 1
  fi
done

# use the logging manager from log4j-jul
LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"

正如尼克在回应中提到的那样, 访问日志.我也没有尝试做任何事情.

As Nick mentioned in his response, there's also the output to the access log. I haven't tried to do anything with that, either.

我希望其他人觉得这有用.回想起来,看起来很漂亮 直截了当.但是,有很多部分必须 恰到好处"才能发挥作用,这是一个挑战(至少对于 新手).

I hope others find this useful. In retrospect, it seems pretty straightforward. However, there are a lot of parts that have to be “just right” for it to work, and that was a challenge (at least for a newbie).

这篇关于转换log4j.properties文件以使Tomcat与Log4j2一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆