Here is HauteLook’s logger class from our Android Code Library. It displays class name, method name and line number per line of log message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.hautelook.android.lib.logger;
  
/**
 * @authorjimmy@hautelook.com
 * @copyright 2011 HauteLook
 *
 * Description:
 *   Simple logger that displays: class name, method name and line number
 *
 *
 * Usage:
 
 *   Use this class directly or customize it by extending it.
 
 *      HLlog.i("");
 *      HLLog.i("called");
 *      HLLog.i("called","tag");
 *  
 *      L.i();
 *      L.i("called");
 *      L.i("called","tag");
 *
 * Sub-classing example:
 *
 *    // C.DEBUG = boolean true/false (project specific constant class)
 *
 *    public class L extends HLLog {
 *    
 *           public static String DEFAULT_TAG = "HauteLook";
 *    
 *           public static void i() {
 *                if(C.DEBUG) callLogger("i", DEFAULT_TAG, "");
 *           }
 *    
 *           public static void i(String message) {
 *                  if(C.DEBUG) callLogger("i", DEFAULT_TAG, message);
 *           }
 *
 *           public static void i(String message, String tag) {
 *                  if(C.DEBUG) callLogger("i", tag, message);
 *           }
 *
 *    }
 
 */
  
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.util.Log;
  
@SuppressWarnings("unused")
publicclass HLLog {
  
       publicstatic String DEFAULT_TAG ="HauteLookLib";
  
       finalstaticintdepth = 4;
  
       publicstaticvoid i(String message) {
              callLogger("i", DEFAULT_TAG, message);
       }
  
       publicstaticvoid i(String message, String tag) {
              callLogger("i", tag, message);
       }
  
       publicstaticvoid d(String message) {
              callLogger("d", DEFAULT_TAG, message);
       }
  
       publicstaticvoid d(String message, String tag) {
              callLogger("d", tag, message);
       }
  
       publicstaticvoid e(String message) {
              callLogger("e", DEFAULT_TAG, message);
       }
  
       publicstaticvoid e(String message, String tag) {
              callLogger("e", tag, message);
       }
  
       publicstaticvoid w(String message) {
              callLogger("w", DEFAULT_TAG, message);
       }
  
       publicstaticvoid w(String message, String tag) {
              callLogger("w", tag, message);
       }
  
       publicstaticvoid v(String message) {
              callLogger("v", DEFAULT_TAG, message);
       }
  
       publicstaticvoid v(String message, String tag) {
              callLogger("v", tag, message);
       }
  
       @SuppressWarnings("rawtypes")
       publicstaticvoid callLogger(String methodName, String tag, String message) {
              final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
              try {
                     Class cls = Class.forName("android.util.Log");
                     Method method = cls.getMethod(methodName, String.class,       String.class);
                     method.invoke(null, tag, getTrace(ste) + message);
              } catch (ClassNotFoundException e) {
                     e.printStackTrace();
              } catch (IllegalArgumentException e) {
                     e.printStackTrace();
              } catch (SecurityException e) {
                     e.printStackTrace();
              } catch (IllegalAccessException e) {
                     e.printStackTrace();
              } catch (InvocationTargetException e) {
                     e.printStackTrace();
              } catch (NoSuchMethodException e) {
                     e.printStackTrace();
              }
       }
  
       publicstatic String getTrace(StackTraceElement[] ste) {
              return"[" + getClassName(ste) + "][" + getMethodName(ste) + "][" + getLineNumber(ste) + "] ";
       }
  
       publicstatic String getClassPackage(StackTraceElement[] ste) {
              return ste[depth].getClassName();
       }
  
       publicstatic String getClassName(StackTraceElement[] ste) {
              String[] temp = ste[depth].getClassName().split("\\.");
              return temp[temp.length - 1];
       }
  
       publicstatic String getMethodName(StackTraceElement[] ste) {
              return ste[depth].getMethodName();
       }
  
       publicstaticint getLineNumber(StackTraceElement[] ste) {
              return ste[depth].getLineNumber();
       }
  
}

Android Logging的更多相关文章

  1. Android Native 代码NDK开发学习笔记

    引用:http://www.kunli.info/2011/08/21/android-native-code-study-note/ JNI,全称Java Native Interface,是用于让 ...

  2. Android Debugging

    Debugging methods for Android Contents [hide]  1 Debuggers 1.1 Kernel and User co-debug with GDB on ...

  3. 【转】Android使用Log4j例子

    Step 1 Download Log4J library from http://logging.apache.org/log4j/1.2/download.html Step 2 Configur ...

  4. 6、android 普通日志输出到SD卡

    这是本人见过写博文最负责的一个人: http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_ca ...

  5. Android中使用logwrapper来重定向应用程序的标准输出

    在Android应用程序调试中,有时候第三方应用程序的日志输出是通过printf之类的标准函数输出的,logcat不能捕获这些日志,一个方法是使用logwrapper命令来执行第三方应用程序,logw ...

  6. android从应用到驱动之—camera(2)---cameraHAL的实现

    本文是camera系列博客,上一篇是: android从应用到驱动之-camera(1)---程序调用流程 本来想用这一篇博客把cameraHAL的实现和流程都给写完的.搞了半天,东西实在是太多了.这 ...

  7. android从应用到驱动之—camera(2)---cameraHAL的实现

    本来想用这一篇博客把cameraHAL的实现和流程都给写完的.搞了半天,东西实在是太多了.这篇先写cameraHAL的基本实现框架,下一篇在具体写camerahal的流程吧. cameraHAL的实现 ...

  8. android开发教程(八)——环境搭建之java-ndk

    目录 android ndk是android用于开发本地代码的开发工具包.它提供C/C++交叉编译工具.android内核.驱动.已有的C/C++代码,都需要ndk来支持开发. 目前支持以下平台:ar ...

  9. Android 启动过程总结

    SystemServer的启动 frameworks/base/services/java/com/android/server/SystemServer.java: run() 其中调用Activi ...

随机推荐

  1. Ubuntu16下编译linux内核,报"mkimage" command not found错的解决

    "mkimage" command not found - U-Boot images will not be built /work/system/linux-3.4.20/ar ...

  2. SpingData 的学习

    Spring Data : Spring 的一个子项目,类似于Sping MVC 一样是Spring的另一个模块,所以还需要下载其jar ,它需要的jar有: spring-data-jpa-1.11 ...

  3. 【学习笔记】Markdown入门

    Markdown入门 最近把博客园的编辑器换成了Markdown.   语法入门 Markdown确实好用,本来我想总结一下常用Markdown的语法,但有下面这篇文章在,我实在是不敢画蛇添足了. 基 ...

  4. Integer-->String String-->Integer

    参考:http://blog.csdn.net/wangjolly/article/details/18354457 crane: String str="123";int a=0 ...

  5. 你知道的,javascript语言的执行环境是"单线程模式",这种模式的好处是实现起来比较简单,执行环境相对单纯;坏处是只要有一个任务耗时很长,后面的任务都必须排队等着,会拖延整个程序的执行,因此很多时候需要进行“异步模式”,请列举js异步编程的方法。

    回调函数,这是异步编程最基本的方法. 事件监听,另一种思路是采用事件驱动模式.任务的执行不取决于代码的顺序,而取决于某个事件是否发生. 发布/订阅,上一节的"事件",完全可以理解成 ...

  6. GRE and VXLAN with Open vSwitch

    因为在OpenStack的Neutron中比较常用,所以参考别人的博客试了下OVS的一些隧道封装功能(GRE,VXLAN). 实验:实现两个host的Network namespace之间的通信,NS ...

  7. Eclipse的基本使用

    01Eclipse的下载安装 * A: Eclipse的下载安装  * a: 下载 * http://www.eclipse.org * b: 安装 * 只需要解压后就能使用 * c: 卸载 * 只 ...

  8. pycharm安装---优秀的IDE

    概述:pycharm当前来讲是python最优秀的IDE. 1. 官网下载安装包 2.解压 3. cd 到解压的bin文件中 4.执行sh ./pycharm.sh 5.锁定到图标中

  9. 机器学习,数据挖掘,统计学,云计算,众包(crowdsourcing),人工智能,降维(Dimension reduction)

    机器学习 Machine Learning:提供数据分析的能力,机器学习是大数据时代必不可少的核心技术,道理很简单:收集.存储.传输.管理大数据的目的,是为了“利用”大数据,而如果没有机器学习技术分析 ...

  10. C++与C#有关对库(动态库dll,静态库.lib)文件的调用

    1 动态库的相互调用 1.1 C#调用C++ dll步骤(只能导出方法): 1. c++建立空项目->源文件文件夹中添加cpp文件和函数 2. c++属性设置中,配置类型设置为动态库dll,公共 ...