Since log files are a type of text file, they may be considered a subset of text files. The ". log" extension simply specifies that the text file contains a log of data. In fact, you can usually change the extension from ".
Print : Click to print the logcat messages. After selecting your print preferences in the dialog that appears, you can also choose to save to a PDF. Restart : Click to clear the log and restart logcat.
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
Some options for logging to txt in Android are below
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 Log4j with android-logging-log4j. What does android-logging-log4j do ? It makes Log4j easier to use in Android by giving two functions.
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.
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 class
package 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 in assets
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
Warning: I may be totally misunderstanding you, but if all you want is a log file, why sweat?
Put this in a bat file (change the path to your tools directory, and yourappname is of course your app's name):
cd "C:\devAndroid\Software\android-sdk-windows-1.6_r1\android-sdk-windows-1.6_r1\tools"
adb logcat -v time ActivityManager:W yourappname:D *:W >"C:\devAndroid\log\yourappname.log"
Then in your code just do something similar to this:
Log.d("yourappname", "Your message");
To create the log, connect the USB cable and run your bat file.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With