Android 中打印日记的妙用(转发:供以后学习)
2014-10-30 00:27
288 查看
一直都对打印日记检测工程不熟练,转发此文供学习.
链接如下:http://www.cnblogs.com/jacktu/archive/2008/11/22/1339062.html 学习啦
在程序中输出日志, 使用 android.util.Log 类.
该类提供了若干静态方法
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);
分别对应 Verbose, Debug, Info, Warning,Error.
tag是一个标识,可以是任意字符串,通常可以使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件.
程序运行后 并不会在 ide的控制台内输出任何信息.
如果要后查看日志 请使用
adb logcat
关于adb的更多信息请查看官方网站.
当执行 adb logcat 后会以tail方式实时显示出所有的日志信息.
这时候我们通常需要对信息进行过滤,来显示我们需要的信息, 这时候我们指定的 tag就派上了用场.
adb logcat -s MyAndroid:I
这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息.
示例代码如下:
package com.zijun;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyAndroid extends Activity {
protected static final String ACTIVITY_TAG="MyAndroid";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
@Override
protected void onDraw(Canvas canvas) {
}
@Override
public boolean onMotionEvent(MotionEvent event) {
Log.i(MyAndroid.ACTIVITY_TAG, "=============================");
Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");
Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");
Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");
return true;
}
}
}
以上程序运行后, 在命令行执行 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 activity com.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.
链接如下:http://www.cnblogs.com/jacktu/archive/2008/11/22/1339062.html 学习啦
在程序中输出日志, 使用 android.util.Log 类.
该类提供了若干静态方法
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);
分别对应 Verbose, Debug, Info, Warning,Error.
tag是一个标识,可以是任意字符串,通常可以使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件.
程序运行后 并不会在 ide的控制台内输出任何信息.
如果要后查看日志 请使用
adb logcat
关于adb的更多信息请查看官方网站.
当执行 adb logcat 后会以tail方式实时显示出所有的日志信息.
这时候我们通常需要对信息进行过滤,来显示我们需要的信息, 这时候我们指定的 tag就派上了用场.
adb logcat -s MyAndroid:I
这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息.
示例代码如下:
package com.zijun;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyAndroid extends Activity {
protected static final String ACTIVITY_TAG="MyAndroid";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
@Override
protected void onDraw(Canvas canvas) {
}
@Override
public boolean onMotionEvent(MotionEvent event) {
Log.i(MyAndroid.ACTIVITY_TAG, "=============================");
Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");
Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");
Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");
return true;
}
}
}
以上程序运行后, 在命令行执行 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 activity com.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.
相关文章推荐
- rac 学习日记-- 一些内容主要是从 《大话 rac》里面摘抄的,便于以后记忆
- Android学习日记Activities
- 【Android学习日记】(十三) Http Get使用简单例子
- 黑马程序员_JAVA学习日记_JAVAJDK1.5以后高新技术
- Android多媒体学习日记(一)
- Android开发学习日记 -- 1.安装与HelloWorld
- android学习日记——activity生命周期二
- 【Android学习日记】(十二) QR Code条形码的产生
- 【Android学习日记】(十一) 文件存取简单例子
- android学习日记——Activity生命周期
- android学习日记(1) -- Activity生存周期
- 【Android学习日记】(四) 菜单使用例子
- 【Android学习日记】(三) 常用方法
- android_新浪微博布局学习——妙用TabHost
- 【Android学习日记】(八) Service的简单使用例子
- Android学习日记1
- 【Android学习日记】(二) 如何解决:Android中 Error generating final archive: Debug Certificate expired
- 【Android学习日记】(六) SQLite的简单实例
- android学习日记---------View 和Surfaceview
- android学习日记——activity和Intent