log4j support in Android

后端 未结 6 1351
無奈伤痛
無奈伤痛 2020-11-27 04:57

I am attempting to shoehorn an existing SDK onto an android device and one of the dependencies of said SDK is Apache log4j. I am able to load my test program onto the androi

6条回答
  •  时光说笑
    2020-11-27 05:37

    There is a new project, which enables log4j on android. Also using log4j over slf4j is possible. It also provides an appender for LogCat. See Logging in Android using Log4J.

    The following example shows how to configure and use log4j in Android.

    Configure the log4j system in Android

    import org.apache.log4j.Level;
    import android.os.Environment;
    import de.mindpipe.android.logging.log4j.LogConfigurator;
    /**
     * Simply place a class like this in your Android applications classpath.
     */
    public class ConfigureLog4J {
        static {
            final LogConfigurator logConfigurator = new LogConfigurator();
    
            logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "myapp.log");
            logConfigurator.setRootLevel(Level.DEBUG);
            // Set log level of a specific logger
            logConfigurator.setLevel("org.apache", Level.ERROR);
            logConfigurator.configure();
        }
    }
    

    Logging in Android using log4j using slf4j API

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class ExampleLog4JOverSLF4J {
        private final Logger log = LoggerFactory.getLogger(ExampleLog4JOverSLF4J.class);
    
        public void myMethod() {
            log.info("This message should be seen in log file and logcat");
        }
    }
    

    Logging in Android using log4j API

    import org.apache.log4j.Logger;
    
    public class ExampleLog4J {
        private final Logger log = Logger.getLogger(LogConfiguratorTest.class);
    
        public void myMethod() {
            log.info("This message should be seen in log file and logcat");
        }
    }
    

提交回复
热议问题