How to configure java.util.logging on Android?

前端 未结 5 2021
星月不相逢
星月不相逢 2020-12-04 11:49

I want to use java.util.logging on Android. I want to configure the logging system with logging.properties. But how can I tell Android using the specific configure file? For

5条回答
  •  青春惊慌失措
    2020-12-04 12:15

    Here is modified answer of Christian Bauer

    If somewhere in any third party lib you will see something like this:

       final public class AnyClass {        
            private static final Logger logger = Logger.getLogger(AnyClass.class.getName());
             ...
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("any log event");
                }
             ...
         }
    

    Add to your code this after creating first instance of AnyClass.class:

    SetupLogger.setup(Arrays.asList(
                    new Pair<>(AnyClass.class.getName(), Level.FINEST),//AnyClass.class.getName() is because instance `logger` has his name
                    new Pair<>(AnyOther.class.getName(), Level.FINEST)
            ));
    

    SetupLogger .class

    public class SetupLogger {
    
        static public void setup(List> loggerNames) {
            // suppress the logging output to the console
            AndroidLoggingHandler rootHandler = new AndroidLoggingHandler();
            AndroidLoggingHandler.reset(rootHandler );
            for (Pair pair : loggerNames) {
                Logger logger = Logger.getLogger(pair.first);
                logger.setLevel(pair.second);
            }
        }
    }
    

提交回复
热议问题