首页 > 其他 > 详细

开源TT框架上的日志类

时间:2018-02-14 15:31:05      阅读:26      评论:0      收藏:0      [点我收藏+]

标签:views   fin   -m   erro   getclass   msg   equals   line   pri   

public class Logger {
	/**
	 * log tag
	 */
	private String tagName = "MoGuLogger";// tag name

	// private static int logLevel = Log.VERBOSE;

	private static int logLevel = Log.VERBOSE;

	private static Logger inst;

	private Lock lock;

	private Logger() {
		lock = new ReentrantLock();
	}

	public static synchronized Logger getLogger(Class<?> key) {
		if (inst == null) {
			inst = new Logger();
		}

		return inst;
	}

	private String getFunctionName() {
		StackTraceElement[] sts = Thread.currentThread().getStackTrace();

		if (sts == null) {
			return null;
		}

		Log.i("TAG", "start" + "threadName:" + Thread.class.getName() + " className:"
				+ this.getClass().getName());
		for (StackTraceElement st : sts) {
			Log.i("TAG", st.getClassName());
		}
		Log.i("TAG", "end");
		for (StackTraceElement st : sts) {
			Log.i("TAG", st.getClassName());
			if (st.isNativeMethod()) {
				continue;
			}

			if (st.getClassName().equals(Thread.class.getName())) {
				continue;
			}

			if (st.getClassName().equals(this.getClass().getName())) {
				continue;
			}
			return "[" + st.getFileName() + ":" + st.getLineNumber() + "]";
		}

		return null;
	}

	private String createMessage(String msg) {
		String functionName = getFunctionName();
		long threadId = Thread.currentThread().getId();
		String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date());
		String message = (functionName == null ?

msg : (functionName + " - " + String.valueOf(threadId) + " - " + msg)); return currentTime + " - " + message; } /** * log.i */ public void i(String format, Object... args) { if (logLevel <= Log.INFO) { lock.lock(); try { String message = createMessage(getInputString(format, args)); Log.i(tagName, message); } finally { lock.unlock(); } } } /** * log.v */ public void v(String format, Object... args) { if (logLevel <= Log.VERBOSE) { lock.lock(); try { String message = createMessage(getInputString(format, args)); Log.v(tagName, message); } finally { lock.unlock(); } } } /** * log.d */ public void d(String format, Object... args) { if (logLevel <= Log.DEBUG) { lock.lock(); try { String message = createMessage(getInputString(format, args)); Log.d(tagName, message); } finally { lock.unlock(); } } } /** * log.e */ public void e(String format, Object... args) { if (logLevel <= Log.ERROR) { lock.lock(); try { String message = createMessage(getInputString(format, args)); Log.e(tagName, message); } finally { lock.unlock(); } } } private String getInputString(String format, Object... args) { if (format == null) { return "null log format"; } return String.format(format, args); } /** * log.error */ public void error(Exception e) { if (logLevel <= Log.ERROR) { StringBuffer sb = new StringBuffer(); lock.lock(); try { 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"); } } } Log.e(tagName, sb.toString()); } finally { lock.unlock(); } } } /** * log.d */ public void w(String format, Object... args) { if (logLevel <= Log.WARN) { lock.lock(); try { String message = createMessage(getInputString(format, args)); Log.w(tagName, message); } finally { lock.unlock(); } } } /** * set log level */ public void setLevel(int l) { lock.lock(); try { logLevel = l; } finally { lock.unlock(); } } }


开源TT框架上的日志类

标签:views   fin   -m   erro   getclass   msg   equals   line   pri   

原文:https://www.cnblogs.com/llguanli/p/8448373.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号