Android日志打印DebugLog

1public class DebugLog{ 2 3 static String className; 4 static String methodName; 5 6    private DebugLog(){ 7        /* Protect from instantiations */ 8    } 9 10 public static boolean isDebuggable(){ 11 return BuildConfig.DEBUG; 12 } 13 14 private static String createLog(String log){ 15 16 StringBuffer buffer = new StringBuffer(); 17 buffer.append("["); 18 buffer.append(methodName); 19 buffer.append("]"); 20 buffer.append(log); 21 22 return buffer.toString(); 23 } 24 25 private static void getMethodNames(StackTraceElement[] sElements){ 26 className = sElements[1].getFileName(); 27 methodName = sElements[1].getMethodName(); 28 } 29 30 public static void e(String message){ 31 if (!isDebuggable()) 32 return; 33 34 // Throwable instance must be created before any methods   35 getMethodNames(new Throwable().getStackTrace()); 36 Log.e(className, createLog(message)); 37 } 38 39 public static void i(String message){ 40 if (!isDebuggable()) 41 return; 42 43 getMethodNames(new Throwable().getStackTrace()); 44 Log.i(className, createLog(message)); 45 } 46 47 public static void d(String message){ 48 if (!isDebuggable()) 49 return; 50 51 getMethodNames(new Throwable().getStackTrace()); 52 Log.d(className, createLog(message)); 53 } 54 55 public static void v(String message){ 56 if (!isDebuggable()) 57 return; 58 59 getMethodNames(new Throwable().getStackTrace()); 60 Log.v(className, createLog(message)); 61 } 62 63 public static void w(String message){ 64 if (!isDebuggable()) 65 return; 66 67 getMethodNames(new Throwable().getStackTrace()); 68 Log.w(className, createLog(message)); 69 } 70 71 public static void wtf(String message){ 72 if (!isDebuggable()) 73 return; 74 75 getMethodNames(new Throwable().getStackTrace()); 76 Log.wtf(className, createLog(message)); 77 } 78 79}

调用:

1@Override 2protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 6 DebugLog.e("simple log from onCreate()"); 7 8 myFunc(); 9 mySecondFunc(); 10} 11 12void myFunc(){ 13 DebugLog.e("simple log from myFunc()"); 14} 15 16void mySecondFunc(){ 17 DebugLog.i("simple log from mySecondFunc()"); 18} 19 20@Override 21protected void onResume() { 22 super.onResume(); 23 24 DebugLog.v("v log"); 25 DebugLog.w("w log"); 26 DebugLog.wtf("wtf log"); 27}

ScreenshotScreenshot

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java 处理emoji表情

public class EmojiUtil {/  将str中的emoji表情转为byte数组    @param str  @return /public static String resolveToByteFromEmoji(String str

java字符串比较和jdkequals源码分析

 package com.cnse.demo;/  比较两个字符串相等 /public class StringTest { public static void main(String args) {  int checkLength  0;  String str

HIVE 时间操作函数

日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec

UTF和GBK互转

不说了直接上代码:package com.yh.photo.util;public class UTF2GBK { public static String gbk2utf8(String gbk) {  String l_temp  GBK2Unicode(gbk);  l_temp  un

JAVA 线程基本知识汇总--ThreadLocal 和 InheritableThreadLoc

package org.test;public class ThreadLocalTest {public static void main(String args) {User user  new User(new ThreadLocal<String());Book book