---
title: "android应用程序logcat命令"
author: "fonter"
date: "2009-09-25T11:37:39Z"
tags: []
slug: "android-logcat"
draft: false
lastmod: "2009-10-21T15:24:22Z"
id: 477112
url: "https://fonter.iteye.com/blog/477112"
---

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
```

[iWidsets公告](http://fonter.iteye.com/blog/498654)

[iWidsets 发布1.8.1版本(20090920)](/blog/472509)

[android之显示Log](/blog/474535)

[androd之绘制文本（FontMetrics）](/blog/474526)

[android之获取信息终端](/blog/474521)

[java多线程设计wait/notify机制 (synchronized与对象锁)](/blog/453224)

[android下的创建和读取资源文件](/blog/474584)

[android应用程序logcat命令](/blog/477112)

[android开玩笑之创建xml文件](/blog/477066)

[android view onKeyDown not call 的原因?](/blog/477054)