slf4j/log4j:当控制台不合适时禁用控制台输出
这里的问题是,我在 JVM B 中执行的类是我有时在调试器中运行的类,在调试器中获得控制台输出很有帮助。
但是,如果我使用 ProcessBuilder 运行 JVM B,那么我不需要控制台输出,因为这需要 JVM A 读取进程的输出,否则 JVM B 将挂起。
有没有一种方法可以从 JVM B 内部控制 slf4j 或 log4j,以便如果 JVM 中的主类认为不适合使用控制台进行日志记录,它就不会尝试这样做?(例如禁用基于控制台的附加程序)我宁愿不必维护单独的 log4j.configuration 文件,尽管如果需要的话我会这样做。
例如,在我的主类中,
static {
if (shouldntUseConsole())
{
?????
}
}
我可以弄清楚如何实现 shouldntUseConsole()
,但我不知道要为 ??????
放置什么。
I'm starting a JVM programmatically with ProcessBuilder. For clarity let's call the JVM that uses ProcessBuilder JVM A and the one it starts JVM B. JVM B uses slf4j/log4j for logging.
The issue here is that the class I execute in JVM B is one that I sometimes run in a debugger, where it's helpful to have console output.
If I run JVM B with ProcessBuilder, however, then I don't want console output, because that requires JVM A to read the process's output or JVM B hangs.
Is there a way to control slf4j or log4j from within JVM B so that if my main class in JVM deems it's inappropriate to use a console for logging, it doesn't try to do so? (e.g. it disables console-based appenders) I would rather not have to maintain separate log4j.configuration files, although I'll do that if I need to do so.
e.g. in my main class
static {
if (shouldntUseConsole())
{
?????
}
}
I can figure out how to implement shouldntUseConsole()
, but I don't know what to put for ?????
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,slf4j是一个API,背后有一个实现,这里是log4j,所以它是你需要配置的实现,而不是slf4j。
具有不同日志行为的通常方法是具有不同的配置文件。对于 log4j,您可以为 A 和 B 使用不同的文件集,即两个不同的 log4j.properties 文件,或者您可以使用一个配置将“log4j.configuration”系统属性设置为另一个配置文件的名称。
请参阅 http://logging.apache.org/log4j/1.2 中的“默认初始化过程”部分/manual.html 了解详细信息。
First of all, slf4j is an API with an implementation behind it, which here is log4j so it is the implementation you need to configure, not slf4j.
The usual way to have different logging behaviors is to have different configuration files. For log4j you can either have different file sets for A and B, with two different log4j.properties files, or you can have one configuration set the "log4j.configuration" system property to the name of another configuration file.
See "Default initialization procedure" section in http://logging.apache.org/log4j/1.2/manual.html for details.
您可以将两个 log4j 文件与属性配置器对象一起使用。您的代码看起来像这样:
这是适当的 javadoc:
http://logging.apache.org/log4j/1.2/apidocs/index.html
You could use two log4j files with the property configurator object. Your code would look something like this :
Here is the appropriate javadoc:
http://logging.apache.org/log4j/1.2/apidocs/index.html