1. 程式人生 > >dubbo filter記錄介面引數日誌

dubbo filter記錄介面引數日誌

實現效果:
dubbo的provider和consumer介面呼叫的入參和出參都會列印日誌。
dubbo配置

	<dubbo:consumer check="false" filter="dubboConsumerLogFilter"></dubbo:consumer>
	<dubbo:provider filter="dubboProducerLogFilter"/>
	```
建立檔案
/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
檔案內容
dubboConsumerLogFilter=com.sfpay.msfs.jyd.aspect.DubboLogFilter
dubboProducerLogFilter=com.sfpay.msfs.jyd.aspect.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;
}

}