文章目录

  • 环境
  • 一、配置
  • 二、Springboot项目添加接口入参统一打印



环境

springboot 2.3.2.RELEASE

关于logging.level

# ========================logging 日志相关的配置=====================
#日志级别 trace<debug<info<warn<error<fatal
#默认级别为info,即默认打印info及其以上级别的日志,如下:
#系统默认,全局root配置的日志形式,可以注释掉
logging.level.root=warn
#开发人员自己设置的包结构,对那个package进行什么级别的日志监控
logging.level.com.xiaozhi.redis=info
#开发人员自定义日志路径和日志名称
logging.file.name=D:/mylogs2/logs/redis0511.log
#%d{HH:mm:ss.SSS}――日志输出时间
#%thread――输出日志的进程名字,这在Web应用以及异步任务处理中很有用
#%-5level――日志级别,并且使用5个字符靠左对齐
#%logger- ――日志输出者的名字
#%msg――日志消息
#%n――平台的换行符
#logging.pattern.console:控制台的日志输出格式
#logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n 
#logging.pattern.file:文件的日志输出格式
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n

由于从Spring Boot 2.1.x版本开始,将这些日志的打印级别做了调整:从原来的INFO调整为TRACE。所以,当我们希望在应用启动的时候打印这些信息的话,只需要在配置文件增增加对RequestMappingHandlerMapping类的打印级别设置即可。现在的打印内容根据接口创建的Controller类做了分类打印,这样更有助于开发者根据自己编写的Controller来查找初始化了那些HTTP接口。

如:

springboot 订单打印机 springboot 打印请求_序列化


在控制台只需要搜索CommonSessionTimeoutController就能把快速找到这个类的相关api

一、配置

修改配置文件
application.properties

logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping = trace

application.yml

logging:
level:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping:trace

控制台显示

2020-02-11 15:36:09.787 TRACE 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : 
    c.d.c.UserController:
    {PUT /users/{id}}: putUser(Long,User)
    {GET /users/{id}}: getUser(Long)
    {POST /users/}: postUser(User)
    {GET /users/}: getUserList()
    {DELETE /users/{id}}: deleteUser(Long)
2020-02-11 15:36:09.791 TRACE 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : 
    o.s.b.a.w.s.e.BasicErrorController:
    { /error}: error(HttpServletRequest)
    { /error, produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)
2020-02-11 15:36:09.793 DEBUG 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : 7 mappings in 'requestMappingHandlerMapping'

提示:以下是本篇文章正文内容,下面案例可供参考

二、Springboot项目添加接口入参统一打印

需求:要求接口被调用时要打印被调用方法名,以及入参情况,参数格式化时选择fastjson
注:使用fastjson序列化时脱敏,建议入参统一使用自定义的对象类型作为入参
如果不需要参数脱敏,直接使用增强中相关代码,并去除参数脱敏相关代码即可

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ParamInfo {
    /**
     * 取消统一打印参数
     * 默认为false统一打印
     * 如需自定义参数打印 请赋值为true
     */
    boolean unPrint() default false;
    /**
     * 需要脱敏的字段,如密码等
     */
    String[] fields() default {};
}

自定义序列化规则

/**
 * 序列化过滤器:值替换
 *
 */
public class ReplaceFieldFilter implements ValueFilter {
    /**
     * 需要进行替换的属性名和替换值
     * key:属性名
     * value:替换值
     */
    private Map<String, Object> fieldMap;
    public ReplaceFieldFilter() {
    }
    public ReplaceFieldFilter(Map<String, Object> fieldMap) {
        this.fieldMap = fieldMap;
    }
    @Override
    public Object process(Object o, String name, Object value) {
        if(!CollectionUtils.isEmpty(fieldMap)){
            Iterator<Map.Entry<String, Object>> iterator = fieldMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                if(next.getKey().equalsIgnoreCase(name)){
                    return next.getValue();
                }
            }
        }
        return value;
    }
    public Map<String, Object> getFieldMap() {
        return fieldMap;
    }
    public void setFieldMap(Map<String, Object> fieldMap) {
        this.fieldMap = fieldMap;
    }
    /**
     * 传入需要脱敏的字段名,序列化时格式化为 * 号
     */
    public ReplaceFieldFilter(String... fields) {
        String str = "******";
        fieldMap = new HashMap<>(4);
        for (String field : fields) {
            fieldMap.put(field, str);
        }
    }
}
写参数打印增强,这里选择环绕增强

/**
 * 序列化过滤器:值替换
 *
 */
public class ReplaceFieldFilter implements ValueFilter {
    /**
     * 需要进行替换的属性名和替换值
     * key:属性名
     * value:替换值
     */
    private Map<String, Object> fieldMap;
    public ReplaceFieldFilter() {
    }
    public ReplaceFieldFilter(Map<String, Object> fieldMap) {
        this.fieldMap = fieldMap;
    }
    @Override
    public Object process(Object o, String name, Object value) {
        if(!CollectionUtils.isEmpty(fieldMap)){
            Iterator<Map.Entry<String, Object>> iterator = fieldMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                if(next.getKey().equalsIgnoreCase(name)){
                    return next.getValue();
                }
            }
        }
        return value;
    }
    public Map<String, Object> getFieldMap() {
        return fieldMap;
    }
    public void setFieldMap(Map<String, Object> fieldMap) {
        this.fieldMap = fieldMap;
    }
    /**
     * 传入需要脱敏的字段名,序列化时格式化为 * 号
     */
    public ReplaceFieldFilter(String... fields) {
        String str = "******";
        fieldMap = new HashMap<>(4);
        for (String field : fields) {
            fieldMap.put(field, str);
        }
    }
}

参数打印增强,这里选择环绕增强

@Component
@Aspect
//表示增强的执行顺序,如果多个增强,数值小的先被执行
@Order(0)
public class ParamInfoAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ParamInfoAspect.class);
    @Around("execution(* com.service.impl.*.*(..))")
    public Object printParam(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        String requestId = RandomStringUtils.randomAlphanumeric(16);
        Object returnValue = null;
        try {
            Object[] args = joinPoint.getArgs();
            // 获取方法对象
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            //通过注解获取脱敏字段,之后初始化fieldMap,完成字段脱敏
            ParamInfo annotation = method.getAnnotation(ParamInfo.class);
            Map<String, Object> fieldMap = new HashMap<>(4);
            fieldMap.put("password", "******");
            if (annotation != null) {
                //获取需要脱敏的字段名数组
                String[] fields = annotation.fields();
                for (String field : fields) {
                    fieldMap.put(field, "******");
                }
            }
            String param;
            //参数整合,多字段入参整合为对象,单个对象入参格式不变
            if (args.length > 1 || (args.length == 1 && args[0].getClass() == String.class)) {
                Map<String, Object> paramMap = new LinkedHashMap<>();
                String[] parameterNames = signature.getParameterNames();
                for (int i = 0; i < parameterNames.length; i++) {
                    paramMap.put(parameterNames[i], args[i]);
                }
                param = "[" + JSON.toJSONString(paramMap, new ReplaceFieldFilter(fieldMap)) + "]";
            } else {
                param = JSON.toJSONString(args, new ReplaceFieldFilter(fieldMap));
            }
            String methodName = method.getName();
            LOGGER.info("method:[{}], parameter:{}, requestId:[{}]", methodName, param, requestId);
            returnValue = joinPoint.proceed();
            return returnValue;
        } catch (Exception e) {
            LOGGER.error("system is error:", e);
   //可在这里定义程序异常时的错误返回值
            returnValue = ErrorCode.SYSTEM_ERROR;
            return returnValue;
        } finally {
            LOGGER.info("request cost:{}ms, requestId:[{}]", System.currentTimeMillis() - startTime, requestId);
            LOGGER.info("returnValue:[{}], requestId:[{}]", returnValue, requestId);
        }
    }
}