Log4j2 Servlet Appender

Log4j2 Servlet Appender

我正在使用 Tomcat 10(实际上是 TomEE,但它在下面使用 Tomcat 10)并且我正在尝试设置 log4j2 日志记录。我有一半的工作是记录到控制台(我在 catalina.log 中有日志输出)。我无法工作的是 'Servlet' appender。从 log4j2 文档中,我将其作为我的配置

<Configuration status="DEBUG" name="MyBlog">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <Servlet name="Servlet">
            <PatternLayout pattern="%m%n%ex{none}"/>
        </Servlet>
    </Appenders>
    <Loggers>
        <Root level="DEBUG">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="Servlet"/>
        </Root>
    </Loggers>

</Configuration>

在我的 web.xml 我有

    <context-param>
        <param-name>log4jContextName</param-name>
        <param-value>myblog</param-value>
    </context-param>

我部署了三个 log4j2 jar,log4j-core、log4j-api 和 log4j-web,版本为 2.17.1。

我在日志中看到三个错误:

2022-01-18 17:42:59,381 http-nio-8080-exec-16 ERROR No servlet context is available
2022-01-18 17:42:59,383 http-nio-8080-exec-16 ERROR Null object returned for Servlet in Appenders.
2022-01-18 17:42:59,386 http-nio-8080-exec-16 ERROR Unable to locate appender "Servlet" for logger config "root"

感谢收到的任何帮助

添加后-Dlog4j2.debug=true

我看到了这个

DEBUG StatusLogger ServletAppender$Builder(logThrowables="null", ignoreExceptions="null", PatternLayout(%m%n%ex{none}), name="Servlet", Configuration(MyBlog), Filter=null, ={})
ERROR StatusLogger No servlet context is available
DEBUG StatusLogger Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
ERROR StatusLogger Null object returned for Servlet in Appenders.
DEBUG StatusLogger createAppenders(={Console, })
DEBUG StatusLogger Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
DEBUG StatusLogger createAppenderRef(ref="Console", level="null", Filter=null)
DEBUG StatusLogger Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
DEBUG StatusLogger createAppenderRef(ref="Servlet", level="null", Filter=null)
DEBUG StatusLogger Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
DEBUG StatusLogger createLogger(additivity="null", level="DEBUG", includeLocation="null", ={Console, Servlet}, ={}, Configuration(MyBlog), Filter=null)
DEBUG StatusLogger Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
DEBUG StatusLogger createLoggers(={root})
ERROR StatusLogger Unable to locate appender "Servlet" for logger config "root"

顺便说一句,如果我在控制台附加程序中将日志级别设置为 DEBUG,我会得到类似的输出

由于 Java EE 8 和 Jakarta EE 9 之间的命名空间发生变化(参见 Eclipse website for example) you need to use log4j-jakarta-web 而不是 log4j-web

在 Maven 格式中,您需要:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jakarta-web</artifactId>
    <version>2.17.1</version>
</dependency>

编辑: log4j-jakarta-web 中似乎有一个小错误阻止 ServletContainerInitializer 启动。我添加了 PR #723。同时,您可以在 web.xml:

中声明一个 Log4jServletContextListener
  <listener>
    <description>Initializes Log4j 2.x</description>
    <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
  </listener>