How to enable FINE logging for a single java class

前端 未结 5 1216
温柔的废话
温柔的废话 2021-01-21 06:13

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 othe

相关标签:
5条回答
  • 2021-01-21 06:25

    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);
    }
    
    0 讨论(0)
  • 2021-01-21 06:28

    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.

    0 讨论(0)
  • 2021-01-21 06:33
    Logger log = Logger.getLogger(this.getClass().getName()).setLevel(Level.FINE);
    
    0 讨论(0)
  • 2021-01-21 06:35

    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.

    0 讨论(0)
  • 2021-01-21 06:36

    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().

    0 讨论(0)
提交回复
热议问题