工具类Log

来源:互联网 发布:苏州网络耗材批发 编辑:程序博客网 时间:2024/05/17 22:08

在这里分享一个工具类Log,简化打印Log的代码书写而且可以关闭Log的工具类

工具类

package com.pc.jiyuan.logtest;import android.annotation.SuppressLint;import android.util.Log;/** * * 打印log日志 可关闭,e红色 w橙色 i绿色 d蓝色 v黑色 */@SuppressLint("DefaultLocale")public class L {// !!! NOTE !!!// TODO: set it true when build for release versionpublic final static boolean mode_for_release = MyApplication.isRelease;public final static boolean server_switch = true;public final static String TAG = "livechannel";public static void I(String msg){if (!mode_for_release){Log.i("dream", msg);}}public static void v(String tag, String msg) {if (!mode_for_release)Log.v(tag, msg);}public static void v(String tag, String type, String msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%s", tag, type, msg);Log.v(TAG, des);}}public static void v(String tag, String type, String msg, String msg1) {if (!mode_for_release) {String des = String.format("[%s][%s]%s%s", tag, type, msg, msg1);Log.v(TAG, des);}}public static void v(String tag, String type, int msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%d", tag, type, msg);Log.v(TAG, des);}}public static void v(String tag, String type, boolean msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%s", tag, type, msg ? "true": "false");Log.v(TAG, des);}}public static void i(String tag, String type, String msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%s", tag, type, msg);Log.i(TAG, des);}}@SuppressLint("DefaultLocale")public static void i(String tag, String type, int msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%d", tag, type, msg);Log.v(TAG, des);}}public static void i(String tag, String msg) {i(tag, "", msg);}public static void i(String tag, String type, boolean msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%s", tag, type, msg ? "true": "false");Log.v(TAG, des);}}public static void e(String tag, String type, String msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%s", tag, type, msg);Log.e(TAG, des);}}@SuppressLint("DefaultLocale")public static void e(String tag, String type, int msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%d", tag, type, msg);Log.e(TAG, des);}}@SuppressLint("DefaultLocale")public static void e(String tag, String type, boolean msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%d", tag, type, msg ? "true": "false");Log.e(TAG, des);}}}

它所依赖的文件


package com.pc.jiyuan.logtest;import android.app.Application;import java.util.Map;/** *  * Created by www_and on 2015/10/27. */public class MyApplication extends Application {    public static boolean isRelease = false;// 设置打印日志 ,为true的时候为关闭      }

记得在Manifest文件application中注册

android:name = ".MyApplication"
我自己在学习这个工具类的时候,打印其他的Log比如
public static void v(String tag, String type, String msg) {if (!mode_for_release) {String des = String.format("[%s][%s]%s", tag, type, msg);Log.v(TAG, des);}}


显示不出结果,不知道是为什么,有小伙伴知道,欢迎留言,谢谢。





0 0
原创粉丝点击