可以在同一个 Tomcat Web 应用程序中使用多个 log4j.properties 文件吗?

发布于 2024-09-30 13:42:37 字数 461 浏览 0 评论 0原文

我正在为现成的 Java Web 应用程序编写自定义扩展。该应用程序使用 log4j 进行日志记录,我想专门为我的扩展添加一个新的记录器和附加程序。问题在于应用程序管理 log4j.properties 文件,该文件是根据管理屏幕 UI 中的选择动态生成的。由于这是一个“现成的”应用程序,我无法修改源代码。所以,如果我添加自己的记录器 &附加到文件中,只要管理员更改 UI 中的日志首选项,它就会被覆盖。

是否可以让 log4j 从 2 个文件中获取其配置?例如,我想要如下所示的内容:

applog.properties #(Dynamically generated from admin UI)
mylog.properties  #(My static properties)

在这种情况下,log4j 会以某种方式组合两个文件中的条目以获得完整的配置。

这可能吗?或者还有其他解决方法吗?

I'm writing a custom extension to an off-the-shelf Java web application. The application uses log4j for logging and I'd like to add a new logger and appender specifically for my extension. The problem is that the application manages the log4j.properties file which is dynamically generated based on selections in an admin screen UI. Since this is an "off-the-shelf" application, I can't modify the source code. So, if I add my own logger & appender(s) to the file, it gets overwritten anytime an admin changes logging preferences in the UI.

Is it possible to get log4j to get it's configuration from 2 files? For example, I'd want something like the following:

applog.properties #(Dynamically generated from admin UI)
mylog.properties  #(My static properties)

In this scenario, log4j would somehow combine the entries from both files for the complete configuration.

Is this possible? or are there other workarounds?

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

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

发布评论

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

评论(1

最好是你 2024-10-07 13:42:37

我从未找到一种方法来“合并”多个 log4j.properties 文件,但我确实找到了一个可行的解决方案。 log4j 配置可以在运行时以编程方式进行操作,类似于下面的代码片段。这有效地将我的自定义 log4j 设置合并到 log4j.properties 文件定义的配置中,在我的情况下,我无法编辑该配置。

// Init custom logging

// Define layout
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("%d [%-5p] -- %m%n");

// Create appender
RollingFileAppender appender = new RollingFileAppender();
appender.setFile(LOG_PATH);
appender.setMaxFileSize("2MB");
appender.setMaxBackupIndex(0);
appender.setLayout(layout);
appender.activateOptions(); // It didn't work without this

// Get our logger and add appender.
log = Logger.getLogger("[MyCustomLogger]");
log.setLevel(YOUR_LOGGING_LEVEL_HERE);
log.addAppender(appender);

I never did find a way to "merge" multiple log4j.properties file, but I did find a workable solution. log4j configuration can be manipulated programatically at runtime similar to the code snippet below. This effectively merged my custom log4j settings into the configuration defined by the log4j.properties file, which in my case I couldn't edit.

// Init custom logging

// Define layout
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("%d [%-5p] -- %m%n");

// Create appender
RollingFileAppender appender = new RollingFileAppender();
appender.setFile(LOG_PATH);
appender.setMaxFileSize("2MB");
appender.setMaxBackupIndex(0);
appender.setLayout(layout);
appender.activateOptions(); // It didn't work without this

// Get our logger and add appender.
log = Logger.getLogger("[MyCustomLogger]");
log.setLevel(YOUR_LOGGING_LEVEL_HERE);
log.addAppender(appender);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文