• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > android源码解析之(五)--)Log相关介绍

android源码解析之(五)--)Log相关介绍

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android log日志,android log输出,android log.i, android log print,android log.d等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

android源码解析之(五)--)Log相关介绍


这里面基本都是android framework层的源码了。而且最近发现了一个比较不错的github插件:OctoTree,它 是一个浏览器插件,它可以让你在Github 看代码时,左边栏会出现一个树状结构,就像我们在IDE 一样。当我们看一个项目的结构,或者想看具体的某个文件,这样就会很方便。这里写图片描述

怎么样这样查看源代码的话是不是很方面?<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPrrDwcvLtdK7z8LO0sPHvfHM7NDo0qq96cnctcRMb2e21M/zo6zL/M6709phbmRyb2lkIGZyYW1ld29ya7LjdXRpbHOw/M/Co6zKx9K7uPZmaW5hbCBjbGFzc8Dgo7qy6b+0xuS+38zltqjS5aO6PC9wPg0KPHByZSBjbGFzcz0="brush:java;"> 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 {@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(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 println(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(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(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 println(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(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(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 println(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(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(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 println(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr)); } /* * 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 println(LOG_ID_MAIN, WARN, tag, getStackTraceString(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(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 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自带的日志框架不太满意,主要是无法定位日志位置。日志可以个性化的展示相关信息:
这里写图片描述

 </div>  </div> <
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Android Log,androidlog
  • 明明已经执行Log.i,偏偏打不出日志,执行log.i日志
  • android源码解析之(五)--)Log相关介绍
  • ULog远程日志——让Android调试更加方便直观

相关文章

  • 2017-05-26Andriod中自定义Dialog样式的Activity点击空白处隐藏软件盘(Dialog不消失),andrioddialog
  • 2018-01-28android单选按钮RadioGroup的详细使用
  • 2017-05-26Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent},recyclerviewholder
  • 2017-05-26Android 学习之路,android学习之路
  • 2017-05-26Android listview和gridview以及view的区别,androidgridview
  • 2017-08-23Android 隐藏状态栏 标题栏
  • 2017-05-26EditText的setSelection属性,setselection
  • 2017-05-26EventBus通信小能手,EventBus通信能手
  • 2017-05-26Linux(CentOS 7)命令行模式安装VMware Tools
  • 2017-05-26android开发之路01,android之路01

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android 博客园客户端 (五) 查看评论、搜索博主,android查看评论
    • 安卓界面组件----时间日期拾取器,安卓拾取
    • SIMLock锁卡功能解析,simlock锁解析
    • TextView 实现跑马灯效果,textview实现跑马灯
    • Android保持屏幕常亮,android屏幕常亮
    • android实现无限轮播,android实现轮播
    • Android开发2:事件处理及实现简单的对话框,android开发
    • 硅谷新闻8--TabLayout替换ViewPagerIndicator,tablayoutindicator
    • [android] 手机卫士读取联系人,android卫士
    • AlertDialog创建6种对话框的用法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有