Androd封装一个Log打印工具一键实现打印不打印
2014-09-16 17:16
429 查看
在写项目的时候,我们是避免不了用到安卓里面的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);
}
}
不要感谢我,请叫我雷锋
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);
}
}
相关文章推荐
- 封装一个字符数组,实现增加,删除,替换,打印,搜索等功能
- 【Android】封装一个简单好用的打印Log的工具类
- 【Log日志】一个封装的日志打印库(显示类,方法,行数)--破晓觉醒
- 一个打印Log日志,好用的工具logger
- 设计并实现一个LogService,应用开发时可以打印log到视图(TextView)中显示
- Logger:封装系统log的日志打印工具
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
- 【Android】封装一个简单好用的打印Log的工具类
- VC8实现的一个PC端查看TSK主题文件的工具,以及由其引出的VC8MFC程序脱离.net框架分发问题
- J2EE 0.7.1—报表功能。传入封装数据的list实现打印标签报表。。
- 一个通用的调用shell命令及打印退出状态的C语言实现
- 自己实现的一个directxinput键盘的封装
- c#实现一个自动关机的小工具
- bash实现一个简单的错误(mutation)注入工具
- 用匿名类的方式实现一个线程,并打印
- 一个很好的打印程序log到文件的类
- 我的本科毕业设计(非水文,设计了一个新算法):一种字符编码猜测工具的实现方法
- Visual Paradigm - 一个用于绘制UML的java实现的一个工具软件
- Visual Paradigm - 一个用于绘制UML的java实现的一个工具软件
- 发一个自己用JS写的实用看图工具实现代码