How to configure java.util.logging on Android?

前端 未结 5 2024
星月不相逢
星月不相逢 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:00

    In case one of you only wants to route java.util.logging output from third party libraries, this can achieved pretty easily with SLF4J and its jul-to-slf4j bridge. This works for FINE and FINEST log statements, too, BTW.

    Here's the maven dependency

    
        org.slf4j
        jul-to-slf4j
        ${slf4j.version}
    
    

    To bootstrap it, put the following in your Application class, Roboguice Module or somewhere else where it gets executed before your first log statement. (Configuring this in assets/logging.propertiesseems not to work, unfortunately).

    /*
     * add SLF4JBridgeHandler to j.u.l's root logger, should be done once
     * during the initialization phase of your application
     */
    SLF4JBridgeHandler.install();
    

    You can then either

    • configure all your log statements from assets/logback.xml (using logback-android). See here for a mapping of log levels.

    • or just use SLF4J-android to forward the log statements to logcat. To do so, just put the following dependency to your classpath, no further config required:

      
          org.slf4j
          slf4j-android
          ${slf4j.version}
      
      

    I implemented this successfully using

    
        1.7.6
    
    

    Note:

    • Please read the part of the jul-to-slf4j doc that relates to performance carefully!
    • If you use the LogcatAppender make sure to keep in mind that the JUL log messages (except the ones with level finer than INFO) are routed to logcat by android. So make sure to apply appropriate filters that avoid duplicates.

提交回复
热议问题