本文介绍了log4j2 Web查找不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有基于Spring Java的Web部署,这些部署使用log4j2.xml将消息记录到文件等中.

We have Spring java-based web deployments which use log4j2.xml for logging messages to files, etc.

我们现在需要更新log4j2.xml配置,以便能够在其中进行$ {web:contextPath} Web查找,以便我们可以将部署的上下文名称用作记录器名称的一部分.将消息记录到.但是,当我们部署应用程序时,log4j2配置无法识别任何与Web查找相关的内容.创建用于将消息记录到的文件只是使用名称${web创建的,实际上没有消息记录在其中.

We now need to update our log4j2.xml configs in order to be able to do a ${web:contextPath} web lookup inside them so that we can use a deployment's context name as part of the log file's name which the loggers log messages to. However, when we deploy the apps, the log4j2 configurations fail to recognise any web lookup related stuff. The file created to log messages to is simply created with the name ${web and no messages are actually logged in them.

在3.0 servlet中运行时,我已经在线阅读了与log4j2 Web查找有关的各种文档,但是我仍然看不到我们的配置中可能存在什么问题.而且我不知道要在log4j的跟踪日志中查找什么,以查看我们缺少的是什么.

I have read various docs online related to log4j2 web lookups when running in 3.0 servlets but I still can't see what the problem might be in our configurations. And I don't know what to look for in the log4j's trace logs in order to see what it is that we are missing.

我们的堆栈:

Windows and Linux OSesJava 8Tomcat 7.0.5xlog4j-xxx 2.2 (log4j-api, log4j-core, log4j-slf4j-impl, log4j-jcl, log4j-web all in classpath)

Windows and Linux OSesJava 8Tomcat 7.0.5xlog4j-xxx 2.2 (log4j-api, log4j-core, log4j-slf4j-impl, log4j-jcl, log4j-web all in classpath)

非常感谢您提供有关如何使网络查找正常工作的帮助.

Any help on how to get web lookups to work is much appreciated.

干杯,下午

推荐答案

如果您有一个基于Spring 4 Java注释的Web应用程序,则可以在类路径中使用log4j-slf4j-impl jar并仍然进行log4j2 Web查找通过使您的Web初始化类扩展Log4jServletContainerInitializer并在其上调用super.onStartup().

If you have a Spring 4 java annotation based web application, it is possible to have log4j-slf4j-impl jar in the classpath and still do a log4j2 web lookup by having your web initialization class extend Log4jServletContainerInitializer and calling super.onStartup() on it.

示例:

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.logging.log4j.web.Log4jServletContainerInitializer;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class WebInitialiser extends Log4jServletContainerInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext)
            throws ServletException {

        super.onStartup(null, servletContext);

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

        rootContext.register(ApplicationConfig.class, IntegrationConfig.class,
                JmsConfig.class, JmxConfig.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));
    }

}

但是请注意,您似乎仍然需要让web.xml包含<display-name>节点,以便log4j2 Web查找在Tomcat 7.0.5x容器上工作.

Note however that you still seem to need to have your web.xml include a <display-name> node in order for log4j2 web lookups to work on a Tomcat 7.0.5x container.

有关所有这些的更多详细信息,请参阅我在log4j用户邮件列表线程中得到的答案:

For more detail on all this, see the answers that I got in the log4j user mailing list thread:

log4j2 web lookup questions

干杯,下午.

这篇关于log4j2 Web查找不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!