Android日志打印DebugLog

Stella981
• 阅读 458
public class DebugLog{

    static String className;
    static String methodName;

    private DebugLog(){
        /* Protect from instantiations */
    }

    public static boolean isDebuggable(){
        return BuildConfig.DEBUG;
    }

    private static String createLog(String log){

        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        buffer.append(methodName);
        buffer.append("]");
        buffer.append(log);

        return buffer.toString();
    }

    private static void getMethodNames(StackTraceElement[] sElements){
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
    }

    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));
    }

}

调用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DebugLog.e("simple log from onCreate()");

    myFunc();
    mySecondFunc();
}

void myFunc(){
    DebugLog.e("simple log from myFunc()");
}

void mySecondFunc(){
    DebugLog.i("simple log from mySecondFunc()");
}

@Override
protected void onResume() {
    super.onResume();

    DebugLog.v("v log");
    DebugLog.w("w log");
    DebugLog.wtf("wtf log");
}

Android日志打印DebugLog Android日志打印DebugLog

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java 处理emoji表情
public class EmojiUtil {/  将str中的emoji表情转为byte数组    @param str  @return /public static String resolveToByteFromEmoji(String str
Wesley13 Wesley13
2年前
java字符串比较和jdkequals源码分析
 package com.cnse.demo;/  比较两个字符串相等 /public class StringTest { public static void main(String args) {  int checkLength  0;  String str
Easter79 Easter79
2年前
String.intern()引发的性能问题
项目代码中用到反射,伴随大量的NoSuchFieldException异常,发现cpu飙高,排查后发现跟String.intern有关。在Class中有连个常见的方法:Ø public Field getField(String name)Ø getMethod(String name, Class<?... parameterTypes)
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
2年前
UTF和GBK互转
不说了直接上代码:package com.yh.photo.util;public class UTF2GBK { public static String gbk2utf8(String gbk) {  String l_temp  GBK2Unicode(gbk);  l_temp  un
Wesley13 Wesley13
2年前
JAVA 线程基本知识汇总--ThreadLocal 和 InheritableThreadLoc
package org.test;public class ThreadLocalTest {public static void main(String args) {User user  new User(new ThreadLocal<String());Book book 
Stella981 Stella981
2年前
BlockingQueue队列的使用
import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;public class BlockingQueueTest { public static void main(String args) 
Stella981 Stella981
2年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Wesley13 Wesley13
2年前
5.48 正则表达式的概述和基本使用
import java.util.Scanner;public class RegexDemo {public static void main(String args) {// 创建键盘录入对象Scanner sc  new Scanner(System.in);Sys
Stella981 Stella981
2年前
CountDownLatch 计数栓
import java.util.concurrent.CountDownLatch;/  Created by zyBourn on 15/12/25. /public class Entity {    public static void main(String args)