@Activate(group = Constants.PROVIDER)
@Slf4j
public class LogFilter implements Filter {
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
long before = System.currentTimeMillis();
Result result = invoker.invoke(invocation);
log(invoker, invocation, result, before);
return result;
}
private void log(Invoker<?> invoker, Invocation invocation, Result result, long before) {
String serviceClassName = invoker.getInterface().getName();
String signature = serviceClassName + "." + invocation.getMethodName();
Object[] args = invocation.getArguments();
log(signature, args, result, System.currentTimeMillis() - before);
}
private void log(String signature, Object[] args, Result result, long take) {
if (result.hasException()) {
Throwable e = result.getException();
log.error("service:{}, FAILED, take(ms):{}, args:{}, result:{}", signature, take, args, e.getMessage());
} else {
Object value = result.getValue();
log.debug("service:{}, SUCCESS, take(ms):{}, args:{}, result:{}", signature, take, args, value);
}
}