
android-logging-log4j
<wiki:gadget url="https://android-logging-log4j.googlecode.com/svn/trunk/adsense-gadget-top.xml" width="728" height="90" />
Logging with Log4J in Android
http://mindpipe.blogspot.com/2011/11/android-log4j-exception-properties.html'>Why logging with log4j in Android is problematic
Features
Provides
* logging in Android using [log4j](http://logging.apache.org/log4j/) * support for logging with [slf4j](http://www.slf4j.org/) in Android using log4j * a **LogCatAppender** for log4j, which logs to [LogCat](http://developer.android.com/guide/developing/tools/logcat.html) * a log4j **configuration facade** class for convenient Log4J configuration * no modified _log4j.jar_ is needed ## Maven support ##For users using Maven for Android
``` de.mindpipe.android android-logging-log4j 1.0.3 ``` <wiki:gadget url="https://android-logging-log4j.googlecode.com/svn/trunk/adsense-gadget-middle.xml" width="728" height="90" /> # Quickstart example using log4j in Android # ## Log4J configuration ## ``` import java.io.File; import org.apache.log4j.Level; import android.os.Environment; import de.mindpipe.android.logging.log4j.LogConfigurator; /** * Call {@link #configure()}} from your application's activity. */ public class ConfigureLog4J { public static void configure() { final LogConfigurator logConfigurator = new LogConfigurator(); logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "myapp.log"); logConfigurator.setRootLevel(Level.DEBUG); // Set log level of a specific logger logConfigurator.setLevel("org.apache", Level.ERROR); logConfigurator.configure(); } } ```Above code configures the log4j system with some default settings e.g.
- file appender and LogCat appender
- rotating logs and corresponding backup files and file sizes
- default log output pattern
See full log4j example.
NOTE: In order to log to a file on the external storage, the following permission needs to be placed in AndroidManifest.xml.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Using log4j directly
Using log4j directly causes smaller application sizes than using it through an abstraction layer e.g. with slf4j. In case of not using slf4j, you can save potentially about 36Kb.
Prerequisites
Add the following libraries to the classpath.
- android-logging-log4j.jar
- log4j.jar (version 1.2.x)
import org.apache.log4j.Logger;
public class ExampleLog4J {
private final Logger log = Logger.getLogger(ExampleLog4J.class);
public void myMethod() {
log.info("This message should be seen in log file and logcat");
}
}
Using log4j over slf4j
- in general logging in java should be implemented by using a logging abstraction API such as slf4j
- a log4j implementation using slf4j is available
- see the slf4j documentation for details
- use android-logging-log4j to configure your log4j system and/or use the LogCatAppender for convenient logging in your development environment
- a log4j implementation using slf4j is available
Prerequisites
Add the following libraries to the classpath.
- android-logging-log4j.jar
- log4j.jar (version 1.2.x)
- slf4j-api.jar
- slf4j-log4j12.jar
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");
}
}
See full log4j over slf4j example
Motivation
- log4j.properties does not work on Android
- log4j.xml does not work on Android (at least for me)
- log4j provides good features e.g. rotating log files and appenders
- no log appender for log4j available to log to LogCat