Android系统日志文件

时间:2011-06-17 23:05:41

标签: android

android OS是否将系统事件记录到某个日志文件中?如果是这样,我在哪里/怎么读这个?

背景:昨晚离开我的SEMC X10 Mini Pro后,我醒来发现它已关闭。我想查看一个日志文件,试着找出手机自动关机的时间和原因。

2 个答案:

答案 0 :(得分:3)

找到this older post,这对你也很有意思。

答案 1 :(得分:0)

使用此类:这将在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);
    }
}