以编程方式设置 Logback Appender 路径
我正在尝试以编程方式设置 Logback 附加程序路径。 (确切地说,RollingFileAppender 与FixedWindowRollingPolicy)
我这样做是因为我想让我的用户能够在首选项对话框(Eclipse RCP)中设置日志路径
我尝试过类似的操作,但我没有更改配置文件中定义的日志路径:
Logger logback_logger = (ch.qos.logback.classic.Logger)LoggerFactory
.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
RollingFileAppender<ILoggingEvent> rfappender =
(RollingFileAppender<ILoggingEvent>)logback_logger.getAppender("FILE");
rfappender.setFile(newFile);
FixedWindowRollingPolicy rollingPolicy =
(FixedWindowRollingPolicy)rfappender.getRollingPolicy();
rollingPolicy.setFileNamePattern(newPattern);
I'm trying to set Logback appender path programmatically. (RollingFileAppender with FixedWindowRollingPolicy to be exact)
I'm doing this because I want to enable my users to set the log path in a preference dialog (Eclipse RCP)
I've tried something like this, but I doesn't change the log path from what's defined in the configuration file:
Logger logback_logger = (ch.qos.logback.classic.Logger)LoggerFactory
.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
RollingFileAppender<ILoggingEvent> rfappender =
(RollingFileAppender<ILoggingEvent>)logback_logger.getAppender("FILE");
rfappender.setFile(newFile);
FixedWindowRollingPolicy rollingPolicy =
(FixedWindowRollingPolicy)rfappender.getRollingPolicy();
rollingPolicy.setFileNamePattern(newPattern);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以编程方式配置附加程序后,您需要调用其
start()
方法。如果追加器有子组件,请首先在子组件上调用start()
。然后,您将附加程序添加到您选择的记录器中。下面是一个示例:
上面的代码是 logback 的 XML 配置器(即 Joran)在解析 RollingFixedWindow.xml 文件。
Once you programmatically configure your appender, you need invoke its
start()
method. If the appender has sub-components, invokestart()
on the sub-components first. You then add the appender to the logger of your choice.Here is an example:
The above code is the programmatic expression of the steps taken by the logback's XML configurator, i.e. Joran, when it parses the RollingFixedWindow.xml file.
使用系统属性并重新加载配置文件看起来更干净:
更改 logback.xml 文件:
这会将默认位置设置为工作目录。
然后,使用:
Using system properties and reloading the configuration file seems cleaner:
change the logback.xml file:
This will set the default location to the working directory.
Then, use:
查看 Logback 代码,我找到了一个解决方法:
这会导致 Logback 使用新定义。不过,这仍然感觉像是一种解决方法。
Looking at the Logback code, I have found a workaround:
This causes Logback to use the new definitions. It still feels like a workaround, though.