android源码 () - gt,Android源代码解析之(六)-->Log日志

本文介绍了Android框架中的Log类,作为不可继承的final类,如何通过关联方式实现自定义日志工具。重点讲解了六种日志级别及其用途,并探讨了日志输出的底层实现机制。此外,还提到了如何解决日志定位问题和个性化日志框架的实践分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请标明出处:一片枫叶的专栏

首先说点题外话,对于想学android framework源代码的同学,事实上能够在github中fork一份,详细地址:platform_frameworks_base

这里面基本都是android framework层的源代码了。并且近期发现了一个比較不错的github插件:OctoTree,它 是一个浏览器插件,它能够让你在Github 看代码时,左边栏会出现一个树状结构。就像我们在IDE 一样。当我们看一个项目的结构,或者想看详细的某个文件,这样就会非常方便。

b1e060cc240ece846cdc1276889c0297.png

怎么样这样查看源代码的话是不是非常方面?

好了说一下我们今天须要介绍的Log对象,它位于android framework层utils包下,是一个final class类:查看其详细定义:

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;

private Log() {

}

/**

* Send a [email protected] #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(LOG_ID_MAIN, VERBOSE, tag, msg);

}

/**

* Send a [email protected] #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 println(LOG_ID_MAIN, VERBOSE, tag, msg + ‘\n‘ + getStackTraceString(tr));

}

/**

* Send a [email protected] #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(LOG_ID_MAIN, DEBUG, tag, msg);

}

/**

* Send a [email protected] #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 println(LOG_ID_MAIN, DEBUG, tag, msg + ‘\n‘ + getStackTraceString(tr));

}

/**

* Send an [email protected] #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(LOG_ID_MAIN, INFO, tag, msg);

}

/**

* Send a [email protected] #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 println(LOG_ID_MAIN, INFO, tag, msg + ‘\n‘ + getStackTraceString(tr));

}

/**

* Send a [email protected] #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(LOG_ID_MAIN, WARN, tag, msg);

}

/**

* Send a [email protected] #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 println(LOG_ID_MAIN, WARN, tag, msg + ‘\n‘ + getStackTraceString(tr));

}

/*

* Send a [email protected] #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 println(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));

}

/**

* Send an [email protected] #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(LOG_ID_MAIN, ERROR, tag, msg);

}

/**

* Send a [email protected] #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 println(LOG_ID_MAIN, ERROR, tag, msg + ‘\n‘ + getStackTraceString(tr));

}

/**

* 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 PrintWriter(sw);

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(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 */ @SuppressWarnings("unused")

public static int println(int bufID,

int priority, String tag, String msg) {

return 0;

}

}

能够看到事实上final 类。所以我们不能通过继承Log类的方式实现自身的日志工具类,一般的我们能够通过定义Log成员变量的方式。封装Log工具方法;

在Log类中我们定义了六种日志级别,各自是:VERBOSE、DEBUG、INFO、WARN、ERROR、ASSERT等六种级别,可是我们平时使用的仅仅有前五种,即VERBOSE,DEBUG,INFO,WARN,ERROR。

通过查看源代码我们发现Log类中全部的静态日志方法Log.v(),Log.d()。Log.i(),Log.w(),Log.e()等方法都是底层都是调用了println方法,然后在github的源代码中查看,事实上其内部调用的是println_native方法。也就是通过JNI调用底层的c++输出日志;

我们临时仅仅是分析到这里。至于底层的c++日志输出的详细实现不作分析。总结一下:

Log.java是一个final类。所以我们不能够继承Log类来实现自己的日志框架。可是能够通过关联(保存Log成员变量)的方式实现自己的Log工具类;

Log.java中定义了六种日志级别。可是我们通常仅仅是使用当中的五种日志级别。分别相应着VERBOSE、DEBUG、INFO、WARN、ERROR,在详细的使用场景下详细分析;

有些同学对android自带的日志框架不太惬意。主要是无法定位日志位置,这里能够查看我写的一篇实现自己定义日志框架的文章:github项目解析(五)–>android日志框架

日志能够个性化的展示相关信息:

baa16a2c673db57b73c9812b7232b477.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值