在加载之前将属性传递给 log4j
有没有办法在加载之前将数据或设置传递给 log4j,然后在配置文件中使用该属性。
我假设有一个可以使用的系统属性:
log4j.appender.R.File=/usr/local/pfs/logs/${ws.host}/log4j.log
其中 ws.host 是我要使用的属性。
但我该如何设置这个值呢?
另外,我处于网络环境中。我如何知道在 log4j 加载之前什么时候设置属性设置。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认的 log4j PropertiesConfigurator 支持变量替换。
因此,您可以将像这样的系统属性
"-DmyProject.logFile="/temp/test.log"
传递给您的 Java 启动,然后在属性文件中有"log4j.appender.R.File=${myProject.logFile}"
如果在 Web 环境中工作,您可能需要查看 Spring 的 Log4jConfigListener。使用侦听器(Servlet API 2.4+)在其他组件之前初始化 log4j 即使不使用 Spring,您也应该能够使用源代码作为示例来轻松创建自己的侦听器。
The default log4j PropertiesConfigurator supports variable substitution.
So, you could pass system properties like this
"-DmyProject.logFile="/temp/test.log"
to your Java startup, and then in the properties files have"log4j.appender.R.File=${myProject.logFile}"
.If working from a web environment, you might want to check out Spring's Log4jConfigListener. It uses a listener (Servlet API 2.4+) to initialization log4j ahead of other components. Even if not using Spring, you should be able to use the source as an example to easily create your own listener.