android应用程序logcat命令
logcat命令
logcat介绍命令选项。
选项 说明 -s 默认设置过滤器 - f 文件 输出到日志文件 -c 清除日志 -d 获取日志 -g 获取日志的大小 - v 格式
设置日志(见下面的格式打印格式)
- v 格式 例 brief W/tag ( 876): message process W( 876) message (tag) tag W/tag : message thread W( 876:0x37c) message raw message time 09-08 05:40:26.729 W/tag ( 876): message threadtime 09-08 05:40:26.729 876 892 W tag : message long [ 09-08 05:40:26.729 876:0x37c W/tag ] message
代码例子:
AndroidManifest.xml添加读取权限
<uses-permission android:name="android.permission.READ_LOGS" />
清除日志
try {
Runtime.getRuntime().exec("logcat -c");
} catch(Exception e) {
}
获取日志
try {
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add( "logcat");
commandLine.add( "-d");
commandLine.add( "-v");
commandLine.add( "time");
commandLine.add( "-s");
commandLine.add( "tag:W");
Process process = Runtime.getRuntime().exec( commandLine.toArray( new String[commandLine.size()]));
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()), 1024);
String line = bufferedReader.readLine();
while ( line != null) {
log.append(line);
log.append("\n")
}
} catch ( IOException e) {
}
结果:
09-08 09:44:42.267 W/tag ( 754): message1
09-08 09:44:42.709 W/tag ( 754): message2
09-08 09:44:43.187 W/tag ( 754): message3
09-08 09:44:45.295 E/tag ( 754): message8
哎 麻烦……没用上
很好 收藏下!
没找过,你是不想找log文件的,如果是,可在logcat视图中最右边的三角形的“Export selection as text...”可导出log文件
请教个问题,android的log可以在eclipse通过代开logcat视图可以看到,在android插件的file视图中也明确能看到log文件在模拟器中的位置,可是实际却找不到,我用的os是ubuntu,还有你自己是不是也找过,没找到,才尝试自己去写这个东西。呵呵!