日志打印改进

Posted shoshana-kong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日志打印改进相关的知识,希望对你有一定的参考价值。

今天在家写代码,给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();
	        }
	    }
	
}

  

以上是关于日志打印改进的主要内容,如果未能解决你的问题,请参考以下文章

日志打印改进

使用Line Pos Info 和 Modern C++ 改进打印日志记录

webstorm代码片段的创建

在日志中打印当前返回堆栈

常用python日期日志获取内容循环的代码片段

JAVA之AOP