在Android开发中,Log打印日志,作为调试或者管理,都是必不可少的,但是log输入的地方太大,如果产品上线的时候,经理要求不要有log输出,避免泄露一些信息等,这时,如果我们一处一次的去找log并注释掉它们,非得疯了不可,太多了!!
实际开发中,很少有人会直接使用系统自带的log打印日志,基本都会自己封装一下,否则会显得很low!
这里列一下自己的一些封装类,比如log统一管理类,比如单例的toast等....
log的统一管理,无非是加一个boolean类型的tag作为标记,显示log的时候,tag=true,不显示tag=false:
用的时候很简单,比如我想输出i级别的,直接Hlog.i("自己的标记","要输出的信息...");
log输出类:
/**
* @author tyxo
* @qq 1577441454
* @date 2015/6/6 11:41
* @desc 用于统一管理log日志的输出,不想显示,将 DEBUG 设置为false即可
*/
public class HLog {
private static HashMap<String, HILogger> loggerHashMap = new HashMap<String, HILogger>();
private static final HILogger defaultLogger = new HPrintToLogCatLogger();
public static void addLogger(HILogger logger) {
String loggerName = logger.getClass().getName();
String defaultLoggerName = defaultLogger.getClass().getName();
if (!loggerHashMap.containsKey(loggerName)
&& !defaultLoggerName.equalsIgnoreCase(loggerName)) {
logger.open();
loggerHashMap.put(loggerName, logger);
}
}
public static void removeLogger(HILogger logger) {
String loggerName = logger.getClass().getName();
if (loggerHashMap.containsKey(loggerName)) {
logger.close();
loggerHashMap.remove(loggerName);
}
}
public static void d(Object object, String message) {
printLoger(HConfig.LOG_DEBUG, object, message);
}
public static void e(Object object, String message) {
printLoger(HConfig.LOG_ERROR, object, message);
}
public static void i(Object object, String message) {
printLoger(HConfig.LOG_INFO, object, message);
}
public static void v(Object object, String message) {
printLoger(HConfig.LOG_VERBOSE, object, message);
}
public static void w(Object object, String message) {
printLoger(HConfig.LOG_WARN, object, message);
}
public static void out(Object object, String message) {
printLoger(HConfig.SYS_OUT, object, message);
}
public static void err(Object object, String message) {
printLoger(HConfig.SYS_ERR, object, message);
}
public static void d(String tag, String message) {
printLoger(HConfig.LOG_DEBUG, tag, message);
}
public static void e(String tag, String message) {
printLoger(HConfig.LOG_ERROR, tag, message);
}
public static void i(String tag, String message) {
printLoger(HConfig.LOG_INFO, tag, message);
}
public static void v(String tag, String message) {
printLoger(HConfig.LOG_VERBOSE, tag, message);
}
public static void w(String tag, String message) {
printLoger(HConfig.LOG_WARN, tag, message);
}
public static void out(String object, String message) {
printLoger(HConfig.SYS_OUT, object, message);
}
public static void err(String object, String message) {
printLoger(HConfig.SYS_ERR, object, message);
}
public static void println(int priority, String tag, String message) {
printLoger(priority, tag, message);
}
private static void printLoger(int priority, Object object, String message) {
Class<?> cls = object.getClass();
String tag = cls.getName();
String arrays[] = tag.split("\\.");
tag = arrays[arrays.length - 1];
printLoger(priority, tag, message);
}
private static void printLoger(int priority, String tag, String message) {
if (HConfig.DEBUG) {
printLoger(defaultLogger, priority, tag, message);
Iterator<Entry<String, HILogger>> iter = loggerHashMap.entrySet()
.iterator();
while (iter.hasNext()) {
Map.Entry<String, HILogger> entry = iter.next();
HILogger logger = entry.getValue();
if (logger != null) {
printLoger(logger, priority, tag, message);
}
}
}
}
private static void printLoger(HILogger logger, int priority, String tag, String message) {
switch (priority) {
case HConfig.LOG_VERBOSE:
logger.v(tag, message);
break;
case HConfig.LOG_DEBUG:
logger.d(tag, message);
break;
case HConfig.LOG_INFO:
logger.i(tag, message);
break;
case HConfig.LOG_WARN:
logger.w(tag, message);
break;
case HConfig.LOG_ERROR:
logger.e(tag, message);
break;
case HConfig.SYS_OUT:
logger.e(tag, message);
break;
case HConfig.SYS_ERR:
logger.e(tag, message);
break;
default:
break;
}
}
/** 调试标记 */
public static boolean getHardDebugFlag() {
return HConfig.DEBUG;
}
}
当然还有一个配置文件,比较简单:
/**
* @author tyxo
* @qq 1577441454
* @date 2015/6/6 11:41
* @desc 用于统一管理log日志的输出, 不想显示, 将 DEBUG 设置为false即可
*/
public class HConfig {
public static final boolean DEBUG = true;
/**
* Priority constant for the println method; use TALogger.v.
*/
public static final int LOG_VERBOSE = 2;
/**
* Priority constant for the println method; use TALogger.d.
*/
public static final int LOG_DEBUG = 3;
/**
* Priority constant for the println method; use TALogger.i.
*/
public static final int LOG_INFO = 4;
/**
* Priority constant for the println method; use TALogger.w.
*/
public static final int LOG_WARN = 5;
/**
* Priority constant for the println method; use TALogger.e.
*/
public static final int LOG_ERROR = 6;
/**
* Priority constant for the println method.
*/
public static final int LOG_ASSERT = 7;
/**
* System.out.println();
*/
public static final int SYS_OUT = 8;
/**
* System.err.println();
*/
public static final int SYS_ERR = 9;
}
一个简单回调类:
/**
* @author tyxo
* @qq 1577441454
* @date 2015/6/6 11:41
* @desc
*/
public interface HILogger {
void v(String tag, String message);
void d(String tag, String message);
void i(String tag, String message);
void w(String tag, String message);
void e(String tag, String message);
void out(String tag, String message);
void err(String tag, String message);
void open();
void close();
void println(int priority, String tag, String message);
}
一个回调实现类:
/**
* @author tyxo
* @qq 1577441454
* @date 2015/6/6 11:41
* @desc 回调实现
*/
public class HPrintToLogCatLogger implements HILogger {
@Override
public void d(String tag, String message) {
Log.d(tag, message);
}
@Override
public void e(String tag, String message) {
Log.e(tag, message);
}
@Override
public void i(String tag, String message) {
Log.i(tag, message);
}
@Override
public void v(String tag, String message) {
Log.v(tag, message);
}
@Override
public void w(String tag, String message) {
Log.w(tag, message);
}
@Override
public void err(String tag, String message) {
System.out.println(tag + " : " + message);
}
@Override
public void out(String tag, String message) {
System.err.println(tag + " : " + message);
}
@Override
public void println(int priority, String tag, String message) {
Log.println(priority, tag, message);
}
@Override
public void open() {
}
@Override
public void close() {
}
}
OK,这样我们就可以放心的使用Hlog进行打印输出了.