Android writes a log to a file

By default, the log class in android writes logs to the console (logcat). Is there an easy way to write fe logs in some kind of file?

+5
source share
4 answers

A simple class Log(only for development / testing), similar android.util.Log, but written to sdcard. Only a modification of the existing code will consist of a change import android.util.Logto import com.example.Log. Also add permission as

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

package com.example.logger;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;  

import android.os.Environment;

public class Log {
    private static final String NEW_LINE =  System.getProperty("line.separator") ; 
    public static boolean mLogcatAppender = true;
    final static File mLogFile;

    static {
        mLogFile = new File( Environment.getExternalStorageDirectory(),  "logs.log" ); 
        if ( !mLogFile.exists() ) {
            try {
                mLogFile.createNewFile();
            }
            catch (final IOException e) {
                e.printStackTrace();
            }
        }
        logDeviceInfo();
    }

    public static void i( String TAG, String message ){
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.i( TAG, message );
        }
    }

    public static void d( String TAG, String message ){
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.d( TAG, message );
        }
    }

    public static void e( String TAG, String message ){
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.e( TAG, message );
        }
    }

    public static void v(String TAG, String message ){
        appendLog(TAG + " : " + message);
        if ( mLogcatAppender ) {
            android.util.Log.v( TAG, message );
        }
    }

    public static void w( String TAG, String message ) {
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.w( TAG, message ); 
        }
    }

    private static synchronized void appendLog( String text ) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        try {
            final FileWriter fileOut = new FileWriter( mLogFile, true );
            fileOut.append( sdf.format(new Date()) + " : " + text + NEW_LINE ); 
            fileOut.close();
        }
        catch ( final IOException e ) {
            e.printStackTrace();
        }
    }

    private static void logDeviceInfo() {
        appendLog("Model : " + android.os.Build.MODEL);
        appendLog("Brand : " + android.os.Build.BRAND);
        appendLog("Product : " + android.os.Build.PRODUCT);
        appendLog("Device : " + android.os.Build.DEVICE);
        appendLog("Codename : " + android.os.Build.VERSION.CODENAME);
        appendLog("Release : " + android.os.Build.VERSION.RELEASE);
    }

}
+3
source

If this is for dev purposes only, you can call logcat on the device and redirect its output to a file. For example: adb shell logcat > log.txt.

stdout stderr , iirc , , .

, , , .

+1

FileInputStream

0

Eclipse ddms .

0

All Articles