2016-11-21 84 views
5

我正在使用slf4android(https://github.com/bright/slf4android)寫日誌,但不清楚如何閱讀它們(理想情況下,我只想將它們下載到我的電腦中)。其他應用無法訪問該應用的內部存儲空間。我可以配置slf4android登錄到共享目錄嗎?我試過這個,但我得到了:閱讀文件通過slf4android記錄?

FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this); 
File lol = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS); 
fileHandler.setFullFilePathPattern(fileHandler.toString() + "/my_log.%g.%u.log"); 
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler); 
+0

你得到了什麼錯誤? –

+0

我的答案解決了你的問題嗎? – mklimek

回答

0

在代碼示例中,您提供的實際上並未使用您定義的「File lol」。 因此,它可能會失敗,因爲您嘗試在第一個日誌上創建另一個日誌(例如,在「/sdcard/your.package/my_log.%g.%u.log/my_log.%g.%u.log」中) ;

嘗試:

File lol = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS); 
fileHandler.setFullFilePathPattern(lol.getAbsolutePath() + "/my_log.%g.%u.log"); 

但你也可以只添加一個菜單選項,點擊它會發現日誌,可以(用分貝或任何一起)和send by email它們壓縮,上傳到服務器或只是複製到另一文件夾。

1

官方的文檔說,你可以這樣做:

FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this); 

fileHandler.setFullFilePathPattern(SOMEPATH); 

LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler); 

和日誌文件將位於成SOMEPATH。我會建議你使用一個普通的環境目錄而不是任意的字符串,如

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath()+File.pathSeparator+"appLogs" 

現在,如果你希望將一些現有日誌複製到outher目的地,你可以複製文件。

if(BuildConfig.DEBUG) { 
      File logs = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath(), "logs"); 

      FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this); 
      LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler); 

      File currentLogs = fileHandler.getCurrentFileName(); 

      if (currentLogs.exists()) { 
       FileChannel src = new FileInputStream(currentLogs).getChannel(); 
       FileChannel dst = new FileOutputStream(logs).getChannel(); 
       dst.transferFrom(src, 0, src.size()); 
       src.close(); 
       dst.close(); 
      } 
     } 

最後,請記住,如果您沒有獲得正確的存儲權限,則什麼都不會有效!

希望它有幫助。快樂的編碼!

9

一旦你通過配置日誌記錄到文件:

FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this); 
fileHandler.setFullFilePathPattern("/sdcard/your.package/my_log.log"); 
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler); 

有兩個(既簡單)的方法來獲得一個日誌文件:

  1. 使用NotifyDeveloperHandler(我的最愛)

    slf4android有一個非常好的功能(出於某種原因沒有記錄),它允許發送電子郵件到給定的地址與日誌和截圖包含在附件中。

    NotifyDeveloperHandler handler = LoggerConfiguration.configuration().notifyDeveloperHandler(this, "[email protected]"); 
    handler.notifyWhenDeviceIsShaken(); 
    LoggerConfiguration.configuration().addHandlerToRootLogger(handler); 
    

這是非常方便使用(字面意思),因爲你可以通過搖晃設備觸發發送動作。

enter image description hereenter image description here

  • 使用在終端adb

    運行adb pull /sdcard/your.package/my_log.log ~/my_log.log複製從設備到主目錄日誌文件中。