android打印日志封装

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
public class LogUtils {
    static String className;//类名
    static String methodName;//方法名
    static int lineNumber;//行数
 
    private LogUtils(){
        /* Protect from instantiations */
    }
 
    public static boolean isDebuggable() {
        return BuildConfig.DEBUG;
    }
 
    private static String createLog( String log ) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(methodName);
        buffer.append("(").append(className).append(":").append(lineNumber).append(")");
        buffer.append(log);
        return buffer.toString();
    }
 
    private static void getMethodNames(StackTraceElement[] sElements){
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }
 
 
    public static void e(String message){
        if (!isDebuggable())
            return;
 
        // Throwable instance must be created before any methods
        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, createLog(message));
    }
 
 
    public static void i(String message){
        if (!isDebuggable())
            return;
 
        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, createLog(message));
    }
 
    public static void d(String message){
        if (!isDebuggable())
            return;
 
        getMethodNames(new Throwable().getStackTrace());
        Log.d(className, createLog(message));
    }
 
    public static void v(String message){
        if (!isDebuggable())
            return;
 
        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, createLog(message));
    }
 
    public static void w(String message){
        if (!isDebuggable())
            return;
 
        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, createLog(message));
    }
 
    public static void wtf(String message){
        if (!isDebuggable())
            return;
 
        getMethodNames(new Throwable().getStackTrace());
        Log.wtf(className, createLog(message));
    }

 

posted @   新年新气象  阅读(407)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示