springboot+日志系统日志

LogResponseBodyAdvice

package com.wpmt.framework.aspectj;
import javax.servlet.http.HttpServletRequest;

import org.jboss.logging.MDC;
import org.springframework.context.annotation.Profile;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.alibaba.fastjson.JSON;
import com.wpmt.common.util.IpUtils;

import lombok.extern.slf4j.Slf4j;


@ControllerAdvice
@Slf4j
@Profile({"th","bch"})
public class LogResponseBodyAdvice implements ResponseBodyAdvice {
	
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//        if (log.isDebugEnabled()) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String requestUriWithoutContextPath = servletRequest.getRequestURI().substring(servletRequest.getContextPath().length());
            String ip = IpUtils.getRealIp(servletRequest);
            log.info("responseBody_ip={}", ip);
//            log.info("requestId={}",MDC.get(LogInterceptor.REQUEST_ID));
            log.info("uri={}", requestUriWithoutContextPath);
//            System.out.println(servletRequest.getMethod());
            if("GET".equals(servletRequest.getMethod())) {
            	 log.info("args={}",servletRequest.getQueryString());
                 String ss = servletRequest.getScheme()+"://"+servletRequest.getServerName()+":"+
                 servletRequest.getServerPort()+ requestUriWithoutContextPath+"?"+servletRequest.getQueryString();
                 log.info("请求全路径={}",ss);
            }
            if(!"/operation/info/nurse/pick/queryForPad".equals(requestUriWithoutContextPath)) {
            	log.info("responseBody={}",JSON.toJSONString(body));
            }
            
//        }
        return body;
    }
}

LogRequestBodyAdvice

package com.wpmt.framework.aspectj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import com.alibaba.fastjson.JSON;
import com.wpmt.common.util.IpUtils;

import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
@Slf4j
@Profile({"th","bch"})
public class LogRequestBodyAdvice implements RequestBodyAdvice {
	
	@Autowired
	private HttpServletRequest servletRequest;
	
    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType,
                            Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage,
                                  MethodParameter parameter, Type targetType,
                                  Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage,
                                           MethodParameter parameter, Type targetType,
                                           Class<? extends HttpMessageConverter<?>> converterType) {
        return inputMessage;
    }
    
    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage,
                                MethodParameter parameter, Type targetType,
                                Class<? extends HttpMessageConverter<?>> converterType) {
//        if (log.isDebugEnabled()) {
            Method method = parameter.getMethod();
            String classMappingUri = getClassMappingUri(method.getDeclaringClass());
            String methodMappingUri = getMethodMappingUri(method);
            if (!methodMappingUri.startsWith("/") && !classMappingUri.endsWith("/")) {
                methodMappingUri = "/" + methodMappingUri;
            }
            String ip = IpUtils.getRealIp(servletRequest);
            log.info("requestBody_ip={}", ip);
            log.info("uri={}", classMappingUri + methodMappingUri );
            log.info("requestBody={}",JSON.toJSONString(body));
//        }
        return body;
    }

    private String getMethodMappingUri(Method method) {
        return getMappingUri(method.getDeclaredAnnotations());
    }

    private String getClassMappingUri(Class<?> declaringClass) {
        return getMappingUri(declaringClass.getDeclaredAnnotations());
    }

    private String getMappingUri(Annotation[] declaredAnnotations) {
        String mappingUri = "";
        for (Annotation declaredAnnotation : declaredAnnotations) {
            if (declaredAnnotation instanceof RequestMapping) {
                mappingUri = getMaxLengthUri(((RequestMapping) declaredAnnotation).value());
            } else if (declaredAnnotation instanceof PostMapping) {
                mappingUri = getMaxLengthUri(((PostMapping) declaredAnnotation).value());
            } else if (declaredAnnotation instanceof GetMapping) {
                mappingUri = getMaxLengthUri(((GetMapping) declaredAnnotation).value());
            }
        }
        return mappingUri;
    }

    private String getMaxLengthUri(String[] strings) {
        String maxLengthUri = "";
        for (String string : strings) {
            if (string.length() > maxLengthUri.length()) {
                maxLengthUri = string;
            }
        }
        return maxLengthUri;
    }
}

你可能感兴趣的:(spring,boot,后端,java)