I have a Main non-actor class where routes are defined.
I would like to log the incoming requests to those routes into a specific log file but they are being logged into the "root" log file.
val logger = LoggerFactory.getLogger("access_log")l
logger.info("log to access log") //<--- is logged in access log file
val routes =
path("ping" ) {
logRequest("logging ping", Logging.InfoLevel) { <-- logged to root log
complete("pong")
}
}
}
The documentation states that "To change the logger, wrap this directive by withLog.", but I don't know how to change my logger
into a LoggingAdapter
that withLog
is called with.
My logback.xml has a logger entry
<logger name="access_log" level="INFO">
<appender-ref ref="ACCESSLOG" />
</logger>
Can anyone help?
Edit 2: following @Ramon J Romero y Vigil's update to his answer I realise I can simply (given a correctly configured logback.xml) do:
val accessLog = Logging(system.eventStream, "access_log")
val routes =
path("ping" ) {
withLog(accessLog) {
logRequest("ping", Logging.InfoLevel) {
complete("pong")
}
}
}
Edit:
Based on @Ramon J Romero y Vigil's answer I was able to get the requests logged to my access_log by doing the following:
val loggingAdapter : LoggingAdapter = new LoggingAdapter {
override def isDebugEnabled : Boolean = logger.isDebugEnabled
override def isErrorEnabled : Boolean = logger.isErrorEnabled
override def isInfoEnabled : Boolean = logger.isInfoEnabled
override def isWarningEnabled : Boolean = logger.isWarnEnabled
override def notifyDebug(message: String): Unit = logger.debug(message)
override protected def notifyError(message: String): Unit = logger.error(message)
override protected def notifyError(cause: Throwable, message: String): Unit = logger.error(message, cause)
override protected def notifyWarning(message: String): Unit = logger.warn(message)
override protected def notifyInfo(message: String): Unit = logger.info(message)
}
val routes =
path("ping" ) {
withLog(loggingAdapter) {
logRequest("**ping**", Logging.InfoLevel) {
complete("pong")
}
}
}