Log.java
package com.tsots.Log; public class Log { public static String TAG = "TSOTS_LOG"; static String nowClass = Thread.currentThread().getStackTrace()[3].getClassName(); //顯示呼叫此Log的Class static String nowMethod = Thread.currentThread().getStackTrace()[3].getMethodName(); //顯示呼叫此Log的Method static int nowLineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber(); //顯示呼叫此Log的行號 public final static void d(String TAG, String msg) { String nowClass = ""; String nowMethod = ""; String finalStr = null; int nowLineNumber = 0; StackTraceElement stackTraceElement = null; String tId="-["+Thread.currentThread().getId()+"]"; try { stackTraceElement=Thread.currentThread().getStackTrace()[3]; nowClass = stackTraceElement.getClassName(); nowClass=(nowClass.indexOf(46)!=-1)?nowClass.substring(nowClass.lastIndexOf(46)+1):nowClass; nowMethod = stackTraceElement.getMethodName(); nowLineNumber = stackTraceElement.getLineNumber(); finalStr = tId+"["+nowClass + "][" + nowMethod +"]" + nowLineNumber + "]" + msg; android.util.Log.d(TAG, finalStr); }catch (Exception e) { android.util.Log.e(TAG, nowMethod, e); android.util.Log.d(TAG, msg); } } } //
============================================
在同package下的其它檔案要印Log時只要呼叫
L.info("this is a msg");
就會顯示如下資訊
Application Tag Text
com.tsots.log TSOTS_LOG [MainActivity][onCreate][16]this is a msg
沒有留言:
張貼留言