打印dubbo接口所有请求数据日志

import com.ctl.util.StringUtil;
import net.sf.json.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * <p>Title: DubboServiceAspect</p>
 * <p>Description: 打印dubbo服务请求数据</p>
 * <p>Copyright: Copyright (c) 2018</p>
 * @version 1.0
 * @date 2018-11-09 13:07
 */
public class DubboServiceAspect {
    private Logger logger = LoggerFactory.getLogger(DubboServiceAspect.class);
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        Object classNameAndMethodName = proceedingJoinPoint.toString();
        StringBuilder queryJson = new StringBuilder();
        if (args != null && args.length > 0) {
            try {
                queryJson.append(StringUtil.formatJson(JSONObject.fromObject(args[0]).toString())).append("\n");
            } catch (Exception e) {
            }
//            for (int i = 1; i < args.length; i++) {
//                if (args[i].getClass().getSimpleName().startsWith("java")) {
//                    queryJson.append("\targs[").append(i).append("]=").append(args[i]).append("\t");
//                }
//            }
            logger.info("classNameAndMethodName={},\tqueryJson={}", classNameAndMethodName, queryJson);
        }
        return proceedingJoinPoint.proceed();

    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
   <bean id="dubboServuceAspect" class="com.*.aspect.DubboServiceAspect" />
   <!--统一验证小程序调用接口时是否带有合法的securityKey,合法则继续走,不合法则直接拦截-->
   <aop:config>
      <aop:aspect id="dubboServuce" ref="dubboServuceAspect" order="1">
         <!-- 配置一个切点 -->
         <aop:pointcut id="dubboServucePoint" expression="execution(* com.*.*.service.impl.*.*(..))"  />
         <aop:around method="around" pointcut-ref="dubboServucePoint"/>
      </aop:aspect>
   </aop:config>
</beans>

 StringUtil.java

public class StringUtil {
    private static Logger logger = LoggerFactory.getLogger(StringUtil.class);
    public static JsonConfig jsonConfig = null;

    static {
        jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
        jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateValueProcessor());
        jsonConfig.registerJsonValueProcessor(Integer.class, new JsonNumberValueProcessor());
        jsonConfig.registerJsonValueProcessor(Long.class, new JsonNumberValueProcessor());
        jsonConfig.registerJsonValueProcessor(Byte.class, new JsonNumberValueProcessor());
        jsonConfig.registerJsonValueProcessor(Float.class, new JsonNumberValueProcessor());
        jsonConfig.registerJsonValueProcessor(Double.class, new JsonNumberValueProcessor());
    }
    /**
     * 单位缩进字符串。
     */
    private static String SPACE = "   ";
    private static String NEWLINE="\n";
    /**
     * 判断是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * 判断是否为空去除空字符
     * @param str
     * @return
     */
    public static boolean isEmptyTrim(String str) {
        return str == null || str.trim().length() == 0;
    }

    /**
     * 判断字符串数组是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String[] str) {
        return str == null || str.length == 0;
    }
    /**
     * sql防止注入替换
     * @param paramStr
     * @return
     */
    public static  String sqlResplace(String paramStr) {
        if (StringUtil.isEmptyTrim(paramStr)) {
            logger.info("sqlResplace={}为空", paramStr);
            return null;
        } else {
            logger.info("sqlResplace={}", paramStr);
        }
        StringBuilder strDest = new StringBuilder();
        for (int i = 0; i < paramStr.length(); i++) {
            char ch = paramStr.charAt(i);
            switch (ch) {
                case '\0':
                    strDest.append("\\0");
                    break;
                case '\n':
                    strDest.append("\\n");
                    break;
                case '\r':
                    strDest.append("\\r");
                    break;
                case '\'':
                    strDest.append("\\'");
                    break;
                case '"':
                    strDest.append("\\\"");
                    break;
                case '\\':
                    strDest.append("\\\\");
                    break;
                case '%':
                    strDest.append("\\%");
                    break;
                case '_':
                    strDest.append("\\_");
                    break;
                default:
                    strDest.append(ch);
                    break;
            }
        }
        return strDest.toString();
    }

    /**
     * 返回格式化JSON字符串。
     *
     * @param json 未格式化的JSON字符串。
     * @return 格式化的JSON字符串。
     */
    public static String formatJson(String json) {
        if (StringUtil.isEmptyTrim(json)) {
            logger.info("json={}为空", json);
            return null;
        }
        try {
            StringBuffer result = new StringBuffer();

            int length = json.length();
            int number = 0;
            char key = 0;

            //遍历输入字符串。
            for (int i = 0; i < length; i++) {
                //1、获取当前字符。
                key = json.charAt(i);

                //2、如果当前字符是前方括号、前花括号做如下处理:
                if ((key == '[') || (key == '{')) {
                    //(1)如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。
                    if ((i - 1 > 0) && (json.charAt(i - 1) == ':')) {
                        result.append(NEWLINE);
                        result.append(indent(number));
                    }

                    //(2)打印:当前字符。
                    result.append(key);

                    //(3)前方括号、前花括号,的后面必须换行。打印:换行。
                    result.append(NEWLINE);

                    //(4)每出现一次前方括号、前花括号;缩进次数增加一次。打印:新行缩进。
                    number++;
                    result.append(indent(number));

                    //(5)进行下一次循环。
                    continue;
                }

                //3、如果当前字符是后方括号、后花括号做如下处理:
                if ((key == ']') || (key == '}')) {
                    //(1)后方括号、后花括号,的前面必须换行。打印:换行。
                    result.append(NEWLINE);

                    //(2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。
                    number--;
                    result.append(indent(number));

                    //(3)打印:当前字符。
                    result.append(key);

                    //(4)如果当前字符后面还有字符,并且字符不为“,”,打印:换行。
                    if (((i + 1) < length) && (json.charAt(i + 1) != ',')) {
                        result.append(NEWLINE);
                    }

                    //(5)继续下一次循环。
                    continue;
                }

                //4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。
                if ((key == ',')) {
                    result.append(key);
                    result.append(NEWLINE);
                    result.append(indent(number));
                    continue;
                }

                //5、打印:当前字符。
                result.append(key);
            }

            return result.toString();
        } catch (Exception e) {
            logger.error("格式化jsonStr失败:" + json, e);
            return json;
        }
    }

    /**
     * 返回指定次数的缩进字符串。每一次缩进三个空格,即SPACE。
     *
     * @param number 缩进次数。
     * @return 指定缩进次数的字符串。
     */
    private static String indent(int number) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < number; i++) {
            result.append(SPACE);
        }
        return result.toString();
    }
    /**
     * 根据imgUrl获取imgPath失败
     * @param imgUrl
     * @return
     */
    public static String imagePath(String imgUrl) {
        String imageUrlPath = "";
        try {
            if(isEmptyTrim(imgUrl)){
                return null;
            }
            int index = -1;
//            switch (imgUrl) {
//                case "http://":
//                    index = imgUrl.indexOf("/", 7);
//                    if (index > 0) {
//                        imageUrlPath = imgUrl.substring(index + 1);
//                        logger.info("imageUrl={},imageUrlPath={}", imgUrl, imageUrlPath);
//                    }
//                    break;
//                case "https://":
//                    index = imgUrl.indexOf("/", 8);
//                    if (index > 0) {
//                        imageUrlPath = imgUrl.substring(index + 1);
//                        logger.info("imageUrl={},imageUrlPath={}", imgUrl, imageUrlPath);
//                    }
//                    break;
//                default:
//                    imageUrlPath = imgUrl;
//                    break;
//            }
            if (imgUrl.startsWith("http://")) {
                index = imgUrl.indexOf("/", 7);
                if (index > 0) {
                    imageUrlPath = imgUrl.substring(index + 1);
                    logger.info("imageUrl={},imageUrlPath={}", imgUrl, imageUrlPath);
                }
            }
            if (imgUrl.startsWith("https://")) {
                index = imgUrl.indexOf("/", 8);
                if (index > 0) {
                    imageUrlPath = imgUrl.substring(index + 1);
                    logger.info("imageUrl={},imageUrlPath={}", imgUrl, imageUrlPath);
                }
            }
            if(!imgUrl.startsWith("http://")&&!imgUrl.startsWith("https://")){
                imageUrlPath = imgUrl;
            }
            return imageUrlPath;
        } catch (Exception e) {
            logger.error("根据imgUrl获取imgPath失败",e);
        }
        return imageUrlPath;
    }

    public static void main(String[] args) {
         
    }
}
发布了193 篇原创文章 · 获赞 67 · 访问量 46万+

猜你喜欢

转载自blog.csdn.net/CTLLIN/article/details/83928979
今日推荐