zoukankan      html  css  js  c++  java
  • 开源TT框架上的日志类

    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 + " "); } else { sb.append(e + " "); } if (sts != null && sts.length > 0) { for (StackTraceElement st : sts) { if (st != null) { sb.append("[ " + st.getFileName() + ":" + st.getLineNumber() + " ] "); } } } 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(); } } }


  • 相关阅读:
    如何使用Arrays工具类操作数组
    Java 内存模型详解
    HashSet源码分析:JDK源码系列
    在ASP.NET Core中用HttpClient(六)——ASP.NET Core中使用HttpClientFactory
    在ASP.NET Core中用HttpClient(五)——通过CancellationToken取消HTTP请求
    在ASP.NET Core中用HttpClient(四)——提高性能和优化内存
    ASP.NET Core与Redis搭建一个简易分布式缓存
    在ASP.NET Core中用HttpClient(三)——发送HTTP PATCH请求
    在ASP.NET Core中用HttpClient(二)——发送POST, PUT和DELETE请求
    在ASP.NET Core中用HttpClient(一)——获取数据和内容
  • 原文地址:https://www.cnblogs.com/llguanli/p/8448373.html
Copyright © 2011-2022 走看看