android - 将 android logcat 输出流式传输到 SD 卡

标签 android file sd-card logcat

我想实现以下目标,但到目前为止,运气不好

  • 在 android 应用程序首次启动时打开 SD 卡中的文件。
  • 将 logcat 输出流式传输到文件。
  • 当应用程序退出时,停止 logcat 流。

在我的 ApplicationClass extends Application onCreate() 方法中,我这样做了

wwLogManager.openLogFile();

这是 openLogFile() 中的代码

        private static final String LOGCAT_COMMAND = "logcat -v time -f ";
        String timestamp = Long.toString(System.currentTimeMillis());
        String logFileName = BuildConstants.LOG_LOCATION + "_" + timestamp + ".log";                    
        mLogFile = new File(Environment.getExternalStorageDirectory() + logFileName);
        mLogFile.createNewFile();
        String cmd = LOGCAT_COMMAND + mLogFile.getAbsolutePath();
        Runtime.getRuntime().exec(cmd);

我确实在 SD 卡中获取日志文件,但这些文件中的日志输出没有任何我在 Activity 中调用 Log.i() 的痕迹。我在这里使用的 logcat 命令是否正确?谢谢!

最佳答案

如果我误解了您的目标,我深表歉意,但也许您可以使用 java.util.logging API而不是使用 Logcat 或 Android 日志记录机制。

与 Android 日志记录 API 一样,java.util.logging API允许您轻松地记录各种级别的消息,例如 FINE、FINER、WARN、SEVERE 等。

但是标准的日志 API 也有额外的优势。例如,您可以使用 FileHandler 轻松创建日志文件 .事实上,FileHandler有一个内置的日志轮转机制,所以你不必担心(那么多)清理日志文件。您还可以创建 Logger 的层次结构;所以,例如,如果你有两个 Logger s、com.example.foocom.example.foo.bar,改变前者的日志级别也会改变后者的日志级别。如果两个 Logger 这甚至会起作用s 是在不同的类中创建的!此外,您可以通过指定日志记录配置文件在运行时更改日志记录行为。最后,您可以通过实现自己的 Formatter 来自定义日志的格式。 (或者只使用 SimpleFormatter 来避免默认的 XML 格式)。

要使用标准日志记录 API,您可以尝试如下操作:

    // Logger logger is an instance variable
    // FileHandler logHandler is an instance variable

    try {
        String logDirectory =
            Environment.getExternalStorageDirectory() + "/log_directory";

        // the %g is the number of the current log in the rotation
        String logFileName = logDirectory + "/logfile_base_name_%g.log";

        // ...
        // make sure that the log directory exists, or the next command will fail
        // 

        // create a log file at the specified location that is capped 100kB.  Keep up to 5 logs.
        logHandler = new FileHandler(logFileName, 100 * 1024, 5);
        // use a text-based format instead of the default XML-based format
        logHandler.setFormatter(new SimpleFormatter());
        // get the actual Logger
        logger = Logger.getLogger("com.example.foo");
        // Log to the file by associating the FileHandler with the log
        logger.addHandler(logHandler);
    }
    catch (IOException ioe) {
        // do something wise
    }

    // examples of using the logger
    logger.finest("This message is only logged at the finest level (lowest/most-verbose level)");
    logger.config("This is an config-level message (middle level)");
    logger.severe("This is a severe-level message (highest/least-verbose level)");

Android 日志记录机制当然简单方便。不过,它不是很可定制,而且日志过滤必须使用标签来完成,这很容易变得笨拙。通过使用 java.uitl.logging API,您可以避免处理大量标记,但可以轻松地将日志文件限制为应用程序的特定部分,更好地控制日志的位置和外观,甚至可以在运行时自定义日志记录行为。

关于android - 将 android logcat 输出流式传输到 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6970169/

相关文章:

java - 关闭另一个类(class)的 Activity

android - 在 android 中发送短信作为服务

c - C中通过特定方式读取和更改文件信息

我可以使用标准库在 C 中寻找超过 2GB 的位置吗?

android - 带有复选框行的 ExpandableListView OnChildClickListener 在滚动时不会触发

android - 如何从保管箱中的特定文件夹中获取文件

python - "rb+"和 "wb+"有什么区别?

android - 在 Android 应用程序中解压缩 sd 卡上的压缩文件

raspberry-pi - 树莓派 CM3 + 额外的 sd 卡

java - 无法将列表 <> 保存到 SD 卡