问题描述
我有一个带有log4j2的Spring MVC Web应用程序(4.0).此WebApp不使用Web.xml,并通过Java Config来处理配置. log4j2配置需要在外部配置文件中进行,而不是在类路径上进行.
在先前使用web.xml的化身中,我们有
<context-param> <param-name>log4jConfiguration</param-name> <param-value>file:///path/to/log4j2.xml</param-value> </context-param>
这有效.
在新的web.xml世界中,我尝试了以下方式:
public class WebappInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { System.out.println("WebappInitializer.onStartup()"); servletContext.setAttribute("log4jConfiguration", "file:///path/to/log4j2.xml");
这不起作用.似乎发生的时间太晚了,无论如何都行不通.日志显示:
ERROR StatusLogger No Log4j context configuration provided. This is very unusual. WebappInitializer.onStartup()
,没有log4j记录.
在Web.xml spring MVC应用中,用Web.xml替换此上下文-Param声明的正确方法是什么?
更新:
我可以通过添加以下web.xml:
来消除此问题<?xml version="1.0"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>log4jConfiguration</param-name> <param-value>file:///path/to/log4j2.xml</param-value> </context-param> </web-app>
当然,必须有更好的方法!
推荐答案
我相信您可以在这样的运行时将log4j2重新配置为新的配置文件.
LoggerContext context = (LoggerContext)LogManager.getContext(false); context.setConfigLocation(URI.create("path to file")); context.reconfigure();
可以将其添加到您的Onstartup中.
其他推荐答案
可能这有点太晚了,但是您可以执行以下操作以将log4j2配置设置为外部路径:
public class ApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.setInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION, "file:///path/to/log4j2.xml");
其他推荐答案
我尚未测试它,但值得一试.用以下 Log4jConfigurer 呼叫:
Log4jConfigurer.initLogging("file:///path/to/log4j2.xml")
这基本上是在web.xml文件中使用log4jConfigLocation时在引擎盖下发生的情况(通过<context-param>/Current/javadoc-api/org/springframework/web/util/log4jwebconfigurer.html" rel =" nofollow> Log4jWebConfigurer ,请参阅 code ). P>
我只是想知道这是否也可以与log4j 2 一起使用,尽管我也不期望<context-param>也可以使用它.仍在使用web.xml文件时,您还使用log4j2吗?
问题描述
I have a Spring MVC Web App (4.0) with log4j2. This webapp uses no web.xml and takes care of configuration through Java Config. Log4j2 configuration needs to take place in an external config file, not on the classpath.
In a previous incarnation with web.xml we had
<context-param> <param-name>log4jConfiguration</param-name> <param-value>file:///path/to/log4j2.xml</param-value> </context-param>
and this worked.
In the new web.xml-less world, I tried this:
public class WebappInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { System.out.println("WebappInitializer.onStartup()"); servletContext.setAttribute("log4jConfiguration", "file:///path/to/log4j2.xml");
This does not work. It seems to happen too late and in any event doesn't work. The logs show:
ERROR StatusLogger No Log4j context configuration provided. This is very unusual. WebappInitializer.onStartup()
and no log4j logging occurs.
What is the right way to replace this context-param declaration with web.xml in a web.xml-less Spring MVC app?
UPDATE:
I can make this problem go away by adding the following web.xml:
<?xml version="1.0"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>log4jConfiguration</param-name> <param-value>file:///path/to/log4j2.xml</param-value> </context-param> </web-app>
Surely, there must be a better way!
推荐答案
I believe you can reconfigure log4j2 to a new config file during runtime like this.
LoggerContext context = (LoggerContext)LogManager.getContext(false); context.setConfigLocation(URI.create("path to file")); context.reconfigure();
Could just add this to your onStartup.
其他推荐答案
Probably this is a little too late, but you can do the following to set the log4j2 configuration to an external path:
public class ApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.setInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION, "file:///path/to/log4j2.xml");
其他推荐答案
I haven’t tested it but the following should be worth a try; replace your servletContext.setAttribute(…) call in the onStartup(…) method with the following Log4jConfigurer call:
Log4jConfigurer.initLogging("file:///path/to/log4j2.xml")
This is basically what happens under the hood when you use log4jConfigLocation in the <context-param> of a web.xml file (via Log4jWebConfigurer, see code).
I’m only wondering if this will also work with log4j2, although I wouldn’t expect the <context-param> way to work with it either. Did you also use log4j2 when you were still using the web.xml file?