【问题标题】:Reading file logged via slf4android?读取通过 slf4android 记录的文件?
【发布时间】:2017-04-05 09:25:53
【问题描述】:

我正在使用 slf4android (https://github.com/bright/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);

【问题讨论】:

  • 你遇到了什么错误?
  • 我的回答解决了你的问题吗?

标签: android logging slf4j slf4android


【解决方案1】:

在您提供的代码示例中,您实际上并未使用您定义的“文件大声笑”。 所以它可能会失败,因为您尝试在第一个日志之上创建另一个日志(例如在“/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");

但是您也可以添加一个菜单选项,单击它会找到日志,可以将它们压缩(连同 db 或其他)和send by email,上传到服务器或只是复制到另一个文件夹。

【讨论】:

    【解决方案2】:

    官方文档说你可以这样做:

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

    并且日志文件将位于 SOMEPATH 中。我建议您使用常规环境目录而不是任意字符串,例如

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

    现在,如果您想将一些现有日志复制到外部目的地,您只需复制文件即可。

    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();
                }
            }
    

    最后,请记住,如果您没有获得适当的存储权限,任何事情都不会奏效!

    希望对您有所帮助。编码愉快!

    【讨论】:

      【解决方案3】:

      一旦您通过以下方式配置日志记录到文件:

      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, "example@gmail.com");
        handler.notifyWhenDeviceIsShaken();
        LoggerConfiguration.configuration().addHandlerToRootLogger(handler);
        

      使用起来非常方便(字面意思),因为您可以通过摇晃设备来触发发送操作。

      1. 使用adb

        在终端运行adb pull /sdcard/your.package/my_log.log ~/my_log.log,将日志文件从设备复制到主目录。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-11
        • 1970-01-01
        • 1970-01-01
        • 2017-03-15
        • 1970-01-01
        相关资源
        最近更新 更多