Mic*_*ael 6 android file sd-card logcat
我想实现以下目标,但到目前为止,没有运气
在我的ApplicationClass中扩展了Application onCreate()方法,我这样做了
wwLogManager.openLogFile();
Run Code Online (Sandbox Code Playgroud)
这是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);
Run Code Online (Sandbox Code Playgroud)
我确实在SD卡中获取了日志文件,但这些文件中的日志输出没有我在活动中放置的Log.i()调用的任何痕迹.我在这里使用的logcat命令是否正确?谢谢!
小智 6
如果我误解了你的目标,我很抱歉,但也许你可以使用java.util.logging API而不是使用Logcat或Android Logging机制.
与Android日志记录API一样,java.util.logging API允许您轻松地在各种级别记录消息,例如FINE,FINER,WARN,SEVERE等.
但标准日志记录API也具有其他优势.例如,您可以使用FileHandler轻松创建日志文件.实际上,FileHandler具有内置的日志轮换机制,因此您不必担心(如此多)清理日志文件.您还可以创建Logger的层次结构; 因此,例如,如果您有两个Logger,com.example.foo和com.example.foo.bar,更改前者的日志记录级别也将更改后者的日志记录级别.如果两个Logger在不同的类中创建,这甚至可以工作!此外,通过指定日志记录配置文件,可以在运行时更改日志记录行为 最后,您可以通过实现自己的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)");
Run Code Online (Sandbox Code Playgroud)
Android日志记录机制当然简单方便.但是,它不是可定制的,并且必须使用标记进行日志过滤,这很容易变得难以处理.通过使用java.uitl.logging API,您可以避免处理大量标记,但可以轻松地将日志文件限制为应用程序的特定部分,从而更好地控制日志的位置和外观,甚至可以自定义日志记录行为在运行时.
尝试按照此处所述手动设置过滤器: http://developer.android.com/guide/developing/debugging/debugging-log.html#filteringOutput
就像是:
logcat ActivityManager:I MyApp:V *:S
Run Code Online (Sandbox Code Playgroud)
如果您将“MyApp”替换为您正在使用的日志标签,那么应该会显示来自 ActivityManager 的所有信息(和更多)日志,以及来自应用程序的所有详细(和更多)日志。