如何使akka-http logRequest日志到特定的记录器日志文件 [英] How to make akka-http logRequest log to a specific logger log file

查看:169
本文介绍了如何使akka-http logRequest日志到特定的记录器日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Main非角色类,其中定义了路由.

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")
          }
        }
      }

文档指出要更改记录器,请使用withLog包裹此指令.",但我不知道如何将logger更改为调用withLogLoggingAdapter.

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.

我的logback.xml有一个记录器条目

My logback.xml has a logger entry

 <logger name="access_log" level="INFO">
        <appender-ref ref="ACCESSLOG" />
    </logger>

任何人都可以帮忙吗?

在@Ramon J Romero和Vigil更新了他的答案之后,我意识到我可以简单地(给出正确配置的logback.xml)来做

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")
      }
    }
  }

基于@Ramon J Romero和Vigil的回答,我可以通过执行以下操作将请求记录到我的access_log中:

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")
          }
        }
      } 

推荐答案

如果必须使用LoggerFactory

If you have to use LoggerFactory

似乎您是直接使用slf4j而不是使用akka的日志记录机制,但是您希望akka使用slf4j Logger而不是其自己的

It appears as if you are using slf4j directly instead of using akka's logging mechanisms, but you want akka to use the slf4j Logger instead of its own LoggingAdapter. This type of mixing & matching of log systems isn't advised, it is easier to stick with one or the other.

如果需要进行这种混合,则可以使用已创建的Logger并覆盖LoggingAdapter中的所有抽象字段,手动创建LoggingAdapter:

If this mixing is necessary then you can just create a LoggingAdapter manually by using the Logger that you've already created and overriding all of the abstract fields in LoggingAdapter:

val logger = LoggerFactory.getLogger("access_log")

val loggingAdapter : LoggingAdapter = new LoggingAdapter {
  override def isDebugEnabled : Boolean = logger.isDebugEnabled
  override def isErrorEnabled : Boolean = logger.isErrorEnabled
  ...
}

现在可以通过添加 withLog 指令.

The adapter can now be used by adding the withLog directive.

直接使用Akka记录

使用 akka的日志记录功能要容易得多.直接.

It is much easier to utilize akka's logging functionality directly.

这仍然允许您与slf4j api交互.通过使用slf4j API,您可以指定要使用的回溯追加器,包括文件追加器,其中在配置设置中指定了输出文件.

This still allows you to interact with the slf4j api. By using the slf4j API you can specify which logback appender you want to use, including file appenders in which the output file is specified in configuration settings.

这篇关于如何使akka-http logRequest日志到特定的记录器日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆