log4j2 Web 查找不工作
log4j2 Web Lookup not working
我们有基于 Spring java 的 Web 部署,它们使用 log4j2.xml 将消息记录到文件等。
我们现在需要更新我们的 log4j2.xml 配置,以便能够在其中执行 ${web:contextPath} 网络查找,以便我们可以使用部署的上下文名称作为日志文件的一部分记录器将消息记录到的名称。但是,当我们部署应用程序时,log4j2 配置无法识别任何与 Web 查找相关的内容。创建用于记录消息的文件只是使用名称 ${web
创建的,实际上没有消息记录在其中。
我在 3.0 servlet 中 运行 时阅读了与 log4j2 网络查找相关的各种在线文档,但我仍然看不出我们的配置中可能存在什么问题。而且我不知道要在 log4j 的跟踪日志中查找什么才能查看我们缺少的是什么。
我们的堆栈:
Windows and Linux OSes
Java 8
Tomcat 7.0.5x
log4j-xxx 2.2 (log4j-api, log4j-core, log4j-slf4j-impl, log4j-jcl, log4j-web all in classpath)
非常感谢任何有关如何使网络查找正常工作的帮助。
干杯,
下午
如果您有一个 Spring 4 java 基于注释的 Web 应用程序,则可以在 class 路径中包含 log4j-slf4j-impl
jar,并且仍然执行 log4j2
通过让您的网络初始化 class 扩展 Log4jServletContainerInitializer
并在其上调用 super.onStartup()
来进行网络查找。
示例:
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
容器上工作。
有关所有这些的更多详细信息,请参阅我在 log4j 用户邮件列表线程中获得的答案:
干杯,
下午.
我们有基于 Spring java 的 Web 部署,它们使用 log4j2.xml 将消息记录到文件等。
我们现在需要更新我们的 log4j2.xml 配置,以便能够在其中执行 ${web:contextPath} 网络查找,以便我们可以使用部署的上下文名称作为日志文件的一部分记录器将消息记录到的名称。但是,当我们部署应用程序时,log4j2 配置无法识别任何与 Web 查找相关的内容。创建用于记录消息的文件只是使用名称 ${web
创建的,实际上没有消息记录在其中。
我在 3.0 servlet 中 运行 时阅读了与 log4j2 网络查找相关的各种在线文档,但我仍然看不出我们的配置中可能存在什么问题。而且我不知道要在 log4j 的跟踪日志中查找什么才能查看我们缺少的是什么。
我们的堆栈:
Windows and Linux OSes
Java 8
Tomcat 7.0.5x
log4j-xxx 2.2 (log4j-api, log4j-core, log4j-slf4j-impl, log4j-jcl, log4j-web all in classpath)
非常感谢任何有关如何使网络查找正常工作的帮助。
干杯, 下午
如果您有一个 Spring 4 java 基于注释的 Web 应用程序,则可以在 class 路径中包含 log4j-slf4j-impl
jar,并且仍然执行 log4j2
通过让您的网络初始化 class 扩展 Log4jServletContainerInitializer
并在其上调用 super.onStartup()
来进行网络查找。
示例:
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
容器上工作。
有关所有这些的更多详细信息,请参阅我在 log4j 用户邮件列表线程中获得的答案:
干杯, 下午.