android学习笔记——灵活使用Log打印日志
2016-03-20 00:08
519 查看
我们都知道android的日志打印能够帮助我们快速定位错误,以及帮助我们获取重要信息。但是,当我们需要发布我们的项目时,我们不能让这些日志继续打印执行,得让他们都不工作,但是项目一大,你不可能一条一条的去删Log吧,所以,我们得想一个更好的办法来解决这个问题。
我们尝试写一个这样的类
public class LogUtil {
public static final int VERBOSE = 1;
public static final int DEBUG = 2;
public static final int INFO = 3;
public static final int WARN = 4;
public static final int ERROR = 5;
public static final int NOTHING = 6;
public static final int LEVEL = VERBOSE;
public static void v(String tag , String msg){
if (LEVEL <= VERBOSE){
Log.v(tag , msg);
}
}
public static void d(String tag , String msg){
if (LEVEL <= DEBUG){
Log.d(tag, msg);
}
}
public static void i(String tag , String msg){
if (LEVEL <= INFO){
Log.i(tag, msg);
}
}
public static void w(String tag , String msg){
if (LEVEL <= WARN){
Log.w(tag, msg);
}
}
public static void e(String tag , String msg){
if (LEVEL <= ERROR){
Log.e(tag , msg);
}
}
}
当我们需要去打印日志的时候使用这个类的方法,LogUtil.e("TAG" , "error log");
同样可以打印出日志信息。而当我们的项目需要上线的时候,只需要将LEVEL的级别调整到NOTHING上来,就不会再打印出信息了。
我们尝试写一个这样的类
public class LogUtil {
public static final int VERBOSE = 1;
public static final int DEBUG = 2;
public static final int INFO = 3;
public static final int WARN = 4;
public static final int ERROR = 5;
public static final int NOTHING = 6;
public static final int LEVEL = VERBOSE;
public static void v(String tag , String msg){
if (LEVEL <= VERBOSE){
Log.v(tag , msg);
}
}
public static void d(String tag , String msg){
if (LEVEL <= DEBUG){
Log.d(tag, msg);
}
}
public static void i(String tag , String msg){
if (LEVEL <= INFO){
Log.i(tag, msg);
}
}
public static void w(String tag , String msg){
if (LEVEL <= WARN){
Log.w(tag, msg);
}
}
public static void e(String tag , String msg){
if (LEVEL <= ERROR){
Log.e(tag , msg);
}
}
}
当我们需要去打印日志的时候使用这个类的方法,LogUtil.e("TAG" , "error log");
同样可以打印出日志信息。而当我们的项目需要上线的时候,只需要将LEVEL的级别调整到NOTHING上来,就不会再打印出信息了。
相关文章推荐
- android学习笔记——全局获取Context的技巧
- android学习笔记——Localtion服务之LocationManager
- android学习笔记——Intent的解析(Intent的隐式调用)
- android学习笔记——回调机制
- android布局
- Android规范文档
- android数据传递
- ListView嵌套ListView缓存失效问题
- Android Studio从硬盘上彻底删除Project项目
- Android 存储中的内部存储和外部存储
- android Realm数据操作类详解
- Android Studio添加依赖时出现“Manifest merger failed”
- Android开发之通过反射获取到Android隐藏的方法
- Mac和Windows上Android Studio常用的快捷键
- Android Studio 中快速提取方法
- Android SDK与API版本的对应关系
- 自定义listview Android
- Android开发不完整规范
- Android中listView选中第一行,或任意一行(listView优化,很标准)
- 高德地图发布版SHA1获取