【朝花夕拾】Android Log篇

时间:2022-06-28 07:14:32

前言 

转载请声明,转自【https://www.cnblogs.com/andy-songwei/p/9676823.html】,谢谢!

从事Android开发的这些年中,经常碰到这样一个现象:同一款app中,往往有好几种风格迥异的log处理方式,有时候会让维护者晕头转向。同时笔者也经常碰带一些模棱两可的问题:Log等级分好几种,到底什么情况下用哪个等级的log?什么情况下可以使用log,log怎么用,为什么要这么用?Android的log这么多,要怎么样高效地查看log?带着这些问题,笔者根据平时的开发经验、公司的log规范文档、网络中的相关资料,对log使用做了一定的整理。对于最基本的使用和log介绍,本文不做赘述,希望本文能帮助一部分人,也希望大牛们给出更牛的意见和建议,助我成长!

本文主要内容如下:

【朝花夕拾】Android Log篇

 一、Log等级划分

   1、常用Log等级

Android系统为开发者提供了良好的日志工具android.util.Log,常用的方法有如下5个,将log的输出等级也依次指定了5个级别:

(1)Log.v:这里的v代表Verbose啰嗦的意思,对应的log等级为VERVOSE。采用该等级的log,任何消息都会输出。

(2)Log.d:这里的d代表Debug调试的意思,对应的log等级为DEBUG。采用该等级的log,除了VERBOSE级别的log外,剩余的4个等级的log都会被输出。

(3)Log.i:这里的i代表information,为一般提示性的消息,对应的log等级为INFO。采用该等级的log,不会输出VERBOSE和DEBUG信息,只会输出剩余3个等级的信息。

(4)Log.w:w代表warning警告信息,一般用于系统提示开发者需要优化android代码等场景,对应的等级为WARN。该级别log,只会输出WARN和ERROR的信息。

(5)Log.e:e代表error错误信息,一般用于输出异常和报错信息。该级别的log,只会输出该级别信息。一般Android系统在输出crassh等致命信息的时候,都会采用该级别的log。

2、相关源码(基于android-26,下同)

源码android.util.Log.java对Log的级别做了比较明确的说明,也依次给出了使用方法,相关源码片段如下所示:

 /**
* API for sending log output.
*
* <p>Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
* methods.
*
* <p>The order in terms of verbosity, from least to most is
* ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled
* into an application except during development. Debug logs are compiled
* in but stripped at runtime. Error, warning and info logs are always kept.
*
* <p><b>Tip:</b> A good convention is to declare a <code>TAG</code> constant
* in your class:
*
* <pre>private static final String TAG = "MyActivity";</pre>
*
* and use that in subsequent calls to the log methods.
* </p>
*
* <p><b>Tip:</b> Don't forget that when you make a call like
* <pre>Log.v(TAG, "index=" + i);</pre>
* that when you're building the string to pass into Log.d, the compiler uses a
* StringBuilder and at least three allocations occur: the StringBuilder
* itself, the buffer, and the String object. Realistically, there is also
* another buffer allocation and copy, and even more pressure on the gc.
* That means that if your log message is filtered out, you might be doing
* significant work and incurring significant overhead.
*/
public final class Log { /**
* Priority constant for the println method; use Log.v.
*/
public static final int VERBOSE = 2; /**
* Priority constant for the println method; use Log.d.
*/
public static final int DEBUG = 3; /**
* Priority constant for the println method; use Log.i.
*/
public static final int INFO = 4; /**
* Priority constant for the println method; use Log.w.
*/
public static final int WARN = 5; /**
* Priority constant for the println method; use Log.e.
*/
public static final int ERROR = 6; /**
* Priority constant for the println method.
*/
public static final int ASSERT = 7; ......

3、源码解读

除了在注释中明确的解释说明外,我们也可以留意一下额外的信息

(1)Log.java类被final所修饰,不能被继承,没有子类,同时它也没有继承别的类,没有父类。该类的逻辑关系比较简单,容易阅读,读者有机会可以阅读源码,一定会有更深入的理解。

(2)可以看到,Log的输出等级还包括了ASSERT,用于输出的函数还包括Log.wtf(...)等,源码中也提到,一般只用上述五种级别log,对于ASSERT和Log.wtf()等就不多说,了解一下即可,平时开发也不必要使用。

(3)Log的级别依次为2~7,有一个比较奇特的现象就是,没有0和1,不是从0或1开始排等级的,至于原因,读者如果感兴趣可以研究一下。

(4)类名前的注释中也提到,传入log的字符串会耗用系统开销。所以咱们不能没有节制地使用Log,要讲究技巧和使用规范。

......

更多的信息,读者可以多多发掘!

二、Log使用规范

不同的公司,对Log的使用有不同的要求和规范,以下笔者就工作中碰到的规范来举例说明Log的使用规范(当然,从上节中的源码注释中,也能看出一些端倪来):

1、在app中,一般不允许使用VERBOSE级别的log,对于INFO、WARN级别的log,允许极少量打印重要信息。这是工作中的要求,系统源码中其实对这三个等级用得也不少,例如,系统打印一般Exception信息时,就是用的WARN级别log

2、只有在出现极严重错误的时候,才允许使用ERROR级别,一般的信息要是用DEBUG级别(在后面讲Log.isLoggable()的时候,会讲到用DEBUG级别的好处)。当系统报Fatal Exception的时候,就是用的ERROR级别的log。

3、用户的隐私信息禁止打印,比如:IMEI、手机号、密码、银行卡号等。在国外,一些法律也对Log内容做了严格的要求。

4、Log中不要打印太多具体实现的细节,这样会导致通过log就能猜到架构的设计和代码的实现。

5、Log中不能暴露核心算法或机制细节,比如核心算法相关信息、应用和框架间函数的调用流程等。

6、禁止在循环打印log。在循环条件、频繁操作、频繁调用的接口、ACTION_MOVE事件、重复打印等地方,一定要控制好log的使用。在单位时间内,不同性质的应用对log的数目有一定的要求,对每条log的大小也有一定的限制。因为大量或者频繁的log,对app的性能有一定的影响。即便是有log开关控制日志的输出与否,字符串的拼接也是会耗掉一些性能和资源的。

7、打印捕捉到的异常堆栈必须谨慎,如不需要打印堆栈就能定位问题,就尽量不要打印堆栈,若确实需要堆栈,在同一堆栈,尽量控制打印频度。

8、对于Android源码中自带的log,尽量不要修改。在Event Log中,就严禁修改源码自带的log。

9、Log中的TAG,一般以所划分的功能模块命名,log信息也最好用类名,方法名拼接为前缀。这样做的目的就是在查看log的时候,方便定位,对分析问题很有帮助。

上述不仅包含使用规范,也包含了部分log使用小技巧。这些规范中有些会根据不同公司,不同严格程度而有所不同,而有些则需要统一遵守其规范的,读者可以根据具体情况斟酌。

三、Android Studio中查看log

        Android Studio为开发者提供了良好的log查看工具,开发者可以通过如下方式打开log视图:View > Tool Windows > Logcat,或者用默认的快捷键 Alt+6 打开/隐藏 Logcat视图。下面简单介绍一下该工具的使用。

1、Logcat中选择筛选条件

如下截图中,标注了Android Studio中使用Logcat视图的常用功能,开发者可以根据实际情况选择过滤条件。

【朝花夕拾】Android Log篇

2、Log信息颜色设置

查看log的时候,有一个小技巧,为了便于查看不同等级的log,Android Studio对不同等级的log信息设置了不同的颜色。开发者也可以根据自己的爱好,自行设置颜色或者其他属性,这样,在查看log的时候,就容易对log等级进行区分,查看的时候就比较有层次感。设置路径为:File > Settings > Editor > Colors & Fonts > Android Logcat。如下截图所示:

【朝花夕拾】Android Log篇

设置完成后,用如下代码进行测试

1  private void showLog(){
2 Log.v(TAG,"Hello,I am VERBOSE");
3 Log.d(TAG,"Hello,I am DEBUG");
4 Log.i(TAG,"Hello,I am INFORMATION");
5 Log.w(TAG,"Hello,I am WARNNING");
6 Log.e(TAG,"Hello,I am ERROR");
7 }

logcat视图中打印的log信息如下:

【朝花夕拾】Android Log篇

虽然开发者可以根据自己的爱好设置log的颜色等属性,但是笔者还是建议读者尽量遵守约定俗称的约定,比如,ERROR级别的log,就往往被设置为红色。

3、Logcat中的log信息说明

如下截图为笔者打印的某条log,对其中各个字段的进行了说明

【朝花夕拾】Android Log篇

四、写一份便于使用的Log辅助类

Log的基本使用技能很容易掌握,但是要能灵活地使用在项目中,仍然有很多技巧需要掌握。

1、开发者常碰到的场景

在具体的开发中,开发者往往会遇到如下的情形:

(1)调试的时候,往往会打印不少的log,用于辅助分析问题,但是要发布给用户使用的版本时,这些log必须要关闭掉。

(2)开发者往往会在代码中设置一个变量,比如 boolean isDebug等,来控制日志的打印/关闭。但是每次发布版本的时候,都需要手动去修改这个值,操作不便,甚至容易忘记。

(3)发布给用户使用的user版本,log被关闭了,出现bug需要分析的时候,log信息太少,往往又让开发者感到“巧妇难为无米之炊”,不利于分析问题。

(4)拿到log信息后,又往往不容易找到这条信息和哪个功能有关,从哪个类,哪个方法中打印出来的。

(5)有些log需要在user版本中关闭,但有些log需要一直保留,这两类log的处理,又需要区别对待。

······

诸如此类的情形,想必开发者们都在不断地经历着。

2、辅助工具类代码

有经验的开发者一般都会写一个Log的辅助类来尽量规避这些麻烦,笔者在开发中也总结了一套代码,如下代码所示:

 package com.example.demos;

 import android.os.Build;
import android.util.Log; public class Logger {
private static final String TAG = "FunctionName";//功能模块名,比如你开发的是相机功能,这里可以命名为“Camera”,在查看log的时候,可以查看到该功能全部log
private static final boolean isLogAnyTime = true;//任何情况下都允许打印的log,无论当前手机固件版本为“user”、“userdebug”还是“eng”模式 /**
* 用于根据是否允许打印log来决定是否打印DEBUG等级的log
*
* @param moduleTag //输出该log处所在的类名
* @param methodName //输出该log处所在的方法名
* @param msg //需要输出的信息
*/
public static void d(String moduleTag, String methodName, String msg) {
if (isTagLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, createLogPrefix(moduleTag, methodName, msg));
}
} /**
* 在代码层面,任何情况下都会打印DEBUG等级的日志(在手机系统中也可以设置允许log打印的等级,这种情况另当别论)
*/
public static void alwaysShowD(String moduleTag, String methodName, String msg) {
Log.d(TAG, createLogPrefix(moduleTag, methodName, msg));
} public static void e(String moduleTag, String methodName, String msg) {
if (isTagLoggable(TAG, Log.ERROR)) {
Log.e(TAG, createLogPrefix(moduleTag, methodName, msg));
}
} public static void alwaysShowE(String moduleTag, String methodName, String msg) {
Log.e(TAG, createLogPrefix(moduleTag, methodName, msg));
} /**
* 用于打印方法的调用栈,即该函数一层一层的调用关系列表
*/
public static void printStackTraceInfo() {
if (isTagLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, Log.getStackTraceString(new Throwable()));
}
} /**
* 获取捕捉到的Exception信息,并转化为字符串
*/
public static void printExceptionInfo(Exception pEx) {
String _exStr = pEx.toString() + "\n";
StackTraceElement[] stackTraceElements = pEx.getStackTrace();
if (stackTraceElements == null) {
Log.w(TAG, _exStr);
}
for (StackTraceElement se : stackTraceElements) {
_exStr += ("at " + se.getClassName() + "." + se.getMethodName() + "(" + se.getFileName() + ":" + se.getLineNumber() + ")\n");
}
Log.w(TAG, _exStr);
} /**
* 判断当前log是否允许输出
*
* @param tag 官方:the tag to check
* @param level 官方:the level to check
* @return true 表示允许输出,false表示不允许输出
*/
private static boolean isTagLoggable(String tag, int level) {
return Log.isLoggable(tag, level) || isDebugMode() || isLogAnyTime;
} /**
* 将各个参数按照一定的格式组合,便于log查看
*
* @param moduleTag 传入所在的类名
* @param methodName 传入所在的方法名
* @param msg 要输出的信息
* @return 组合后的字符串
*/
private static String createLogPrefix(String moduleTag, String methodName, String msg) {
StringBuffer buffer = new StringBuffer();
buffer.append("[").append(moduleTag).append("]").append(methodName).append(":").append(msg);
return buffer.toString();
} /**
* 手机的系统一般有“user”、“userdebug”、“eng”版本,“user”版本是最终发给用户使用的版本,
* 而另外两种为工程师调试的版本,可以对手机做更多的操作,比如root,remount等。往往开发者
* 用于调试的大部分log,在发给用户使用时(机user版本),必须要关闭掉。
*
* @return true 表示当前手机系统版本为“eng”或者“userdebug”版本
* false表示“user”版本
*/
private static boolean isDebugMode() {
return "eng".equals(Build.TYPE) || "userdebug".equals(Build.TYPE);
}
}

注:这套代码是根据公司的log使用规范来实现的,笔者当前从事手机系统app的开发,上述的处理办法也相对偏向系统app方面,但是对于纯第三方app开发者而言,也是实用的。

3、辅助类的使用和说明。

(1)打印基本log

根据代码中的注释,想必对于这些方法的使用和含义,是很容易理解的。下面简单演示一下使用的例子

在需要打印log的地方调用 Logger.d(className,methodName,msg);即可,如下演示了输出后的log

【朝花夕拾】Android Log篇

这里提一个小技巧:对于className的获取,可以采用如下的方法(这里的TAG,就是传入的Logger.d(...)中的类名了):

public class HandleDemoActivity extends AppCompatActivity {
private static final String TAG = HandleDemoActivity.class.getSimpleName();
......
}

类名.class.getSimpleName()返回的结果就是"HandleDemoActivity",这样做最大的好处就是,如果类名有变化,这个值也会随着改变,如果采用硬编码写死这个变量,灵活性会比较差。

(2)打印函数调用栈printStackTraceInfo

以下截图展示了函数的调用栈,对于分析某个方法被调用的轨迹非常有用。第二行printStackTraceInfo()方法是最终捕捉调用栈的地方,可以清晰看到其调用轨迹。

【朝花夕拾】Android Log篇

(3)打印异常信息printExceptionInfo(Exception pEx)

该方法主要用打印捕获的Exception信息,如下截图一清晰展示地展示了异常原因,发生的地方,已经调用栈等信息。sdk也自带了e.printStackTrace()方法,由系统自己打印(截图二)。但是其打印信息被拆分为多条信息打印,在按某个tag进行搜索时,只能搜索到其中含有该tag的信息,而不能整体显示,自定义的方法就克服了这一点,便于整体查看。当然,读者可以根据自己爱好来选择是否用sdk自带的函数。

【朝花夕拾】Android Log篇

               截图一:自定义的异常打印

【朝花夕拾】Android Log篇

                       截图二:sdk自带的异常打印

(4)使用Log.isLoggable(tagName, level)

本小结中第1点第(3)条中有提到,调试版本中的log,在user版本中被关闭,这极大地妨碍了对bug的分析。所以在判断是否允许打印log的条件isTagLoggable(...)中,添加了一个“或”条件,Log.isLoggable(tag, level),就很好地解决了user版本中不能打印部分log的问题。

1)基本使用

加上这条件后,在user版本系统中,只要在命令框中执行如下命令即可:

 adb shell setprop log.tag.tagName level

命令中的tagName为辅助类中的TAG值,即FunctionName,level是指希望输出的log等级下限,比如,如果level为D,则除VERBOSE外,其他等级更高log都会输出;level为E,就只有ERROR等级log会输出。针对该辅助类的具体命令为:

 adb shell setprop log.tag.FunctionName D

输入该命令后,凡是以“FunctionName”为tag名,等级在DEBUG及以上的log,就都会输出了。要想恢复到不可打印的状态,只要重启手机即可。

2)相关源码

 /**
* Checks to see whether or not a log for the specified tag is loggable at the specified level.
*
* The default level of any tag is set to INFO. This means that any level above and including
* INFO will be logged. Before you make any calls to a logging method you should check to see
* if your tag should be logged. You can change the default level by setting a system property:
* 'setprop log.tag.&lt;YOUR_LOG_TAG> &lt;LEVEL>'
* Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
* turn off all logging for your tag. You can also create a local.prop file that with the
* following in it:
* 'log.tag.&lt;YOUR_LOG_TAG>=&lt;LEVEL>'
* and place that in /data/local.prop.
*
* @param tag The tag to check.
* @param level The level to check.
* @return Whether or not that this is allowed to be logged.
* @throws IllegalArgumentException is thrown if the tag.length() > 23
* for Nougat (7.0) releases (API <= 23) and prior, there is no
* tag limit of concern after this API level.
*/
public static native boolean isLoggable(String tag, int level);

3)源码解读

依据以上源码及注释,笔者提取了部分信息:

  • 该方法是一个native方法,通过jni在底层实现。
  • 在没有设置运行打印的tag的等级的时候,默认等级为INFO,即此时isLoggable(tag,level)中的leve如果为VERBOSE或DEBUG,isLoggable()会返回false,其它等级level则会返回true。
  • 此处可以设置的level等级更多,在前面提到的5个等级之后,还有ASSERT、SUPPRESS,设置为SUPPRESS可以关闭所有log。
  • TAG字符串长度不可太长,超过23个后,后面的会被截断。
  • 注释中也提供了修改系统文件的方法,来设置允许打印的log的等级。

4)测试代码

如下为一个测试函数

 private void testIsLoggable() {
boolean b1 = Log.isLoggable(TAG, Log.VERBOSE);
boolean b2 = Log.isLoggable(TAG, Log.DEBUG);
boolean b3 = Log.isLoggable(TAG, Log.INFO);
boolean b4 = Log.isLoggable(TAG, Log.WARN);
boolean b5 = Log.isLoggable(TAG, Log.ERROR);
Log.e(TAG, "" + b1 + ";" + b2 + ";" + b3 + ";" + b4 + ";" + b5);
Log.v(TAG,"VERBOSE log can be print");
Log.d(TAG,"DEBUG log can be print");
Log.i(TAG,"INFO log can be print");
Log.w(TAG,"WARN log can be print");
Log.e(TAG,"ERROR log can be print");
}

5)测试结果

a)不执行任何命令,测试结果为:

【朝花夕拾】Android Log篇

证明了tag默认level为INFO的结论,但是Log.v() - Log.e() 均能打印出log。

b)执行命令

adb shell setprop log.tag.HandleDemoActivity I

测试结果为:

【朝花夕拾】Android Log篇

不知道读者有没有发现,尽管默认level为INFO,此处命令设置的值也为INFO,但此时Log.v()和Log.d()的msg都没有再输出了。

c) 执行命令

adb shell setprop log.tag.HandleDemoActivity W

测试结果为:

【朝花夕拾】Android Log篇

d)结论

这里咱们也可以看到,Log.isLoggable(TAG,Log.DEBUG)的值默认是false。咱们在第二节讲Log的使用规范时的第2点中提到过,一般信息的打印用DEBUG级别log,再结合前面给出的Log辅助类,在这里可以感受到一些好处了,当然读者也可以根据自己的理解,利用这些条件设计自己得心应手的使用方法来。

以上的测试结果,我们还可以得到一个结论:adb shell setprop log.tag.tagName level 不仅会改变Log.isLoggable(tag,level)的返回值,也会影响到Log.v() - Log.e() 是否打印。读者一定要注意这些细微的差别,笔者刚开始的时候,也忽视过,也曾蒙圈过-_-!

6)推荐阅读

https://blog.csdn.net/qqxiaoqiang1573/article/details/72867776

五、log的获取

      设计好了log的输入策略,就可以获取log了。笔者接触到的获取log的方式主要有如下几种

1、开发工具中获取。

比如上文中提到的Android Studio自带的Logcat视图,同样eclipse中也有该视图,都比较好用。这种方法主要被开发者使用,测试人员一般不会使用IDE中的类似工具。

2、adb自带工具 logcat

该命令功能也比较强大,使用起来非常方便,不需要额外的IDE,电脑上配置好adb,连接上手机,在命令框中输入命令即可。该工具的命令也不少,功能也比较强大,可惜,笔者对这个功能用得不多,主要使用IDE自带工具和手机的Mobile Log。

推荐阅读:https://blog.csdn.net/liao277218962/article/details/50129009

3、手机自带抓log功能

一般手机也都自带了抓取log的工具,不同的品牌和机型,抓取系统log的方式和log的形式也不尽相同,下面以某比亚的某款机型为例来说明。

(1)在拨号盘中输入暗码(可以在网上搜,不同品牌暗码各不同,同一手机中抓取log的种类也多样)就会进入到log工具界面,如下所示:

【朝花夕拾】Android Log篇

可以看到,可以抓取的log种类非常多,咱们这里只打开MobileLog。开发者可以根据实际情况选择开启需要的log,笔者目前为止,只用到过MoboleLog,-_-

(2)在使用之前,先点击“清空”按钮清理掉之前的log文件, 以免无关log太多,影响查看有用信息。

(3)点击“开始”按钮,系统就开始抓取log了。

(4)开始操作手机,复现bug等,这段期间产生的log会被捕获到。

(5)操作完成后,点击“关闭”按钮,系统会生成日志文件,在最底部可以看到日志的存储路径,在该路径下获取即可。

【朝花夕拾】Android Log篇

六、查看及分析log

拿到日志文件后,就可以分析log了。在IDE的视图工具Logcat中,和adb logcat中获取的log,基本的查看基本上都会,这里不多说了。这里主要讲讲MobileLog中log分析。

1、文档结构

进入到log文件夹后,会看到如下的文件夹列表

【朝花夕拾】Android Log篇

如果开启了MobileLog,重启手机或暂停后重新开启,均会产生一个最新的日志文件夹。开发者从bug复现最近的一次log开始分析。选择某个时间段日志文件夹后点击,会看到如下界面

【朝花夕拾】Android Log篇

一般咱们只关注moblie文件夹的内容(笔者目前为止也只使用过该目录下的文件)。点击进入后,会显示log文件列表,如下所示:

【朝花夕拾】Android Log篇

2、分析log文件

(1)log文件概要

文件名中包含了机型、版本信息,以及文件中log的类型。一般咱们也只需要关注crash、main文件,有时候也会关注system日志文件,其使用情况如下。

  • crash文件中收集了系统中crash的log,首先分析这个文件,看是否有和自己项目相关的crash信息。
  • main文件,咱们前文中讲到的添加的log,允许打印的,都会被收集到该文件中。
  • system文件,收集系统的log,系统框架中自带的log会体现在该文件中,偶尔有需要使用。
  • 其他文件使用得不多,笔者暂时还没有碰到要使用剩余这几个文件的场景。

(2)分析crash文件log

在crash文件中,可以清晰地看到crash发生的时间,引起crash的进程及包名等信息。这里要注意crash的时间,如果和自己复现的场景时间差得比较远(比如10分钟以上),就可能和自己要分析的问题没太大的关联度。

【朝花夕拾】Android Log篇

(3)分析main文件log

在main文件中,往往包含了大量的log信息。前面讲到的logcat视图或adb logcat捕获的log,以及不同机型手机中不同类型的log,其实基本结构基本相同。单条信息中也都包含了日期、时间、进程号、线程号、log等级、TAG,msg等信息。如下图所示:

【朝花夕拾】Android Log篇

在分析这些log的时候,笔者这里提几个经常用的小技巧:

  • 选一个好用的文本编辑器。笔者和周围的同事基本上用的都是Notepad++,对查找信息非常有帮助,对于该工具的使用技巧,读者可以自己网上搜索一下。
  • 结合自己添加log的时候的设计,可以快速根据功能模块、类名、方法名等关键信息,筛选出关联度高的信息来。
  • 每一个app一般对应一个进程号,如果进程号中途变化了,说明中途该app发生了crash,可以在进程号变化点附近查找bug原因。
  • 最重要一点,对这类的log,就是要大量看。有时候看一遍可能找不出问题,就要反复看,找各种关键字搜索,必要时,甚至要逐行逐行看。就像要提升写作技能,首先必须大量去写一样,这是不二法门。

笔者对MobileLog的分析技巧也在学习和摸索中,此处慢慢积累经验,慢慢总结,慢慢更新吧。

3、源码解析

在源码中有如下的代码

     public static int v(String tag, String msg) {
return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
}
......
public static int d(String tag, String msg) {
return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
}
......
public static int i(String tag, String msg) {
return println_native(LOG_ID_MAIN, INFO, tag, msg);
}
......
public static int w(String tag, String msg) {
return println_native(LOG_ID_MAIN, WARN, tag, msg);
}
......
public static int e(String tag, String msg) {
return println_native(LOG_ID_MAIN, ERROR, tag, msg);
}
......
/** @hide */ public static final int LOG_ID_MAIN = 0;
/** @hide */ public static final int LOG_ID_RADIO = 1;
/** @hide */ public static final int LOG_ID_EVENTS = 2;
/** @hide */ public static final int LOG_ID_SYSTEM = 3;
/** @hide */ public static final int LOG_ID_CRASH = 4; /** @hide */ public static native int println_native(int bufID,
int priority, String tag, String msg);

源码中也正好给出了LOG_ID_MAIN ~ LOG_ID_CRASH 5个LOG_ID值,除了event log外,其它的也是一一对应。Log.v()~Log.e()方法的实现都调用了println_native()方法,传入其中的第一个参数bufID值也都是LOG_ID_MAIN,正好这些方法输出的log都保存在了main文件中。笔者尚未找到明确的资料来证明这其中的联系,但笔者认为,应该不是巧合,读者有兴趣可以自己再深入研究研究。另外,我们也可以发现,println_native()也是个native方法,通过jni在本地实现。

七、第三方工具

     当前在app开发生,也出现了不少比较优秀的管理log的第三方工具,笔者使用过的有两款:log4j和腾讯的bugly,都比较好用。

个人建议:使用第三方工具,就必然要导入第三方的jar包,sdk等,无疑会增加app的负载。一般来说,如果自己写的log辅助类能够轻松实现想要的需求,能不用还是别用吧。当然,个人经验来看,bugly这类功能不太容易自己实现 -_-

八、结语

        log的使用算是anroid开发中一个比较基础的技能了,也一个非常实用的技能,是开发中时时刻刻都要用到的。本文所讲的内容大多都算比较基础,当然也包含了一些平时容易忽视的知识点,基本上没有什么讲原理的地方。笔者在MobileLog分析等不少方面,经验也还比较浅,也在不断学习摸索中和总结中,希望读者们能多多指教,万分感谢!

附录

     在文章的最后附上android.util.Log.java的源码,有需要的,可以点开研读研读,在此中秋佳节来临之际,也祝愿所有读者中秋快乐。

 /*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package android.util; import android.os.DeadSystemException; import com.android.internal.os.RuntimeInit;
import com.android.internal.util.FastPrintWriter;
import com.android.internal.util.LineBreakBufferedWriter; import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.UnknownHostException; /**
* API for sending log output.
*
* <p>Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
* methods.
*
* <p>The order in terms of verbosity, from least to most is
* ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled
* into an application except during development. Debug logs are compiled
* in but stripped at runtime. Error, warning and info logs are always kept.
*
* <p><b>Tip:</b> A good convention is to declare a <code>TAG</code> constant
* in your class:
*
* <pre>private static final String TAG = "MyActivity";</pre>
*
* and use that in subsequent calls to the log methods.
* </p>
*
* <p><b>Tip:</b> Don't forget that when you make a call like
* <pre>Log.v(TAG, "index=" + i);</pre>
* that when you're building the string to pass into Log.d, the compiler uses a
* StringBuilder and at least three allocations occur: the StringBuilder
* itself, the buffer, and the String object. Realistically, there is also
* another buffer allocation and copy, and even more pressure on the gc.
* That means that if your log message is filtered out, you might be doing
* significant work and incurring significant overhead.
*/
public final class Log { /**
* Priority constant for the println method; use Log.v.
*/
public static final int VERBOSE = 2; /**
* Priority constant for the println method; use Log.d.
*/
public static final int DEBUG = 3; /**
* Priority constant for the println method; use Log.i.
*/
public static final int INFO = 4; /**
* Priority constant for the println method; use Log.w.
*/
public static final int WARN = 5; /**
* Priority constant for the println method; use Log.e.
*/
public static final int ERROR = 6; /**
* Priority constant for the println method.
*/
public static final int ASSERT = 7; /**
* Exception class used to capture a stack trace in {@link #wtf}.
*/
private static class TerribleFailure extends Exception {
TerribleFailure(String msg, Throwable cause) { super(msg, cause); }
} /**
* Interface to handle terrible failures from {@link #wtf}.
*
* @hide
*/
public interface TerribleFailureHandler {
void onTerribleFailure(String tag, TerribleFailure what, boolean system);
} private static TerribleFailureHandler sWtfHandler = new TerribleFailureHandler() {
public void onTerribleFailure(String tag, TerribleFailure what, boolean system) {
RuntimeInit.wtf(tag, what, system);
}
}; private Log() {
} /**
* Send a {@link #VERBOSE} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int v(String tag, String msg) {
return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
} /**
* Send a {@link #VERBOSE} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int v(String tag, String msg, Throwable tr) {
return printlns(LOG_ID_MAIN, VERBOSE, tag, msg, tr);
} /**
* Send a {@link #DEBUG} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int d(String tag, String msg) {
return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
} /**
* Send a {@link #DEBUG} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int d(String tag, String msg, Throwable tr) {
return printlns(LOG_ID_MAIN, DEBUG, tag, msg, tr);
} /**
* Send an {@link #INFO} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int i(String tag, String msg) {
return println_native(LOG_ID_MAIN, INFO, tag, msg);
} /**
* Send a {@link #INFO} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int i(String tag, String msg, Throwable tr) {
return printlns(LOG_ID_MAIN, INFO, tag, msg, tr);
} /**
* Send a {@link #WARN} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int w(String tag, String msg) {
return println_native(LOG_ID_MAIN, WARN, tag, msg);
} /**
* Send a {@link #WARN} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int w(String tag, String msg, Throwable tr) {
return printlns(LOG_ID_MAIN, WARN, tag, msg, tr);
} /**
* Checks to see whether or not a log for the specified tag is loggable at the specified level.
*
* The default level of any tag is set to INFO. This means that any level above and including
* INFO will be logged. Before you make any calls to a logging method you should check to see
* if your tag should be logged. You can change the default level by setting a system property:
* 'setprop log.tag.&lt;YOUR_LOG_TAG> &lt;LEVEL>'
* Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
* turn off all logging for your tag. You can also create a local.prop file that with the
* following in it:
* 'log.tag.&lt;YOUR_LOG_TAG>=&lt;LEVEL>'
* and place that in /data/local.prop.
*
* @param tag The tag to check.
* @param level The level to check.
* @return Whether or not that this is allowed to be logged.
* @throws IllegalArgumentException is thrown if the tag.length() > 23
* for Nougat (7.0) releases (API <= 23) and prior, there is no
* tag limit of concern after this API level.
*/
public static native boolean isLoggable(String tag, int level); /*
* Send a {@link #WARN} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param tr An exception to log
*/
public static int w(String tag, Throwable tr) {
return printlns(LOG_ID_MAIN, WARN, tag, "", tr);
} /**
* Send an {@link #ERROR} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int e(String tag, String msg) {
return println_native(LOG_ID_MAIN, ERROR, tag, msg);
} /**
* Send a {@link #ERROR} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int e(String tag, String msg, Throwable tr) {
return printlns(LOG_ID_MAIN, ERROR, tag, msg, tr);
} /**
* What a Terrible Failure: Report a condition that should never happen.
* The error will always be logged at level ASSERT with the call stack.
* Depending on system configuration, a report may be added to the
* {@link android.os.DropBoxManager} and/or the process may be terminated
* immediately with an error dialog.
* @param tag Used to identify the source of a log message.
* @param msg The message you would like logged.
*/
public static int wtf(String tag, String msg) {
return wtf(LOG_ID_MAIN, tag, msg, null, false, false);
} /**
* Like {@link #wtf(String, String)}, but also writes to the log the full
* call stack.
* @hide
*/
public static int wtfStack(String tag, String msg) {
return wtf(LOG_ID_MAIN, tag, msg, null, true, false);
} /**
* What a Terrible Failure: Report an exception that should never happen.
* Similar to {@link #wtf(String, String)}, with an exception to log.
* @param tag Used to identify the source of a log message.
* @param tr An exception to log.
*/
public static int wtf(String tag, Throwable tr) {
return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false);
} /**
* What a Terrible Failure: Report an exception that should never happen.
* Similar to {@link #wtf(String, Throwable)}, with a message as well.
* @param tag Used to identify the source of a log message.
* @param msg The message you would like logged.
* @param tr An exception to log. May be null.
*/
public static int wtf(String tag, String msg, Throwable tr) {
return wtf(LOG_ID_MAIN, tag, msg, tr, false, false);
} static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack,
boolean system) {
TerribleFailure what = new TerribleFailure(msg, tr);
// Only mark this as ERROR, do not use ASSERT since that should be
// reserved for cases where the system is guaranteed to abort.
// The onTerribleFailure call does not always cause a crash.
int bytes = printlns(logId, ERROR, tag, msg, localStack ? what : tr);
sWtfHandler.onTerribleFailure(tag, what, system);
return bytes;
} static void wtfQuiet(int logId, String tag, String msg, boolean system) {
TerribleFailure what = new TerribleFailure(msg, null);
sWtfHandler.onTerribleFailure(tag, what, system);
} /**
* Sets the terrible failure handler, for testing.
*
* @return the old handler
*
* @hide
*/
public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) {
if (handler == null) {
throw new NullPointerException("handler == null");
}
TerribleFailureHandler oldHandler = sWtfHandler;
sWtfHandler = handler;
return oldHandler;
} /**
* Handy function to get a loggable stack trace from a Throwable
* @param tr An exception to log
*/
public static String getStackTraceString(Throwable tr) {
if (tr == null) {
return "";
} // This is to reduce the amount of log spew that apps do in the non-error
// condition of the network being unavailable.
Throwable t = tr;
while (t != null) {
if (t instanceof UnknownHostException) {
return "";
}
t = t.getCause();
} StringWriter sw = new StringWriter();
PrintWriter pw = new FastPrintWriter(sw, false, 256);
tr.printStackTrace(pw);
pw.flush();
return sw.toString();
} /**
* Low-level logging call.
* @param priority The priority/type of this log message
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @return The number of bytes written.
*/
public static int println(int priority, String tag, String msg) {
return println_native(LOG_ID_MAIN, priority, tag, msg);
} /** @hide */ public static final int LOG_ID_MAIN = 0;
/** @hide */ public static final int LOG_ID_RADIO = 1;
/** @hide */ public static final int LOG_ID_EVENTS = 2;
/** @hide */ public static final int LOG_ID_SYSTEM = 3;
/** @hide */ public static final int LOG_ID_CRASH = 4; /** @hide */ public static native int println_native(int bufID,
int priority, String tag, String msg); /**
* Return the maximum payload the log daemon accepts without truncation.
* @return LOGGER_ENTRY_MAX_PAYLOAD.
*/
private static native int logger_entry_max_payload_native(); /**
* Helper function for long messages. Uses the LineBreakBufferedWriter to break
* up long messages and stacktraces along newlines, but tries to write in large
* chunks. This is to avoid truncation.
* @hide
*/
public static int printlns(int bufID, int priority, String tag, String msg,
Throwable tr) {
ImmediateLogWriter logWriter = new ImmediateLogWriter(bufID, priority, tag);
// Acceptable buffer size. Get the native buffer size, subtract two zero terminators,
// and the length of the tag.
// Note: we implicitly accept possible truncation for Modified-UTF8 differences. It
// is too expensive to compute that ahead of time.
int bufferSize = NoPreloadHolder.LOGGER_ENTRY_MAX_PAYLOAD // Base.
- 2 // Two terminators.
- (tag != null ? tag.length() : 0) // Tag length.
- 32; // Some slack.
// At least assume you can print *some* characters (tag is not too large).
bufferSize = Math.max(bufferSize, 100); LineBreakBufferedWriter lbbw = new LineBreakBufferedWriter(logWriter, bufferSize); lbbw.println(msg); if (tr != null) {
// This is to reduce the amount of log spew that apps do in the non-error
// condition of the network being unavailable.
Throwable t = tr;
while (t != null) {
if (t instanceof UnknownHostException) {
break;
}
if (t instanceof DeadSystemException) {
lbbw.println("DeadSystemException: The system died; "
+ "earlier logs will point to the root cause");
break;
}
t = t.getCause();
}
if (t == null) {
tr.printStackTrace(lbbw);
}
} lbbw.flush(); return logWriter.getWritten();
} /**
* NoPreloadHelper class. Caches the LOGGER_ENTRY_MAX_PAYLOAD value to avoid
* a JNI call during logging.
*/
static class NoPreloadHolder {
public final static int LOGGER_ENTRY_MAX_PAYLOAD =
logger_entry_max_payload_native();
} /**
* Helper class to write to the logcat. Different from LogWriter, this writes
* the whole given buffer and does not break along newlines.
*/
private static class ImmediateLogWriter extends Writer { private int bufID;
private int priority;
private String tag; private int written = 0; /**
* Create a writer that immediately writes to the log, using the given
* parameters.
*/
public ImmediateLogWriter(int bufID, int priority, String tag) {
this.bufID = bufID;
this.priority = priority;
this.tag = tag;
} public int getWritten() {
return written;
} @Override
public void write(char[] cbuf, int off, int len) {
// Note: using String here has a bit of overhead as a Java object is created,
// but using the char[] directly is not easier, as it needs to be translated
// to a C char[] for logging.
written += println_native(bufID, priority, tag, new String(cbuf, off, len));
} @Override
public void flush() {
// Ignored.
} @Override
public void close() {
// Ignored.
}
}
}