在加载之前将属性传递给 log4j

发布于 2024-09-28 18:42:03 字数 243 浏览 0 评论 0 原文

有没有办法在加载之前将数据或设置传递给 log4j,然后在配置文件中使用该属性。

我假设有一个可以使用的系统属性:

log4j.appender.R.File=/usr/local/pfs/logs/${ws.host}/log4j.log

其中 ws.host 是我要使用的属性。

但我该如何设置这个值呢?

另外,我处于网络环境中。我如何知道在 log4j 加载之前什么时候设置属性设置。

Is there a way to pass data or setting to log4j before it loads and then use that property within the config file.

I was assuming there is a system properties I could use:

log4j.appender.R.File=/usr/local/pfs/logs/${ws.host}/log4j.log

Where ws.host is the property I want to use.

But how can I set that value?

Also, I am in a web environment. How can I know at what point to set the property setting before log4j loads.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2024-10-05 18:42:03

默认的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文