首页 / 日志 / Android开发日志统一管理
Android开发日志统一管理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android开发日志统一管理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2157字,纯文字阅读大概需要4分钟。
内容图文
在开发中,我们通常要对日志的输出做统一管理,下面就为大家推荐一个日志输出类,在开发阶段只需将DEBUG常量设为true,生产环境将DEBUG设为false即可控制日志的输出。啥都不说了,需要的朋友直接带走。
package com.android.util; /** * 日志输出统一管理 * * @author 祁连山 * @date 2015-04-27 * @version 1.0 * */ public class Log { private static final String TAG = "com.android.app"; privatestaticfinalboolean DEBUG = true; privatestatic String getFunctionName() { StackTraceElement[] sts = Thread.currentThread().getStackTrace(); if (sts == null) { returnnull; } for (StackTraceElement st : sts) { if (st.isNativeMethod()) { continue; } if (st.getClassName().equals(Thread.class.getName())) { continue; } if (st.getClassName().equals(Log.class.getName())) { continue; } return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() + "): " + st.getFileName() + ":" + st.getLineNumber() + "]"; } returnnull; } privatestatic String createMessage(String msg) { String functionName = getFunctionName(); String message = (functionName == null ? msg : (functionName + " - " + msg)); return message; } publicstaticvoid i(String msg) { if (DEBUG) { String message = createMessage(msg); android.util.Log.i(TAG, message); } } publicstaticvoid v(String msg) { if (DEBUG) { String message = createMessage(msg); android.util.Log.v(TAG, message); } } publicstaticvoid d(String msg) { if (DEBUG) { String message = createMessage(msg); android.util.Log.d(TAG, message); } } publicstaticvoid e(String msg) { if (DEBUG) { String message = createMessage(msg); android.util.Log.e(TAG, message); } } publicstaticvoid w(String msg) { if (DEBUG) { String message = createMessage(msg); android.util.Log.w(TAG, message); } } publicstaticvoid e(Exception e) { if (DEBUG) { StringBuffer sb = new StringBuffer(); String name = getFunctionName(); StackTraceElement[] sts = e.getStackTrace(); if (name != null) { sb.append(name + " - " + e + "\r\n"); } else { sb.append(e + "\r\n"); } if (sts != null && sts.length > 0) { for (StackTraceElement st : sts) { if (st != null) { sb.append("[ " + st.getFileName() + ":" + st.getLineNumber() + " ]\r\n"); } } } android.util.Log.e(TAG, sb.toString()); } } }
原文:http://www.cnblogs.com/magics/p/4484803.html
内容总结
以上是互联网集市为您收集整理的Android开发日志统一管理全部内容,希望文章能够帮你解决Android开发日志统一管理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。