Logcat相关

一个Android应用程序运行后 并不会在 IDE 的控制台内输出任何信息. 不能在控制台输出。可是android提供的Log类。java

 

在程序中输出日志, 使用 android.util.Log 类. 
该类提供了若干静态方法android

Log.v(String tag, String msg); 
Log.d(String tag, String msg); 
Log.i(String tag, String msg); 
Log.w(String tag, String msg); 
Log.e(String tag, String msg);canvas

分别对应 Verbose, Debug, Info, Warning,Error.app

tag是一个标识,能够是任意字符串,一般可使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件.ide

 

若是要后查看日志 请使用工具

adb logcat网站

关于adb的更多信息请查看官方网站.ui

当执行 adb logcat 后会以tail方式实时显示出全部的日志信息.this

这时候咱们一般须要对信息进行过滤,来显示咱们须要的信息, 这时候咱们指定的 tag就派上了用场.spa

adb logcat -s MyAndroid:I

这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息.

示例代码以下:


Java代码 

[java] view plain copy

 在CODE上查看代码片派生到个人代码片

  1. package com.example.hello;      
  2.      
  3. import android.app.Activity;      
  4. import android.content.Context;      
  5. import android.graphics.Canvas;      
  6. import android.os.Bundle;      
  7. import android.util.Log;      
  8. import android.view.MotionEvent;      
  9. import android.view.View;      
  10.      
  11. public class MyAndroid extends Activity {      
  12.           
  13.     protected static final String ACTIVITY_TAG="MyAndroid";      
  14.           
  15.     @Override     
  16.     protected void onCreate(Bundle icicle) {      
  17.         super.onCreate(icicle);      
  18.         setContentView(new MyView(this));      
  19.     }      
  20.     public class MyView extends View {      
  21.         public MyView(Context c) {      
  22.             super(c);      
  23.         }      
  24.         @Override     
  25.         protected void onDraw(Canvas canvas) {      
  26.        
  27.         }      
  28.         @Override     
  29.         public boolean onMotionEvent(MotionEvent event) {      
  30.             Log.i(MyAndroid.ACTIVITY_TAG, "=============================");      
  31.                   
  32.             Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");      
  33.             Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");      
  34.             Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");      
  35.      
  36.             return true;      
  37.         }      
  38.               
  39.     }      
  40.      
  41. }    


 

以上程序运行后, 在命令行执行  adb logcat -s MyAndroid:I 

而后在手机模拟器的屏幕上 点击 拖动鼠标 就能看到相应的日志信息.

logcat是Android中一个命令行工具,能够用于获得程序的log信息。

logcat使用方法以下所示: 
logcat [options] [filterspecs]
logcat的选项包括:
  -s                    设置过滤器,例如指定 '*:s'
  -f <filename>   输出到文件,默认状况是标准输出。
  -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f
  -n <count>      Sets max number of rotated logs to <count>, default 4
  -v <format>     设置log的打印格式,  <format> 是下面的一种:
                         brief process tag thread raw time threadtime long

  -c                   清除全部log并退出
  -d                   获得全部log并退出 (不阻塞)
  -g                   获得环形缓冲区的大小并退出
  -b <buffer>     请求不一样的环形缓冲区    ('main' (默认), 'radio', 'events')
  -B                   输出log到二进制中。

过滤器的格式是一个这样的串:
  <tag>[:priority]

其中 <tag> 表示log的component, tag (或者使用 * 表示全部) , priority 以下所示:
  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent


事实上logcat的功能是由Android的类android.util.Log决定的,在程序中log的使用方法以下所示:
Log.v() -------------------- VERBOSE
Log.d() -------------------- DEBUG
Log.i() -------------------- INFO
Log.w() -------------------- WARN
Log.e() -------------------- ERROR
以上log的级别依次升高,DEBUG信息应当只存在于开发中,INFO, WARN,ERROR这三种log将出如今发布版本中。

对于JAVA类,能够声明一个字符串常量TAG,Logcat能够根据他来区分不一样的log,例如在计算器(Calculator)的类中,定义以下所示:

public class Calculator extends Activity {
/* ...... */
    private static final String LOG_TAG = "Calculator";
    private static final boolean DEBUG  = false;
    private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
/* ...... */
   由此,全部在Calculator中使用的log,均以"Calculator"为开头。

例如使用方法以下所示:
# logcat &
< 获得一个log片断 >
W/KeyCharacterMap(  130): No keyboard for id 0
W/KeyCharacterMap(  130): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
I/ActivityManager(   52): Displayed activitycom.android.contacts/.DialtactsContactsEntryActivity: 983 ms
I/ARMAssembler(   52): generated scanline__00000077:03545404_00000A04_00000000 [ 29 ipp] (51 ins) at [0x25c978:0x25ca44] in 1764174 ns
I/ARMAssembler(   52): generated scanline__00000077:03515104_00000001_00000000 [ 46 ipp] (65 ins) at [0x25d1c8:0x25d2cc] in 776789 ns
D/dalvikvm(  130): GC freed 834 objects / 81760 bytes in 63ms
D/dalvikvm(   52): GC freed 10588 objects / 425776 bytes in 94ms

其中W/I/D表示log的级别,“dalvikvm”“ARMAssembler”等是不一样组件(component)的名称,后面括号里面的数字表示了发出log的进程号。

使用技巧:
1.使用logcat &在后台运行
2.使用-d获得全部log
3.使用-f或者重定向(>和>>)输出到文件
4.使用-s设置过滤器,获得想要的log。

固然,最重要的仍是在程序中加入恰当的log.

相关文章
相关标签/搜索