2011-06-17 46 views
6

Android操作系統是否將系統事件記錄到某個日誌文件?如果是這樣,我在哪裏/如何讀取這個?Android系統日誌文件

背景:在我昨晚離開我的SEMC X10 Mini Pro後,我醒來發現它已關閉。我想查看一個日誌文件來嘗試解決何時以及爲什麼電話關機。

回答

3

找到this older post,這可能對你也很有趣。

+0

日誌收集器從Android Market廣場文件是完美的,謝謝。 – Michael

0

使用這個類:這將WITE登錄到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); 
    } 
}