2016-11-21 60 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复制从设备到主目录日志文件中。