Yury Pogrebnyak
Yury Pogrebnyak

Reputation: 4173

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?

Upvotes: 6

Views: 9080

Answers (4)

kiranpradeep
kiranpradeep

Reputation: 11201

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);
    }

}

Upvotes: 10

Sohan Badaya
Sohan Badaya

Reputation: 365

In Eclipse ddms there is a option to save logs into file.

Upvotes: 0

jli
jli

Reputation: 6623

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

Alternatively, you could try redirecting stdout and stderr to a file, iirc this will work but I don't have my phone with me to test it.

However, it would be easier to just make your own basic logging class that does the same thing as the built in one but also saves to a file.

Upvotes: 1

user1087919
user1087919

Reputation:

Redirect all error messages to file using FileInputStream

Upvotes: -1

Related Questions