• Google Android中打印日志


    在程序中输出日志, 使用 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)的日志信息.

    示例代码如下:
    Java代码 复制代码
    1. package com.zijun;   
    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
    然后在手机模拟器的屏幕上 点击 拖动鼠标 就能看到相应的日志信息.

    logcatAndroid中一个命令行工具,可以用于得到程序的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.

  • 相关阅读:
    display值的作用分别是什么?relative和absolute分别是相对谁定位的?
    CSS 选择符有哪些?哪些属性可以继承?优先级算法如何计算?
    position的absolute与fixed共同点与不同点
    .net core实践系列之SSO-同域实现
    .net core实践系列之短信服务-Sikiro.SMS.Job服务的实现
    .net core实践系列之短信服务-Sikiro.SMS.Bus服务的实现
    .net core实践系列之短信服务-Api的SDK的实现与测试
    .net core实践系列之短信服务-Sikiro.SMS.Api服务的实现
    .net core实践系列之短信服务-为什么选择.net core(开篇)
    winserver的consul部署实践与.net core客户端使用(附demo源码)
  • 原文地址:https://www.cnblogs.com/jacktu/p/1339062.html
Copyright © 2020-2023  润新知