package cn.axzo.msg.center.notices.client.interceptor;

import cn.axzo.msg.center.notices.common.constans.CommonConstants;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 统一日志打印
 *
 * @author zhaoyong_sh
 * @see LoggerResponseBodyAdvice
 * @since 2021-05-19 10:28
 */
@Slf4j
//@ControllerAdvice
public class LoggerResponseBodyAdvice implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        String path = request.getURI().getPath();
        if(!CommonConstants.HEARTBEAT_URI.equals(path)) {
            log.info("axzo mns request path : " + path + ", response is {}", JSON.toJSONString(body));
        }
        return body;
    }

}

