How to enable FINE logging for a single java class

2019-08-12 02:14发布

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?

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

标签: java logging
5条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-12 02:24
Logger log = Logger.getLogger(this.getClass().getName()).setLevel(Level.FINE);
查看更多
聊天终结者
3楼-- · 2019-08-12 02:32

I believe that you can set your log level for your Handler and your specific class Logger to FINE, while keeping all the other Loggers for the rest of your code base to WARNING should do the trick. Both the Logger and the Handler need to pass the level filter for a message to be logged.

查看更多
该账号已被封号
4楼-- · 2019-08-12 02:45

Well, here's a method I added to my mail class that's actually working. I would still welcome any improvements from others.

private static void setupLogging() {
  // To enable FINE logging in a single class, apparently this bewildering
  // maze of statements is required.
  Logger.getLogger("").setLevel(Level.FINE);
  for (Handler handler : Logger.getLogger("").getHandlers()) {
    handler.setLevel(Level.FINE);
  }
  MyOtherClass.logger.setLevel(Level.FINE);
  Logger.getLogger("").setLevel(Level.WARNING);
}
查看更多
混吃等死
5楼-- · 2019-08-12 02:47

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.

Caveat: I thought it was worthy of inclusion as the header doesn't indicate programatically and many developers will want to manage it through the logging.properties. Also there isn't really a lot on-line about this, it can be confusing and is slightly different to, say log4j

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.

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

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

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.

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

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

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

查看更多
放我归山
6楼-- · 2019-08-12 02:48

If you do not want to have a logger defined for every single class in question but rather want to share loggers between classes, you can alternatively implement your own java.util.logging.Handler that has its own way of filtering for class names using the information provided by LogRecord.getSourceClassName().

查看更多
登录 后发表回答