• 日志打印改进


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

      

  • 相关阅读:
    Git 开发、合并、提交的一些常见命令语句
    selenium自动化过程中窗口句柄的问题
    git 忽略文件夹下面的文件,但是保留空文件夹
    python接口测试中不同的请求方式
    python接口测试中发送请求中的一些参数
    python接口测试中的session运用
    接口测试数据依赖处理
    pygame的安装
    Bottstrap的基本用法
    前端CSS
  • 原文地址:https://www.cnblogs.com/shoshana-kong/p/9537780.html
Copyright © 2020-2023  润新知