Androd封装一个Log打印工具一键实现打印不打印

在写项目的时候,我们是避免不了用到安卓里面的Log打印工具的,但是当代码越写越多的时候我们这加个Log 那加个Log,当我们项目要上线的时候,我们总会忘记哪有Log,很麻烦啊,当时杀人的心都有了,现在封装一个Log工具,只需要定义boolean就可以一键实现打印和不打印功能,直接复制过去拿到项目里面用就行!

不要感谢我,请叫我雷锋

package com.example.qlog;

import java.util.Calendar;

public final class QLog {
	public static final boolean DEBUG = true;//是否打印

    /**
     * Send a {@link #VERBOSE} log message.
     *
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int v(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.v(tag, format);
            }
            return android.util.Log.v(tag, String.format(format, args));
        }
        return 0;
    }

    /**
     * Send a {@link #DEBUG} log message.
     *
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int d(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.d(tag, format);
            }

            return android.util.Log.d(tag, String.format(format, args));
        }
        return 0;
    }

    /**
     * Send an {@link #INFO} log message.
     *
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int i(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.i(tag, format);
            }
            return android.util.Log.i(tag, String.format(format, args));
        }
        return 0;
    }

    /**
     * Send a {@link #WARN} log message.
     *
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int w(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.w(tag, format);
            }
            return android.util.Log.w(tag, String.format(format, args));
        }
        return 0;
    }

    /**
     * Send a {@link #WARN} log message and log the exception.
     *
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param tr An exception to log
     */
    public static int w(String tag, Throwable tr) {
        if (DEBUG) {
            return android.util.Log.w(tag, tr);
        }
        return 0;
    }

    /**
     * Send an {@link #ERROR} log message.
     *
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int e(String tag, String msg, Throwable e) {
        if (DEBUG) {
            return android.util.Log.e(tag, msg, e);
        }
        return 0;
    }

    /**
     * Send an {@link #ERROR} log message.
     *
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int e(String tag, String msg) {
        if (DEBUG) {
            return android.util.Log.e(tag, msg);
        }
        return 0;
    }

    /**
     * Low-level logging call.
     *
     * @param priority The priority/type of this log message
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     * @return The number of bytes written.
     */
    public static int println(int priority, String tag, String msg) {
        if (DEBUG) {
            return android.util.Log.println(priority, tag, msg);
        }
        return 0;
    }

    /**
     * 打印调用时间
     *
     * @param cur ????的时??
     * @return 调用时间
     */
    public static long debugDuration(long cur) {
        long sec = System.currentTimeMillis();
        StackTraceElement elem = Thread.currentThread().getStackTrace()[3];
        d("Performance", elem.getFileName() + "_" + elem.getLineNumber() + ":" + (sec - cur));
        return sec;
    }

    /**
     * 打印当前调用的位置: 文件 行号 方法
     *
     * @param tag
     * @return
     */
    public static int printLogPos(String tag) {
        StackTraceElement elem = Thread.currentThread().getStackTrace()[3];
        return d(tag, elem.getClassName() + ":" + elem.getLineNumber() + "::" + elem.getMethodName());
    }

    /**
     * 获得当前调用位置??类名
     *
     * @param depth 堆栈深度
     * @return
     */
    public static String getLogPos(int depth) {
        StackTraceElement elem = Thread.currentThread().getStackTrace()[depth];
        return elem.getClassName();
    }

    /**
     * depth = 4
     *
     * @see #getLogPos(int)
     * @return
     */
    public static String getLogPos() {
        return getLogPos(4);
    }

    /**
     * 获得当前??code>from</code>????的秒??     *
     * @param from
     * @return
     */
    public static long getSecond(Calendar from) {
        return (System.currentTimeMillis() - from.getTimeInMillis()) / 1000;
    }

    private static final Calendar _20120101 = Calendar.getInstance();
    static {
        _20120101.set(2012, 0, 0, 0, 0, 0);
    }

    /**
     * from = 20120101
     *
     * @see #getSecond(Calendar)
     * @return
     */
    public static long getSecond() {
        return getSecond(_20120101);
    }

}
时间: 2024-08-08 19:34:33

Androd封装一个Log打印工具一键实现打印不打印的相关文章

Python+Selenium进阶版(八)- Python自定义封装一个简单的Log类

目标:如何写一个Python日志类,用来输出不同级别的日志信息到本地文件夹下的日志文件里. 练习场景: 我们需要封装一个简单的日志类,主要有以下内容: 1.生成的日志文件格式是 年月日分秒.log 2.生成的XXX.log文件存储在项目根目录下Logs文件夹下 3.这个日志类,支持INFO,ERROR两种日志级别 4.日志里,每行日志输出,时间日期+执行类名称+日志级别+日志描述 解决思路: 1.在根目录下新建一个Logs的文件夹,获取这个Log的相对路径: 2.日志的保存命名,需要系统时间:

C 封装一个通用链表 和 一个简单字符串开发库

引言 这里需要分享的是一个 简单字符串库和 链表的基库,代码也许用到特定技巧.有时候回想一下, 如果我读书的时候有人告诉我这些关于C开发的积淀, 那么会走的多直啊.刚参加工作的时候做桌面开发, 服务是C++写,界面是C#写.那时候刚进去评级我是中级,因为他问我关于系统锁和信号量都答出来.开发一段 时间,写C#也写的很溜.后面招我那个人让我转行就写C++和php,那时候就开始学习C++有关知识. 后面去四川工作了,开发安卓,用eclipse + java语法 + android jdk,开发前端,

[js高手之路]javascript腾讯面试题学习封装一个简易的异步队列

这道js的面试题,是这样的,页面上有一个按钮,一个ul,点击按钮的时候,每隔1秒钟向ul的后面追加一个li, 一共追加10个,li的内容从0开始技术( 0, 1, 2, ....9 ),首先我们用闭包封装一个创建li元素的函数. 1 var create = (function(){ 2 var count = 0; 3 return function(){ 4 var oLi = document.createElement( "li" ); 5 oLi.innerHTML = co

封装一个Ajax工具函数

/*封装一个ajax工具函数*/ window.$ = {}; /*通过$定义一个ajax函数*/ /* * 1. type   string   请求的方式  默认是get * 2. url    string   请求地址  接口地址 * 3. async  boolean  默认的是true * 4. data   object   {}请求数据 * * 5.success function  成功回调函数 * 6.error   function  失败的回调函数 * */ $.ajax

C 封装一个简单二叉树基库

引文 今天分享一个喜欢佩服的伟人,应该算人类文明极大突破者.收藏过一张纸币类型如下 那我们继续科普一段关于他的简介 '高斯有些孤傲,但令人惊奇的是,他春风得意地度过了中产阶级的一生,而  没有遭受到冷酷现实的打击:这种打击常无情地加诸于每个脱离现实环境生活的  人.或许高斯讲求实效和追求完美的性格,有助于让他抓住生活中的简单现实.  高斯22岁获博士学位,25岁当选圣彼德堡科学院外籍院士,30岁任哥廷根大学数  学教授兼天文台台长.虽说高斯不喜欢浮华荣耀,但在他成名后的五十年间,这  些东西就像

数组的方法(连接,截取,删除,插入,替换,以及封装一个函数)

连接两个数组:concat,形成一个新数组 数组1.concat(数组2,数组1) 返回值:数组 var arr1=[1,2], arr2=[3,4], arr3; arr3=arr1.concat(arr2,arr1); console.log(arr3); 另外方法,截取<是来是>__.slice(start,end) ___.slice(start,end)功能:从已有的数组中返回选定的元素 参数 如果参数有一个是负数,则用数组长度加上该数来确定相应的位置 start(必须)规定从何处开

自己封装一个弹框插件

弹出层提示信息,这是移动前端开发中最常见的需求,你可能会想到一些流行的弹框插件,比如 经典的artDialog 炫酷的Sweetalert等等.. 但是慢慢地你其实会发现通常情况下需求定制化要求较高,一般的弹框插件可能只满足大部分要求,自定义花的时间还不如手动自己封装一个符合自己开发习惯的弹框组件,这样后续开发效率将大大提高. 首先整理一下思路,原生javascript其实是有实现alert()方法的,但是那个会暂时性中断程序运行,并且足以让你丑拒!那么抛开这些细细一想,其实弹框就是两个div层

封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil

封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,代码比较简单,主要是把MongoTarget的配置.FileTarget的配置集成到类中,同时利用缓存依赖来判断是否需要重新创建Logger类,完整代码如下: using NLog; using NLog.Config; using NLog.Mongo; using NLog.Targets; using System; using System.Collections.Generic; using System.IO;

android网页打印,安卓网页打印,h5页面打印,浏览器打印,js打印工具

Android设备打印比较麻烦,一般设备厂商都提供原生app开发的SDK,我们web开发者为难了,不会原生开发啊 给大家提供一个思路,实现web加壳,利用打印浏览器实现 简单来说就是把我们的web页面嵌入浏览器中 web页面的打印功能通过js与浏览器互动 浏览器通过调用硬件SDK实现打印 1.机器安装最新SDK,已安装请忽略 2.下载安装本页下载连接中的打印浏览器并安装 3.对照下方事例代码,修改web页面打印功能 4.打印浏览器中打开web页面,测试打印功能 5.默认主页可以在SD卡根目录修改