Java 如何在运行时访问配置的 Log4J appender?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1909871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access the configured Log4J appenders at runtime?
提问by Seth Weiner
I want to configure an appender at startup and then dynamically add and remove it from various loggers on demand. I'd prefer to have log4j configure this appender itself, and just grab a reference to it when needed. If that's not possible, I'll have to instantiate the appender myself and hold onto it.
我想在启动时配置一个 appender,然后根据需要从各种记录器中动态添加和删除它。我更喜欢让 log4j 自己配置这个 appender,并在需要时获取对它的引用。如果这是不可能的,我将不得不自己实例化附加程序并保留它。
回答by Thomas Owens
The Loggerclass has methods to getAllAppenders(), getAppender(), addAppender(), and removeAppender()methods, inherited from the Categoryclass. However, the Category class is deprecated, and on top of that, I've never tried to do this before, but this might be a useful starting point.
该记录器类有方法来getAllAppenders() ,getAppender() ,addAppender() ,和removeAppender()方法,从继承分类类。但是,不推荐使用 Category 类,除此之外,我以前从未尝试过这样做,但这可能是一个有用的起点。
回答by Alan Krueger
Appenders are generally added to the root logger. Here's some pseudocode
Appenders 通常被添加到根记录器中。这是一些伪代码
// get the root logger and remove the appender we want
Logger logger = Logger.getRootLogger();
Appender appender = logger.getAppender("foo");
logger.removeAppender(appender)
// when we want to add it back...
logger.addAppender(appender);
I'm pretty sure you can do this on other loggers than the root logger as well, though I've never tried that.
我很确定您也可以在除根记录器之外的其他记录器上执行此操作,尽管我从未尝试过。
回答by Joel
I want to do exactly the same thing. I want to configure appenders in log4j.properties and then select some and add the to the rootLogger dynamically at runtime.
我想做完全一样的事情。我想在 log4j.properties 中配置 appender,然后选择一些并在运行时动态添加到 rootLogger。
I could not figure out how to access the appenders other than via a logger to which they had been attached, so I ended up creating a dummy logger, and attaching the appenders to it so i could retrieve them dynamically. This is not ideal though as any resources used by the appenders (e.g. files) are created upfront even if they are not used.
除了通过附加的记录器之外,我无法弄清楚如何访问附加器,因此我最终创建了一个虚拟记录器,并将附加器附加到它,以便我可以动态检索它们。这并不理想,因为即使不使用附加程序使用的任何资源(例如文件)也是预先创建的。
回答by Dima
I would do it this way :
我会这样做:
- have you appender specified in log4j.properties, but not added to root logger.
- at run time, when needed, grab log4j.properties, extract from it the properties you need, instantiate your appender and set its options by reading extracted properties.
- activate the appender
- Logger.getRootLogger().addAppender(appender);
- Kick it off when finished using it - Logger.getRootLogger().removeAppender(..)
- 您是否在 log4j.properties 中指定了 appender,但未添加到根记录器中。
- 在运行时,当需要时,获取 log4j.properties,从中提取您需要的属性,实例化您的 appender 并通过读取提取的属性设置其选项。
- 激活附加程序
- Logger.getRootLogger().addAppender(appender);
- 使用完毕后启动它 - Logger.getRootLogger().removeAppender(..)
Now, if this is your own appender, doing (2) would be easy since you know the meaning of the properties and know what to expect. Otherwise you would probably want to use reflection to instantiate the class and call its property setters before doing (3).
现在,如果这是您自己的 appender,那么执行 (2) 会很容易,因为您知道属性的含义并知道会发生什么。否则,您可能希望在执行 (3) 之前使用反射来实例化类并调用其属性设置器。
回答by nLogN
If it is enabling / disabling of Appenders at run time that you want to do then I found another solution (though not very elegant). Using log4j configuration add all the Appenders you would need as you do normally.
如果要在运行时启用/禁用 Appender,那么我找到了另一个解决方案(虽然不是很优雅)。使用 log4j 配置添加您通常需要的所有 Appender。
At run time when you want to "disable" an appender add a (org.apache.log4j.spi) Filter to it that returns Filter.DENY for every log message. This way no messages make it through for this Appender. When you want to "enable" the Appender back just clear the Filter that you added above.
在运行时,当您想要“禁用”appender 时,向其添加一个 (org.apache.log4j.spi) 过滤器,为每条日志消息返回 Filter.DENY。这样就没有消息通过这个 Appender。当您想“启用”Appender 时,只需清除上面添加的过滤器即可。
I tested this and it works well for us (log4j 1.2).
我对此进行了测试,它对我们很有效(log4j 1.2)。