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