Android Writing Logs to text File
Hope this can help...
public void appendLog(String text)
{
File logFile = new File("sdcard/log.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
For those new to Java logging in general and Android logging
- Log4j is generic java logging implementation and is now a project of Apache software foundation. It is not Android specific and so has some incompatibilities with Android.
- SL4J is not a logging implementation, It is an abstraction layer. It helps avoid a situations like, each 3rd party library dependencies to a project, trying to using its own logging implementation like Log4j. Source.
Some options for logging to txt in Android are below
- Use
logcat -f
as in this answer to log to file. Note that from Android 4.2, READ_LOGS permission doesn't have any impact and every Application (unless phone is rooted) could only read its own logs. The disadvantage here is logcat buffer is circular and has a size limit. You might not get earlier logs. - Use microlog4android ( written for mobile devices like Android ) as in earlier answer. There could be a way, but I couldn't figure, how to use microlog4Android for logging to application internal storage. Only option for logs path was external storage like sdcard and so I couldn't use it.
-
Use Log4j with android-logging-log4j. What does android-logging-log4j do ? It makes Log4j easier to use in Android by giving two functions.
- option to send logs to logcat in addition to logging file
- a simple way to set Log4j configuration options like file path, max file size, number of backups etc by providing LogConfigurator class.
Simple example below. Notice that
logger
object in below example is a Log4j object returned and not an android-logging-log4j class. So android-logging-log4j is used only for configuring Log4j. - Yet to try LogBack. LogBack is developed by same person who came up with Log4J 1.x and SL4J libraries. Not related to Log4j 2.x though.
Steps for using Log4j in Android.
Add both log4j-1.2.x.jar and android-logging-log4j-1.0.3.jar to the libs folder.
Add permissions only if using external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
-
Write
Log4j
helper classpackage com.example.logger; import android.os.Environment; import de.mindpipe.android.logging.log4j.LogConfigurator; public class Log4jHelper { private final static LogConfigurator mLogConfigrator = new LogConfigurator(); static { configureLog4j(); } private static void configureLog4j() { String fileName = Environment.getExternalStorageDirectory() + "/" + "log4j.log"; String filePattern = "%d - [%c] - %p : %m%n"; int maxBackupSize = 10; long maxFileSize = 1024 * 1024; configure( fileName, filePattern, maxBackupSize, maxFileSize ); } private static void configure( String fileName, String filePattern, int maxBackupSize, long maxFileSize ) { mLogConfigrator.setFileName( fileName ); mLogConfigrator.setMaxFileSize( maxFileSize ); mLogConfigrator.setFilePattern(filePattern); mLogConfigrator.setMaxBackupSize(maxBackupSize); mLogConfigrator.setUseLogCatAppender(true); mLogConfigrator.configure(); } public static org.apache.log4j.Logger getLogger( String name ) { org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger( name ); return logger; } }
-
In Activity class
org.apache.log4j.Logger log= Log4jHelper.getLogger( "YourActivity" ); log.error("Error"); log.info("Info"); log.warn("Warn");
Example Source. Note that, log4j 2.x ( improved functionalities ) rewritten from scratch is not backward comptible with log4j 1.x. So you have to use log4j 1.2.x jar with android-logging-log4j jar. I was able to log to application internal file and later email the file with setReadable(true, false)
microlog4android works for me but the documentation is pretty poor. All they need to add is a this is a quick start tutorial.
Here is a quick tutorial I found.
-
Add the following static variable in your main Activity:
private static final Logger logger = LoggerFactory.getLogger();
-
Add the following to your
onCreate()
method:PropertyConfigurator.getConfigurator(this).configure();
Create a file named
microlog.properties
and store it inassets
directory-
Edit the
microlog.properties
file as follows:microlog.level=DEBUG microlog.appender=LogCatAppender;FileAppender microlog.formatter=PatternFormatter microlog.formatter.PatternFormatter.pattern=%c [%P] %m %T
-
Add logging statements like this:
logger.debug("M4A");
For each class you create a logger object as specified in 1)
6.You may be add the following permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Here is the source for tutorial