6

Does android OS log system events to a log file somewhere? If so, where/how can I read this?

Background: After leaving my SEMC X10 Mini Pro on charge last night, I wake up to find it was off. I'd like to look in a log file to try and work out when and why the phone switched itself off.

Michael
  • 63
  • 1
  • 1
  • 3

2 Answers2

3

Found this older post, this might be interesting for you, too.

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
0

Use this class: This will wite log to file on sd

public class Logger {

    public static final String APP_ID = "Android APP";
    public static String logDir = "/androidapp";
    public static String logFileName = "/log.txt";
    public static boolean writeLogsToFile = false;
    public static final int LOG_LEVEL_VERBOSE = 4;
    public static final int LOG_LEVEL_DEBUG = 3;
    public static final int LOG_LEVEL_INFO = 2;
    public static final int LOG_LEVEL_ERROR = 1;
    public static final int LOG_LEVEL_OFF = 0;
    public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG;

    public static void log(String message, int logLevel) {
        if (logLevel > CURRENT_LOG_LEVEL) {
            return;
        } else {
            Log.v(APP_ID, message);
            if (writeLogsToFile) {
                writeToFile(message);
            }
        }
    }

    private static void writeToFile(String message) {
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + logDir);
            dir.mkdirs();
            File file = new File(dir, logFileName);
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024));
            writer.println(APP_ID + " " + new Date().toString() + " : " + message);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void verbose(String message) {
        log(message, LOG_LEVEL_VERBOSE);
    }

    public static void debug(String message) {
        log(message, LOG_LEVEL_DEBUG);
    }

    public static void error(String message) {
        log(message, LOG_LEVEL_ERROR);
    }

    public static void info(String message) {
        log(message, LOG_LEVEL_INFO);
    }
}
Mahesh
  • 2,862
  • 2
  • 31
  • 41