一个好用的Log管理类
2015-10-08 10:58
267 查看
public class L { private static String className; //所在的类名 private static String methodName; //所在的方法名 private static int lineNumber; //所在行号 public static final int VERBOSE = 1; //显示Verbose及以上的Log public static final int DEBUG = 2; //显示Debug及以上的Log public static final int INFO = 3; //显示Info及以上的Log public static final int WARN = 4; //显示Warn及以上的Log public static final int ERROR = 5; //显示Error及以上的Log public static final int NOTHING = 6; //全部不显示 public static final int LEVEL = NOTHING; //控制显示的级别 private L() { } 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(lineNumber); buffer.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 v(String message) { if (!isDebuggable()) { return; } if (LEVEL <= VERBOSE) { getMethodNames(new Throwable().getStackTrace()); Log.v(className, createLog(message)); } } public static void d(String message) { if (!isDebuggable()) { return; } if (LEVEL <= DEBUG) { getMethodNames(new Throwable().getStackTrace()); Log.d(className, createLog(message)); } } public static void i(String message) { if (!isDebuggable()) { return; } if (LEVEL <= INFO) { getMethodNames(new Throwable().getStackTrace()); Log.i(className, createLog(message)); } } public static void w(String message) { if (!isDebuggable()) { return; } if (LEVEL <= WARN) { getMethodNames(new Throwable().getStackTrace()); Log.w(className, createLog(message)); } } public static void e(String message) { if (!isDebuggable()) { return; } if (LEVEL <= ERROR) { getMethodNames(new Throwable().getStackTrace()); Log.e(className, createLog(message)); } } }
原文地址:http://www.devwiki.net/2015/06/24/Android-New-Log/
相关文章推荐
- 密码加盐处理
- 发送http请求的方法
- 51nod 1208 && POJ 2482:Stars in Your Window
- php环境搭建:warmpserver
- iOS 获取设备类型
- 语义分析的一些方法(上篇)
- Math
- 使用Java调用以WSDL形式发布的web service
- 设计模式之Iterator:简单模拟java中集合类的实现
- 黑马程序员---OC学习笔记之ARC的概念及原理
- switch case加条件语句(非等值) php
- mongle db 简单查询
- OKHttp(2)——源码解析
- 华为OJ试题:输入n个整数,输出其中最小的k个
- [python项目一]查找输出fasta序列的gap的起始终止等信息
- Apache http server 64位绿色解压版的安装、部署、启动和使用说明
- linux常见命令
- coco2d-x学习笔记
- IOS和安卓ui设计常用尺寸及基本知识
- iOS开发---集成ShareSDK实现第三方登录、分享、关注等功能。