从战争外部加载 Log4j.xml
在我的应用程序中,我使用 Log4j 进行日志记录。目前,我将 log4j.xml 放置在 WEB-INF/classes 中。 以下是我用来加载 log4j.xml 文件的配置。
<!-- language: xml -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
现在我需要将 log4j.xml 文件放在 war 文件之外。该位置很可能是 JBOSS_HOME/server/default/deploy/settings。在设置目录中,我需要放置 log4j.xml。
我尝试通过编辑 run.bat 设置 jboss 类路径来加载它,如下所示 设置 JBOSS_CLASSPATH=%RUN_CLASSPATH%;%JBOSS_HOME%\server\default\deploy\settings 我在下面的 web.xml 中使用了它,
<!-- language: xml -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
但它在部署应用程序时抛出异常。例外的是 java.lang.IllegalArgumentException:无效的“log4jConfigLocation”参数:类路径资源[/log4j.xml]无法解析为URL,因为它不存在
现在我的问题是如何加载它。
In my application iam using Log4j for logging.Presently I am placing log4j.xml in WEB-INF/classes.
Below are the configurations i am using to load log4j.xml file.
<!-- language: xml -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Now I need to place log4j.xml file outside of my war file. The location will be most likely JBOSS_HOME/server/default/deploy/settings. In settings directory i need to place my log4j.xml.
I tried to load it by setting jboss class path by editing run.bat as follows
set JBOSS_CLASSPATH=%RUN_CLASSPATH%;%JBOSS_HOME%\server\default\deploy\settings
and i used below in web.xml
<!-- language: xml -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
But it throwing exception while deploying application. Exception isjava.lang.IllegalArgumentException: Invalid 'log4jConfigLocation' parameter: class path resource [/log4j.xml] cannot be resolved to URL because it does not exist
Now my question is how can I load it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请记住在路径前面添加“file://”,否则它将在 webapp 文件夹内搜索。
下面的例子对我有用。
Remember to add "file://" in front of the path, or else it will search inside the webapp folder.
The following example works for me.
我也有同样的例外。
我使用了这段代码:
当我执行时,这个异常消失了:
I had the same exception.
I had used this code:
This exception disappeared when I executed:
您可以简单地声明 log4j 配置的位置,如下所示:
You can simply declare location of log4j configuration like this:
您可以使用:
在您的 log4jConfigLocation 标记中。
如果路径是绝对路径(在webapp文件夹之外),请注意添加file:///
You can use:
Inside in your log4jConfigLocation tag.
Pay attention to add file:/// if the path is absolute (outside the webapp folder)
您可以在 web.xml 文件中这样设置:
注意:对于 Windows 环境,以双“/”开头,如“file://”。对于带有单个“/”的Linux环境,例如“file:/”。
You can set it like this in the web.xml file:
Note: for windows environment start with double "/" like "file://". For linux environment with sigle "/" like "file:/" .
log4j2 的语法有点不同:使用 <代码>log4jConfiguration 参数。
参考这些答案:
@rgoers
@Gowtham
The syntax for log4j2 is a little different: uses
log4jConfiguration
param.Refer to these answers:
@rgoers
@Gowtham