以编程方式关闭 Jersey 日志记录
在我的输出中,我有 JUL 记录来自 Jersey 的消息,就像这样
03.12.2010 14:14:55 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
以编程方式我想将它们关闭,所以我尝试了
Logger.getLogger("com.sun.jersey.api.core.PackagesResourceConfig").setLevel( Level.SEVERE );
,
Logger.getLogger("com.sun.jersey").setLevel( Level.SEVERE );
但这不起作用。
有趣的是,这个全局配置有效:
Logger.getLogger( "com" ).setLevel( Level.SEVERE );
或者
Logger.getLogger( "" ).setLevel( Level.SEVERE );
为什么?
In my output I have JUL logging messages from Jersey like this
03.12.2010 14:14:55 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
Programmatically I wanted to swich them off so I tried
Logger.getLogger("com.sun.jersey.api.core.PackagesResourceConfig").setLevel( Level.SEVERE );
or
Logger.getLogger("com.sun.jersey").setLevel( Level.SEVERE );
but this don't work.
Funny enough this global configuration works:
Logger.getLogger( "com" ).setLevel( Level.SEVERE );
or
Logger.getLogger( "" ).setLevel( Level.SEVERE );
WHY?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在这方面遇到了一些问题,所以我想我应该将代码与导入一起放入以避免混淆。我对此进行了测试,它可以在我的迷你网络服务器配置中运行。再次,为了完整性,我包含了整个服务器实现。
I had some problems with this so I thought I'd put code in with imports to avoid confusion. I tested this and it works in my mini webserver configuration. Again, I included the whole server implementation for completeness.
Logger.getLogger()
返回的记录器是WeakReference
。因此,在您设置适当的级别后,它们可能会被垃圾收集,因为您不保留对其的任何引用,从而删除您的设置。将记录器存储在具有适当范围的变量中以保留您的设置。
The loggers returned by
Logger.getLogger()
areWeakReference
s. So directly after you set the appropriate level, they may get garbage collected, since you do not retain any reference to it, thus deleting your setting.Store your logger in a variable with an appropiate scope to keep your settings.
感谢上面的 John Smith,但这里是带有正确包名称的一行答案:
不需要导入,这些来自 java.util。
让这成为你们所有人在 Unix 哲学 的沉默规则中的一课:当你没有什么可说的,闭嘴吧。
Credits to John Smith above, but here is the one line answer with the right package name:
No imports necessary, these come from java.util.
And let this be a lesson to all of you in Unix Philosophy's rule of silence: when you have nothing erroneous to say, shut the hell up.
本质上每个记录器实例都有一个日志级别。但是,当您通过 Logger.getLogger() 检索记录器实例时,您很可能会创建新记录器的新实例。由于您没有保留对记录器的引用,它会立即超出范围并且您的更改会丢失。
Logger.getLogger("") 和 Logger.getLogger("com") 为您工作的原因是已经为这两个级别创建了持久记录器,这意味着您正在检索那些保持持久状态的记录器。
一个简单的修复方法是使用 JUL 中的 LogManager 类:
有一个 有关 O'reilly 的精彩文章,应该会对您有所帮助。
Essentially each logger instance has a loglevel. However, when you retrieve a logger instance via Logger.getLogger() you are more than likely creating a new instance of a new logger. Since you aren't keeping a reference to the Logger it instantly goes out of scope and your change is lost.
The reason Logger.getLogger("") and Logger.getLogger("com") work for you is that persistent loggers are already being created for those two levels, which means you are retrieving those loggers which remain persistent.
One simple fix is to use the LogManager class in JUL:
There is a great article on O'reilly that should help you.