1

I am trying to log, method input/output using the aop approach in Web-flux. I was able to log request using following code ,but had trouble printing response and I see both request and response are printing as request,How can i print response.Is AOP is better solution for logging in reactive approach or filter/controller advice

@Aspect
@Component
class LogAspect(private val log: KLogger) {

    @Around("@annotation(Loggable)")
    @Throws(Throwable::class)
    fun logAround(joinPoint: ProceedingJoinPoint): Any? {
        val start = System.currentTimeMillis()
        return when (val result: Any = joinPoint.proceed()) {
            is Mono<*> -> {
                val traceId = AtomicReference("")
                result
                    .doOnSuccess { o ->
                        if (traceId.get().isNotEmpty()) {
                            MDC.put("correlationId", traceId.get())
                        }
                        var response: Any = ""
                        if (Objects.nonNull(o)) {
                            response = o.toString()
                        }
                        log.info(
                            "Enter: {}.{}() with argument[s] = {}",
                            joinPoint.signature.declaringTypeName, joinPoint.signature.name,
                            joinPoint.args
                        )
                        log.info(
                            "Exit: {}.{}() had arguments = {}, with Response = {}, Execution time = {} ms",
                            joinPoint.signature.declaringTypeName, joinPoint.signature.name,
                            joinPoint.args[0],
                            response, System.currentTimeMillis() - start
                        )
                    }
                    .subscriberContext { context ->
                        val contextTmp: Context = context as Context
                        if (contextTmp.hasKey("correlationId")) {
                            traceId.set(contextTmp.get("correlationId"))
                            MDC.put("correlationId", contextTmp.get("correlationId"))
                        }
                        context
                    }
            }
            else -> 
            {
                log.warn(
                    "Body type is not Mono for {}.{}()",
                    joinPoint.signature.declaringTypeName,
                    joinPoint.signature.name
                )
                result
            }
        }
    }

}
Rocky4Ever
  • 828
  • 3
  • 12
  • 35
  • This seems to be somewhat related to your other question which I already answered [here](https://stackoverflow.com/q/64164101/1082681). So is this question still open or are you one step further and have solved the problem already? – kriegaex Oct 02 '20 at 07:44
  • 3
    I just found [this article](https://medium.com/@azizulhaq.ananto/how-to-handle-logs-and-tracing-in-spring-webflux-and-microservices-a0b45adc4610) by chance. Maybe you should have mentioned it as a source instead of just copying the code, pretending it is your own. – kriegaex Oct 02 '20 at 07:54

0 Answers0