本文介绍了流的Andr​​oid的logcat输出到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现以下但到目前为止,没有运气

I want to achieve the following but so far, no luck

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

在我ApplicationClass扩展应用程序的onCreate()方法,我这样做

In my ApplicationClass extends Application onCreate() method, I do this

wwLogManager.openLogFile();

和这里的code在openLogFile()

and here's the code in the 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卡中的日志文件,但这些文件中的日志输出没有Log.i()的任何踪迹要求,我摆在我的活动。是,我这里使用正确的logcat的命令?谢谢!

I do get log files in the sd card, but the log output in these files do not have any trace of the Log.i() calls that I placed in my activities. Is the logcat command that I used here correct? Thanks!

推荐答案

我很抱歉,如果我误解你的目标,但也许你可以使用java.util.logging API 而不是使用的logcat还是Android日志机制。

I apologize if I am misunderstanding your goals, but perhaps you could use the java.util.logging API instead of using Logcat or the Android Logging mechanism.

像Android的日志记录API,在java.util.logging API 让您轻松记录各级,如细,细,WARN,严重等。

Like the Android logging API, the java.util.logging API allows you to easily log messages at various levels, such as FINE, FINER, WARN, SEVERE, etc.

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

But the standard logging API has additional advantages, too. For example, you can easily create a log file by using a FileHandler. In fact, FileHandler has a built-in log rotation mechanism, so you don't have to worry (so much) about cleaning up the log files. You can also create a hierarchy of Loggers; so, for example, if you have two Loggers, com.example.foo and com.example.foo.bar, changing the logging level of the former will also change the logging level of the latter. This will even work if the two Loggers are created in different classes! Moreover, you change logging behavior at runtime by specifying a logging configuration file. Finally, you can customize the format of the log by implementing your own Formatter (or just use the SimpleFormatter to avoid the default XML format).

要使用标准的记录API,你可以尝试这样的:

To use the standard logging API, you might try something like this:

    // 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,你可以避开处理大量的标签,但很容易日志文件限制为您的应用程序的特定部分,获得更大的控制权,位置和外观的日志,甚至可以自定义记录行为在运行时。

The Android logging mechanism is certainly easy and convenient. It isn't very customizable, though, and log filtering must be done with tags, which can easily become unwieldy. By using the java.uitl.logging API, you can avoid dealing with a multitude of tags, yet easily limit the log file to specific parts of your application, gain greater control over the location and appearance of the log, and even customize logging behavior at runtime.

这篇关于流的Andr​​oid的logcat输出到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:46