6

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

Yury Pogrebnyak
  • 4,093
  • 9
  • 45
  • 78
  • if you take a naive approach of just opening a file handle and writing there - you will be writing to the file system on your Android device, while what you need is probably a file system on your dev machine? – justadreamer Dec 09 '11 at 14:01
  • Do you want to export logcat messages to a file or just write log messages in files? – SERPRO Dec 09 '11 at 14:02
  • I want to export logcat messages into file. – Yury Pogrebnyak Dec 09 '11 at 14:04
  • 3
    this library might be what you want: http://code.google.com/p/android-log-collector/ – SERPRO Dec 09 '11 at 14:07
  • 3
    check out this http://stackoverflow.com/questions/1756296/android-writing-logs-to-text-file – Michael Wang Dec 09 '11 at 14:07
  • I can paraphrase my question like "How can I access logcat on android?". I need to do it inside my application to be able to work with this data - f.e., to write it into file. Log-collector library is almost what I need, but not exactly. – Yury Pogrebnyak Dec 09 '11 at 14:23
  • Well, I suppose here is an acceptable solution http://code.google.com/p/microlog4android/ – Yury Pogrebnyak Dec 09 '11 at 14:34
  • Refer this [thread][1] [1]: http://stackoverflow.com/questions/3359692/how-to-redirect-my-log-output-from-logcat-to-the-sd-card-on-an-android-device – MGK Dec 09 '11 at 17:57
  • https://stackoverflow.com/a/63817908/8168420 try this code for the error log – kader hussain Sep 09 '20 at 19:05

4 Answers4

10

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

}
kiranpradeep
  • 10,859
  • 4
  • 50
  • 82
1

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.

jli
  • 6,523
  • 2
  • 29
  • 37
0

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

Sohan Badaya
  • 365
  • 4
  • 15
-1

Redirect all error messages to file using FileInputStream