dubbo filter记录接口参数日志

实现效果:
dubbo的provider和consumer接口调用的入参和出参都会打印日志。
dubbo配置

	
	
	```
创建文件
/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
文件内容
dubboConsumerLogFilter=com.xxx.DubboLogFilter
dubboProducerLogFilter=com.xxx.DubboLogFilter

	filter类

public class DubboLogFilter implements Filter {
private Logger logger = LoggerFactory.getLogger(this.getClass());

@Override
public Result invoke(Invoker invoker, Invocation invocation) throws RpcException {
	String name = invoker.getInterface().getName();
	Object[] args = invocation.getArguments();
	String method = invocation.getMethodName();
	String prefix = "日志:"+name+"."+method;
	logger.info(prefix+" 入参=>"+JSONArray.toJSONString(args));
	Result r = invoker.invoke(invocation);
	if(r.hasException()){
		Throwable e = r.getException();
		if(e.getClass().getName().equals("java.lang.RuntimeException")){
			logger.error(prefix+" 运行时异常=>"+JSONObject.toJSONString(r));
		}else{
			logger.error(prefix+" 异常=>"+JSONObject.toJSONString(r));
		}
	}else{
		logger.info(prefix+" 结果=>"+JSONObject.toJSONString(r));
	}
	return r;
}

}

你可能感兴趣的:(java)