log4j support in Android

后端 未结 6 1276
無奈伤痛
無奈伤痛 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");
        }
    }
    
    0 讨论(0)
  • 2020-11-27 05:40

    The parser for log4j configuration files is not android safe.slf4j's android compatibility thing with log4j just overrides the log4j classes you will use and forces them to use android logcat style logging. You still don't get the full flexibility of log4j on android. I ported log4j on android in my project https://sourceforge.net/projects/log4j-android/ all you have to do is add the two jars in the binaries directory to you classpath. Then

    static {
        org.apache.log4j.Logger root = org.apache.log4j.Logger.getRootLogger();
    
        final SocketAppender appender = new SocketAppender("192.168.1.4", 4445);
    
    
        root.addAppender(appender);
    }
    
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MyClass.class);
    
    static {
        logger.info("Hello logger");
    }
    

    This will start sending out messages to the remote host you specified. You can then see this messages with Chainsaw http://logging.apache.org/log4j/docs/webstart/chainsaw/chainsawWebStart.jnlp. To make chainsaw work click the second check box on the dialog that pops up hit ok, start your app and a new tab should appear. Be aware your firewall might block it...

    0 讨论(0)
  • 2020-11-27 05:45

    I would recommend trying to swap in slf4j in place of log4j. It's not a painless switch but its likely to be easier than what you have. slf4j provides a common front-end for several loggers including log4j and there is an slf4j-android package.

    No, Android's logging mechanism is not decent. It's very inadequate compared to what log4j can do for you.

    0 讨论(0)
  • 2020-11-27 05:50

    I successfully got log4j working on android with a Socket Appender and Log4j Chainsaw. All code is located in this repository. Slf4j set up and working too. Be aware you have to configure it programmatically. You cannot use .properties or .xml files the parser wont work on android. Enjoy.

    https://sourceforge.net/projects/log4j-android/

    0 讨论(0)
  • 2020-11-27 05:53

    Actually using slf4j turned out a remarkably painless process for me, and it seems the common case, at least for libraries that use straightforward log4j features. You don't really need to swap slf4j in for log4j, only add two slf4j libraries to your project from http://www.slf4j.org/download.html:

    -- the slf4j library for Android (currently slf4j-android-1.6.1-RC1.jar)
    -- the log4j over slf4j (http://www.slf4j.org/legacy.html#log4j-over-slf4j) bridge.

    The latter defines the core log4j classes used by typical implementations and bind them to the slf4j Android implementation. Once the libraries are added the code works.

    0 讨论(0)
  • 2020-11-27 05:53

    Check out this project for a complete implementation: http://code.google.com/p/log4j-android/

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