如何为单个Java类启用FINE日志记录 [英] How to enable FINE logging for a single java class

查看:36
本文介绍了如何为单个Java类启用FINE日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 java.util.logging.Logger 登录我的程序.如何为单个类启用 FINE 日志记录,同时为其他每个类将其设置为 WARNING ?

I'm using java.util.logging.Logger logging in my program. How do I enable FINE logging for a single class, while setting it to WARNING for every other class?

我宁愿以编程方式在我的 main()方法中执行此操作,而不需要设置其他属性文件.

I'd prefer to do this programatically in my main() method rather than needing to set up additional properties files.

推荐答案

我知道OP要求以编程方式执行此操作,但这也是在属性文件中执行此操作的示例.

I know the OP has asked to do this programatically but here's an example of how to do it in the properties file too.

注意事项:我认为值得将其包含在内,因为标头不能以编程方式表示,并且许多开发人员都希望通过logging.properties对其进行管理.另外,关于此的在线讨论并不多,可能会令人困惑,并且与log4j

根日志记录级别由 .level 配置指示.这指示了默认情况下要捕获和分发"事件.记录.根记录级别是根记录器"使用的级别.在日志记录层次结构中.参见这篇Java文章可提供有关日志记录层次结构的更多信息.

The root logging level is indicated by the .level config. This dictates which events are by default to be captured and "distributed for" logging. The root logging level is the level used by the "root logger" in the logging hierarchy. See this onjava article for more info on the logging hierarchy.

以下,根日志级别设置为WARNING,因此通常只捕获WARNING事件.除非另行配置(稍后),否则层次结构中的所有子记录器都将继承该记录:

Below, the root log level is set to WARNING so will ordinarily capture only WARNING events. This is inherited by all child loggers in the hierarchy, unless you configure otherwise (later):

.level=WARNING

此根日志记录级别仅指示已捕获的内容,而不是分布式的"内容.捕获的事件(消息)的分发方式取决于与记录器关联的处理程序.例如,ConsoleHandler会将事件输出到控制台.例如:

This root-logging level only indicates what is captured, not what is "distributed". How a captured event (message) is distributed is down to the handlers associated with the logger. For instance, a ConsoleHandler will output the event to the console. For instance:

java.util.logging.ConsoleHandler.level = WARNING

此ConsoleHandler.level指示此处理程序应为之分发(或打印)消息的级别.因此,如果使用上述配置接收到FINE消息,则此处理程序将不会打印它.但是它将打印警告日志级别或更高级别的任何消息.

This ConsoleHandler.level indicates the level for which this handler should distribute - or print - the message. So, if a FINE message is received with the above config then this handler will not print it. It will print any messages with a WARNING log level or above though.

设置为ALL将确保ConsoleHandler将所有消息打印到控制台(我们还需要配置根级别以确保捕获所有消息):

Setting to ALL will ensure that the ConsoleHandler will print all messages to the console (an we also need to configure the root level to ensure all are captured):

.level=ALL
java.util.logging.ConsoleHandler.level = ALL

但是,这会产生很多我们也不想要的噪音.因此,为了将FINE级别的事件减少到我们感兴趣的类,我们仅更改那些特定记录器的记录级别:

However, this would create a lot of noise which we also don't want. So, to reduce the FINE-level events to those classes we're interested in, we change the logging level of those specific loggers only:

com.level = WARNING
com.mypackage.MyClass1.level = FINE
com.mypackage.MyClass2.level = FINE
com.mypackage.mysubpackage.MyClass3.level = FINE

请注意,在上文中,我已经明确设置了"com"的级别.记录到警告.

Note that in the above, I've explicitly set the level for the "com" logger to WARNING.

这篇关于如何为单个Java类启用FINE日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆