每次战争指定外部log4j2.xml文件
我有一个部署4个单独的战争文件的应用程序,每个文件都需要一个自定义的log4j2.xml文件(这使使用-dlog4j.configurationLocation = ______
不切实际)。
我需要做的是能够以每次战争的基础配置这些文件,在名为/application/config/config/
的文件系统位置中。这应该可以通过Servlet初始化器这样做:
@Override
public void onStartup(ServletContext servletContext) {
// other code
servletContext.setInitParameter("log4jConfiguration", "file:/application/config/log4j2-module1.xml");
// other code
}
我已经在POM中添加了log4j-web
,但我仍然没有运气。我仔细研究了所有其他人都问类似问题,什么也没有。我还创建了一个专门用于初始化log4j的侦听器,但仍然没有。
谁能指出我在做什么错?这是一个弹簧(但不是春季启动)应用程序,因此我不能仅更改application.properties
。
I have an application that deploys 4 separate war files, each requiring a customized log4j2.xml file (this makes using -Dlog4j.configurationLocation=_____
impractical).
What I need to do is to be able to configure these on a per-WAR basis, in a filesystem location named /application/config/
. This should be possible via the servlet initializer as such:
@Override
public void onStartup(ServletContext servletContext) {
// other code
servletContext.setInitParameter("log4jConfiguration", "file:/application/config/log4j2-module1.xml");
// other code
}
I have added log4j-web
to the POM, but I'm still having no luck. I've looked through all of the other people asking similar questions, and nothing. I've also created a Listener specifically to initialize log4j, and still nothing.
Can anyone point out what I'm doing wrong? This is a Spring (but not Spring Boot) application, so I can't just make a change to application.properties
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看
web-fragment.xml
来自log4j-web
,您会注意到它是在其他任何片段之前订购的。这是有意的:日志记录通常是您需要初始化的第一份服务。这就解释了为什么您的WebApplicationInitializer
s执行得太晚。要配置多个Web应用程序,我将在
Web> Web> Web-Inf
文件夹或应用程序的Class Pather中添加一个称为log4j2> .xml
上下文名称> .xml 的文件。上下文名称是:
log4jcontextName
servlet参数,< display>< display-name>
,如果要遵循不同的约定,则可以在应用程序的classPath上添加
log4j2.configurationFile
属性属性属性属性属性。If you look at the
web-fragment.xml
fromlog4j-web
, you'll notice that it is ordered before any other fragments. This is intentional: logging is usually the first service you need to initialize. That explains why yourWebApplicationInitializer
s are executed too late.To configure multiple web applications I would use "convention over configuration" and add a file called
log4j2<context name>.xml
in either theWEB-INF
folder or the application's classpath.The context name is the first non empty value between:
log4jContextName
servlet parameter,<display-name>
,If you want to follow a different convention, you can add a
log4j2.configurationFile
property to alog4j2.component.properties
file on the application's classpath.