Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android writing log to the file

By default, Log class in android writes logs to console (logcat). Is there any simple way to write logs f.e. in some file?

like image 316
Yury Pogrebnyak Avatar asked Dec 09 '11 13:12

Yury Pogrebnyak


People also ask

Is there a log file in Android?

Android allows collecting system logs using Logcat. Log messages can be viewed in a Logcat window in Android Studio, or you can use the command line tool to pull them. Several Android apps are also available in the Google Play store that allow easy access to these tools.

Where are Android logs stored?

To access the logging output, run the 'adb' executable with following arguments to capture the Android Enterprise related logging: Windows: C:\Users\[username]\AppData\Local\Android\sdk\platform-tools> adb logcat -G 32M; adb shell setprop persist. log.


1 Answers

A simple Log class (development/testing purpose only) similar to android.util.Log, but logs to sdcard. Only modification to the existing code will be to change the import android.util.Log to 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);
    }

}
like image 90
kiranpradeep Avatar answered Sep 27 '22 17:09

kiranpradeep