Home > Mobile >  How to customise logging in tapir?
How to customise logging in tapir?

Time:12-22

I'm sure this is a ridiculous question... I'm trying to enable tapir's logging as described here:

https://tapir.softwaremill.com/en/v0.19.0-m4/server/debugging.html

... but even having looked at the ServerOptions docs, I'm not getting anywhere. My naive assumption from the docs was that it should be something like:

import sttp.tapir.server.akkahttp.AkkaHttpServerOptions
import sttp.tapir.server.interceptor.log.DefaultServerLog

val customServerOptions: AkkaHttpServerOptions = AkkaHttpServerOptions.customInterceptors(
    serverLog = DefaultServerLog(logWhenHandled=true, logAllDecodeFailures=true)
)

and then

AkkaHttpServerInterpreter(customServerOptions).toRoute(...)

However, this is clearly way off - DefaultServerLog is the wrong type, and would need masses of configuration.

The docs imply this can just be done by setting a few flags, what am I missing?

CodePudding user response:

The DefaultServerLog is the default implementation for the ServerLog trait, though it has a couple of fields without default values. These fields specify how to actually perform the logging: DefaultServerLog .doLogWhenHandled etc.

What you need to do is to get an instance of DefaultServerLog, which is configured to integrate with akka-http. This is available as AkkaHttpServerOptions.Log.defaultServerLog.

Unfortunately, in 0.19 this was typed too narrowly, not allowing proper customisation. Hence, an ugly cast is needed (this is fixed in the current, stable 1.2 version):

val customServerOptions: AkkaHttpServerOptions = 
  AkkaHttpServerOptions.customInterceptors(
    serverLog = Some(
      AkkaHttpServerOptions.Log.defaultServerLog
        .asInstanceOf[DefaultServerLog[LoggingAdapter => Future[Unit]]]
        .copy(logWhenHandled = true, logAllDecodeFailures = true)
    )
  )

AkkaHttpServerInterpreter(customServerOptions).toRoute(???)

As for the docs, you are right that they might be misleading (they haven't changed much since 0.19), I'll fix that shortly.

  • Related