Android Logging

简介:

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();
        }
  
}
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/p/3479070.html
相关文章
|
1天前
|
数据采集 人工智能 安全
|
10天前
|
云安全 监控 安全
|
2天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
882 150
|
15天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1619 8
|
6天前
|
人工智能 前端开发 文件存储
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择
星哥带你玩转飞牛NAS,部署开源笔记TriliumNext!支持树状知识库、多端同步、AI摘要与代码高亮,数据自主可控,打造个人“第二大脑”。高效玩家的新选择,轻松搭建专属知识管理体系。
362 152
|
7天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
588 152
|
9天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
545 13
|
2天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话