码迷,mamicode.com
首页 > 其他好文 > 详细

log日志应用 自定义的log

时间:2016-09-05 12:14:29      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

package com.kxd.utils;

import java.util.Hashtable;

import android.util.Log;

/**
 * The class for print log
 * 
 */
public class MyLogger {
    private final static boolean logFlag = true; // 调试模式 true 上线模式 false

    public final static String tag = "远程银行";// 日志打印 tag
    private final static int logLevel = Log.VERBOSE; // 日志级别
    // 集合
    private static Hashtable<String, MyLogger> sLoggerTable = new Hashtable<String, MyLogger>();
    private String mClassName;

    // 不同开发人员的日志使用对象
    private static MyLogger djyLog;

    // 开发人员的名字
    private static final String DJY = "djy";

    private MyLogger(String name) {
        mClassName = name;
    }

    /**
     * 
     * @param className
     * @return
     */
    @SuppressWarnings("unused")
    private static MyLogger getLogger(String className) {
        MyLogger classLogger = (MyLogger) sLoggerTable.get(className);
        if (classLogger == null) {
            classLogger = new MyLogger(className);
            sLoggerTable.put(className, classLogger);
        }
        return classLogger;
    }

    /**
     * Purpose:Mark user djy
     * 
     * @return
     */
    public static MyLogger getDJYLog() {
        if (djyLog == null) {
            djyLog = new MyLogger(DJY);
        }
        return djyLog;
    }

    /**
     * Get The Current Function Name
     * 
     * @return
     */
    private String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        if (sts == null) {
            return null;
        }
        for (StackTraceElement st : sts) {
            if (st.isNativeMethod()) {
                // 本地方法native jni
                continue;
            }
            if (st.getClassName().equals(Thread.class.getName())) {
                // 线程
                continue;
            }
            if (st.getClassName().equals(this.getClass().getName())) {
                // 构造方法
                continue;
            }
            return mClassName + " [ 线程:" + Thread.currentThread().getName()
                    + " - 文件名: " + st.getFileName() + " - 行数:"
                    + st.getLineNumber() + " - 方法名:" + st.getMethodName()
                    + " ]";
        }
        return null;
    }

    /**
     * The Log Level:i
     * 
     * log等级:i
     * 
     * @param str
     */
    public void i(Object str) {
        if (logFlag) {
            if (logLevel <= Log.INFO) {
                String name = getFunctionName();
                if (name != null) {
                    Log.i(tag, name + " - " + str);
                } else {
                    Log.i(tag, str.toString());
                }
            }
        }

    }

    /**
     * The Log Level:i
     * 
     * log等级:i
     * 
     * @param str
     */
    public void i(String tag, Object str) {
        if (logFlag) {
            if (logLevel <= Log.INFO) {
                String name = getFunctionName();
                if (name != null) {
                    Log.i(tag, name + " - " + str);
                } else {
                    Log.i(tag, str.toString());
                }
            }
        }

    }

    /**
     * The Log Level:d
     * 
     * log等级:d
     * 
     * @param str
     */
    public void d(Object str) {
        if (logFlag) {
            if (logLevel <= Log.DEBUG) {
                String name = getFunctionName();
                if (name != null) {
                    Log.d(tag, name + " - " + str);
                } else {
                    Log.d(tag, str.toString());
                }
            }
        }
    }

    /**
     * The Log Level:V
     * 
     * log等级:v
     * 
     * @param str
     */
    public void v(Object str) {
        if (logFlag) {
            if (logLevel <= Log.VERBOSE) {
                String name = getFunctionName();
                if (name != null) {
                    Log.v(tag, name + " - " + str);
                } else {
                    Log.v(tag, str.toString());
                }
            }
        }
    }

    /**
     * The Log Level:w
     * 
     * log等级:w
     * 
     * @param str
     */
    public void w(Object str) {
        if (logFlag) {
            if (logLevel <= Log.WARN) {
                String name = getFunctionName();
                if (name != null) {
                    Log.w(tag, name + " - " + str);
                } else {
                    Log.w(tag, str.toString());
                }
            }
        }
    }

    /**
     * The Log Level:e
     * 
     * log等级:e
     * 
     * @param str
     */
    public void e(Object str) {
        if (logFlag) {
            if (logLevel <= Log.ERROR) {
                String name = getFunctionName();
                if (name != null) {
                    Log.e(tag, name + " - " + str);
                } else {
                    Log.e(tag, str.toString());
                }
            }
        }
    }

    /**
     * The Log Level:e
     * 
     * log等级:e
     * 
     * @param ex
     */
    public void e(Exception ex) {
        if (logFlag) {
            if (logLevel <= Log.ERROR) {
                Log.e(tag, "error", ex);
            }
        }
    }

    /**
     * The Log Level:e
     * 
     * log等级:e
     * 
     * @param log
     * @param tr
     */
    public void e(String log, Throwable tr) {
        if (logFlag) {
            String line = getFunctionName();
            Log.e(tag, "{Thread:" + Thread.currentThread().getName() + "}"
                    + "[" + mClassName + line + ":] " + log + "\n", tr);
        }
    }
}

 

log日志应用 自定义的log

标签:

原文地址:http://www.cnblogs.com/fengchuxiaodai/p/5841567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!