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

日志打印改进

时间:2018-08-26 18:30:02      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:split   val   在家   stack   ==   row   rac   owa   []   

今天在家写代码,给rongjun分享了一张我写代码的图片,没想到眼尖的rongjun瞬间发现了日志打印的待提升点。

哈哈。分享给我了个logUtil包,然后直接调用即可。开心,今天 又进步了一点点。如下 

记录并应用之~~

 

package com.yiqifu.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
 * 日志通用类
 * @author rongjun
 *
 */
public class LogUtils {
	 public static void logException(Exception e) {
	        StackTraceElement[] stacks = (new Throwable()).getStackTrace();
	        String newClass = stacks[1].getClassName();
	        StringBuffer logInfo = new StringBuffer(500);
	        logInfo.append("[");
	        logInfo.append(stacks[1].getLineNumber());
	        logInfo.append("]");
	        logInfo.append(stacks[1].getMethodName());
	        StringWriter trace = new StringWriter();
	        e.printStackTrace(new PrintWriter(trace));
	        logInfo.append(" Exception:");
	        logInfo.append(trace.toString());
	        Logger logger = null;
	        try {
	            logger = LoggerFactory.getLogger(Class.forName(newClass).getName());
	            logger.error(logInfo.toString());
	        } catch (ClassNotFoundException e1) {
	            e1.printStackTrace();
	        }
	    }

	    public static void logInfo(String message, Object... args) {
	        StackTraceElement[] stacks = (new Throwable()).getStackTrace();
	        String newClass = stacks[1].getClassName();
	        StringBuffer logInfo = new StringBuffer(500);
	        logInfo.append("[");
	        logInfo.append(stacks[1].getLineNumber());
	        logInfo.append("]");
	        logInfo.append(stacks[1].getMethodName());
	        logInfo.append(" INFO:");
	        if (null != args){
	            for (Object arg : args) {
	                String[] strArr = message.split("\\{\\}", 2);
	                if (arg == null) {
	                    arg = "null";
	                }
	                message = strArr[0] + String.valueOf(arg) + strArr[1];
	            }
	        }
	        logInfo.append(message);
	        Logger logger = null;
	        try {
	            logger = LoggerFactory.getLogger(Class.forName(newClass).getName());
	            logger.info(logInfo.toString());
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        }
	    }

	    public static void logDebug(String message, Object... args) {
	        StackTraceElement[] stacks = (new Throwable()).getStackTrace();
	        String newClass = stacks[1].getClassName();
	        StringBuffer logInfo = new StringBuffer(500);
	        logInfo.append("[");
	        logInfo.append(stacks[1].getLineNumber());
	        logInfo.append("]");
	        logInfo.append(stacks[1].getMethodName());
	        logInfo.append(" DEBUG:");
	        if (null != args){
	            for (Object arg : args) {
	                String[] strArr = message.split("\\{\\}", 2);
	                if (arg == null) {
	                    arg = "null";
	                }
	                message = strArr[0] + String.valueOf(arg) + strArr[1];
	            }
	        }
	        logInfo.append(message);
	        Logger logger = null;
	        try {
	            logger =LoggerFactory.getLogger(Class.forName(newClass).getName());
	            logger.debug(logInfo.toString());
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        }
	    }

	    public static void logError(String message, Object... args) {
	        StackTraceElement[] stacks = (new Throwable()).getStackTrace();
	        String newClass = stacks[1].getClassName();
	        StringBuffer logInfo = new StringBuffer(500);
	        logInfo.append("[");
	        logInfo.append(stacks[1].getLineNumber());
	        logInfo.append("]");
	        logInfo.append(stacks[1].getMethodName());
	        logInfo.append(" INFO:");
	        if (null != args){
	            for (Object arg : args) {
	                String[] strArr = message.split("\\{\\}", 2);
	                if (arg == null) {
	                    arg = "null";
	                }
	                message = strArr[0] + String.valueOf(arg) + strArr[1];
	            }
	        }
	        logInfo.append(message);
	        Logger logger = null;
	        try {
	            logger =LoggerFactory.getLogger(Class.forName(newClass).getName());
	            logger.error(logInfo.toString());
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        }
	    }
	
}

  

日志打印改进

标签:split   val   在家   stack   ==   row   rac   owa   []   

原文地址:https://www.cnblogs.com/shoshana-kong/p/9537780.html

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