让android日志更有用些
来源:互联网 发布:苹果手机使用软件 编辑:程序博客网 时间:2024/05/21 07:46
原文地址:http://marspring.mobi/android-log/
android的log比起log4j等Java EE下的日志有些差距,比如不能直接简单设置就按等级打印,也不能再打印log里显示打印调用该log的类的信息,方法名,行号等。这里利用StackTraceElement对android Log类进行简单的封装。可以打印出当前log在那个类,调用的方法名,行号。
/** * */package com.xx.market.util;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Set;import android.util.Log;public class LogUtility {public static boolean getDecideResult(StackTraceElement ele) {return true;}public static String getCallerInfo() {StackTraceElement[] stack = (new Throwable()).getStackTrace();StackTraceElement ele = null;String className = "";String methodName = "";int lineNO = 0;if (stack.length > 2) {ele = stack[2];try {className = Class.forName(ele.getClassName()).getSimpleName();methodName = ele.getMethodName();lineNO = ele.getLineNumber();} catch (ClassNotFoundException e) {}}String callerInfo = className + ":" + methodName + ":" + lineNO + "=>";return callerInfo;}public static void i(String tag, double info) {if (Constants.DEBUG) {Log.i(tag, getCallerInfo() + String.valueOf(info));}}public static void i(String tag, float info) {if (Constants.DEBUG) {Log.i(tag, getCallerInfo() + String.valueOf(info));}}public static void i(String tag, int info) {if (Constants.DEBUG) {Log.i(tag,getCallerInfo() + String.valueOf(info));}}public static void i(String tag, long info) {if (Constants.DEBUG) {Log.i(tag, getCallerInfo() + String.valueOf(info));}}public static void i(String tag, String info) {if (Constants.DEBUG) {Log.i(tag, getCallerInfo() + String.valueOf(info));}}public static void w(String tag, String info) {if (Constants.WARN) {Log.w(tag, getCallerInfo() + info);}}public static void e(String tag, String info) {if (Constants.ERROR) {Log.e(tag, getCallerInfo() + info);}}@SuppressWarnings("rawtypes")public static void i(String tag, String startMsg, Collection info) {if (Constants.DEBUG) {StringBuffer msg = null;if (startMsg == null) {msg = new StringBuffer(getCallerInfo());} else {msg = new StringBuffer(getCallerInfo() + startMsg);}Iterator itr = info.iterator();while (itr.hasNext()) {msg.append(itr.next().toString());msg.append(";");}Log.i(tag, msg.toString());}}@SuppressWarnings("rawtypes")public static void i(String tag, String startMsg, HashMap info) {if (Constants.DEBUG) {StringBuffer msg = null;if (startMsg == null) {msg = new StringBuffer(getCallerInfo());} else {msg = new StringBuffer(getCallerInfo() + startMsg);}Set keySet = info.keySet();Iterator itr = keySet.iterator();while (itr.hasNext()) {Object key = itr.next();Object value = info.get(key);msg.append(key.toString());msg.append("=");msg.append(value.toString());msg.append(";");}Log.i(tag, msg.toString());}}public static void debugForImage(String info) {// 正式环境去掉if (Constants.DEBUG) {Log.i("Market-lion", getCallerInfo() + info);}}public static void debug(String info) {// 正式环境去掉if (Constants.DEBUG) {Log.i("list",getCallerInfo() + info);}}}
- 让android日志更有用些
- 让您的404页面更有用
- Kotlin让Android更简单~
- Kotlin让Android更简单~
- 让CoreData更简单些
- 如何让你的Web服务器日志文件更安全?
- 让Android UI性能更高效
- 让Android App启动更协调
- 新闻专栏~ART让Android更流畅
- Lottie- 让Android动画实现更简单
- Lottie- 让Android动画实现更简单
- Lottie- 让Android动画实现更简单
- Lottie- 让Android动画实现更简单
- Android O 让安装应用更安全
- 如何让Android WebView访问更快
- 20个有用的工具让你的web开发更有效率
- Fedora 有用更新源
- Splunk 模式的中国践行者——日志易让日志分析更容易
- Thrust快速入门教程(二)——Vector的使用
- poj 2923 状态压缩 + 01背包
- win8应用商店安装路径
- 中语言项目组给中国编程爱好者的告示
- android调用天气预报的webservice例子
- 让android日志更有用些
- 内核驱动小记(一)——Realtek 8168
- 高级字符驱动学习--阻塞型I/0
- Thrust快速入门教程(三)——迭代器与静态调度
- Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架
- linux c使用openssl/md5.h加密
- [问题分析]IIMS 系统开发中遇到的问题
- Android OpenGL ES 开发教程(8):基本几何图形定义
- 中语言特性介绍之前言