《第一行代码》第二版 学习总结35 定制化日志工具
最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。
因为日志的使用还是蛮频繁的,其实我之前一直有一个不太好的习惯,就是使用sout(System.out),毕竟以前使用了很久的Eclipse(那还是在学习java后台的时候了,不过现在也经常使用,毕竟java还是要不断巩固的);言归正转,日志工具使用的好处请点击查看;那么如何在项目中巧妙的使用日志工具还是很重要,对其实现灵活有效的控制既便于追查问题,也有利于优化程序运行。比如,在项目的不同时期是需要不同日志或者是需不需要日志的区别,如何简单有效的对其实现控制。下面就来看看书中是如何巧妙实现控制的;后续我有可能自己会再写一个灵活度更高的日志工具,今天就先来看看书中的代码吧。
1,示例代码
这里就不在多说了,直接给出代码逻辑,我们定制自己的日志工具的目的只有一个:更好的控制日志打印。
LogUtil.java代码:
package com.hfut.operationselflogtool; import android.util.Log; /** * author:why * created on: 2018/3/27 9:17 * description: 书中原代码,感觉很好,就不想再修改了 */ public class LogUtil { public static final int LOGV=1; public static final int LOGD=2; public static final int LOGI=3; public static final int LOGW=4; public static final int LOGE=5; public static int level=0; public static void v(String tag,String msg){ if(level<=LOGV){ Log.v(tag, msg); } } public static void d(String tag,String msg){ if(level<=LOGD){ Log.d(tag, msg); } } public static void i(String tag,String msg){ if(level<=LOGI){ Log.i(tag, msg); } } public static void w(String tag,String msg){ if(level<=LOGW){ Log.w(tag, msg); } } public static void e(String tag,String msg){ if(level<=LOGE){ Log.e(tag, msg); } } }
MainActivity.java代码:
package com.hfut.operationselflogtool; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //打印所以 public void printAll(View view) { LogUtil.level = LogUtil.LOGV; print(); } //打印Debug及以上 public void printAboveDebug(View view) { LogUtil.level = LogUtil.LOGD; print(); } //打印Info及以上 public void printAboveInfo(View view) { LogUtil.level = LogUtil.LOGI; print(); } //打印Warning及以上 public void printAboveWarning(View view) { LogUtil.level = LogUtil.LOGW; print(); } //打印Error public void printError(View view) { LogUtil.level = LogUtil.LOGE; print(); } private void print() { LogUtil.v(TAG, "printVerbose: "); LogUtil.d(TAG, "printDebug: "); LogUtil.i(TAG, "printInfo: "); LogUtil.w(TAG, "printWarning: "); LogUtil.e(TAG, "printError: "); } }
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.hfut.operationselflogtool.MainActivity"> <Button android:textSize="20dp" android:layout_marginTop="30dp" android:text="打印全部日志" android:onClick="printAll" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:textSize="20dp" android:layout_marginTop="10dp" android:text="打印调试及以上级别日志" android:onClick="printAboveDebug" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:textSize="20dp" android:layout_marginTop="10dp" android:text="打印Info及以上级别日志" android:onClick="printAboveInfo" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:textSize="20dp" android:layout_marginTop="10dp" android:text="打印警告及以上级别日志" android:onClick="printAboveWarning" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:textSize="20dp" android:layout_marginTop="10dp" android:text="打印错误日志" android:onClick="printError" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
2,运行结果
第一步:运行程序
第二步:依次点击上面五个按钮
总结:目前的日志工具,我们就可以通过level变量对日志打印进行很好的控制;但是不能随机选择打印什么日志,当然这种需求也不多,后期我可能会重写一个灵活度更高的日志工具。