常用的Log日志打印与输出
2014-06-24 10:27
387 查看
/** * Log日志 * * @author Jenly * @date 2014-6-20 */ public class LogUtils { public static final String TAG = "Jenly"; private static final String COLON = ":"; private static final String VERTICAL = "|"; /** 是否显示Log日志 */ private static boolean isShowLog = true; /** Log日志优先权 */ private static int priority = 1; /** * Priority constant for the println method;use System.out.println */ public static final int PRINTLN = 1; /** * Priority constant for the println method; use Log.v. */ public static final int VERBOSE = 2; /** * Priority constant for the println method; use Log.d. */ public static final int DEBUG = 3; /** * Priority constant for the println method; use Log.i. */ public static final int INFO = 4; /** * Priority constant for the println method; use Log.w. */ public static final int WARN = 5; /** * Priority constant for the println method; use Log.e. */ public static final int ERROR = 6; /** * Priority constant for the println method.use Log.wtf. */ public static final int ASSERT = 7; public static final String TAG_FORMAT = "%s.%s(L:%d)"; public static void setShowLog(boolean isShowLog) { LogUtils.isShowLog = isShowLog; } public static boolean isShowLog() { return isShowLog; } public static int getPriority() { return priority; } public static void setPriority(int priority) { LogUtils.priority = priority; } /** * 根据堆栈生成TAG * @return TAG|className.methodName(L:lineNumber) */ private static String generateTag(StackTraceElement caller) { String tag = TAG_FORMAT; String callerClazzName = caller.getClassName(); callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1); tag = String.format(tag,new Object[] { callerClazzName, caller.getMethodName(),Integer.valueOf(caller.getLineNumber()) }); return new StringBuilder().append(TAG).append(VERTICAL).append(tag).toString(); } /** * 获取堆栈 * @param n * n=0 VMStack * n=1 Thread * n=3 CurrentStack * n=4 CallerStack * ... * @return */ public static StackTraceElement getStackTraceElement(int n) { return Thread.currentThread().getStackTrace() ; } /** * 获取调用方的堆栈TAG * @return */ private static String getCallerStackLogTag(){ return generateTag(getStackTraceElement(5)); } /** * * @param t * @return */ private static String getStackTraceString(Throwable t){ return Log.getStackTraceString(t); } // -----------------------------------Log.v /** * Log.v * @param msg */ public static void v(String msg) { if (isShowLog && priority <= VERBOSE) Log.v(getCallerStackLogTag(), msg); } public static void v(Throwable t) { if (isShowLog && priority <= VERBOSE) Log.v(getCallerStackLogTag(), getStackTraceString(t)); } public static void v(String msg,Throwable t) { if (isShowLog && priority <= VERBOSE) Log.v(getCallerStackLogTag(), msg, t); } // -----------------------------------Log.d /** * Log.d * @param msg */ public static void d(String msg) { if (isShowLog && priority <= DEBUG) Log.d(getCallerStackLogTag(), msg); } public static void d(Throwable t) { if (isShowLog && priority <= DEBUG) Log.d(getCallerStackLogTag(), getStackTraceString(t)); } public static void d(String msg,Throwable t) { if (isShowLog && priority <= DEBUG) Log.d(getCallerStackLogTag(), msg, t); } // -----------------------------------Log.i /** * Log.i * @param msg */ public static void i(String msg) { if (isShowLog && priority <= INFO) Log.i(getCallerStackLogTag(), msg); } public static void i(Throwable t) { if (isShowLog && priority <= INFO) Log.i(getCallerStackLogTag(), getStackTraceString(t)); } public static void i(String msg,Throwable t) { if (isShowLog && priority <= INFO) Log.i(getCallerStackLogTag(), msg, t); } // -----------------------------------Log.w /** * Log.w * @param msg */ public static void w(String msg) { if (isShowLog && priority <= WARN) Log.w(getCallerStackLogTag(), msg); } public static void w(Throwable t) { if (isShowLog && priority <= WARN) Log.w(getCallerStackLogTag(), getStackTraceString(t)); } public static void w(String msg,Throwable t) { if (isShowLog && priority <= WARN) Log.w(getCallerStackLogTag(), msg, t); } // -----------------------------------Log.e /** * Log.e * @param msg */ public static void e(String msg) { if (isShowLog && priority <= ERROR) Log.e(getCallerStackLogTag(), msg); } public static void e(Throwable t) { if (isShowLog && priority <= ERROR) Log.e(getCallerStackLogTag(), getStackTraceString(t)); } public static void e(String msg,Throwable t) { if (isShowLog && priority <= ERROR) Log.e(getCallerStackLogTag(), msg, t); } // -----------------------------------Log.wtf /** * Log.wtf * @param msg */ public static void wtf(String msg) { if (isShowLog && priority <= ASSERT) Log.wtf(getCallerStackLogTag(), msg); } public static void wtf(Throwable t) { if (isShowLog && priority <= ASSERT) Log.wtf(getCallerStackLogTag(), getStackTraceString(t)); } public static void wtf(String msg,Throwable t) { if (isShowLog && priority <= ASSERT) Log.wtf(getCallerStackLogTag(), msg, t); } // -----------------------------------System.out.print /** * System.out.print * * @param msg */ public static void print(String msg) { if (isShowLog && priority <= PRINTLN) System.out.print(msg); } public static void print(Object obj) { if (isShowLog && priority <= PRINTLN) System.out.print(obj); } // -----------------------------------System.out.printf /** * System.out.printf * * @param msg */ public static void printf(String msg) { if (isShowLog && priority <= PRINTLN) System.out.printf(msg); } // -----------------------------------System.out.println /** * System.out.println * * @param msg */ public static void println(String msg) { if (isShowLog && priority <= PRINTLN) System.out.println(msg); } public static void println(Object obj) { if (isShowLog && priority <= PRINTLN) System.out.println(obj); } }
只是进行了一个简单的封装、方便记录与管理、
相关文章推荐
- 常用的Log日志打印与输出
- 经常使用Log日志打印输出
- Python+Selenium进行UI自动化测试项目中,常用的小技巧4:日志打印,longging模块(控制台和文件同时输出)
- android orhanobut logger打印那个类第几行调用log输出,在输出日志上上点击跳转到源代码
- android常用工具类 -- 打印log日志
- Android Log输出 log开关 日志打印器 LogUtil
- Activity生命周期及如何存活 用log日志信息打印输出
- 工具类——Log日志打印,输出到文件,过滤显示日志位置
- OC IOS 自定义Log日志 附:常用输出宏
- iPhone控制程序是否打印Log日志
- android log日志文件输出
- Android心得2.5--对应用进行单元测试、打印错误信息和日志输出
- perl 使用Log::Log4perl输出日志
- LR设置返回值检查点&打印输出执行日志
- LoadRunner常用知识点-----LoadRunner日志输出
- 打印日志的宏,分级输出
- wp打印输出日志
- jsp中打印log日志
- adb打印常用log