作为对this question的回答,我能够获得logRequest记录到名为access.log的文件中。
我有
val routes =
path("ping" ) {
withLog(accessLog) {
logRequest("ping", Logging.InfoLevel) {
complete("pong")
}
}
}
我的logback.xml配置的一部分看起来像
<appender name="ACCESSLOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/access.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} %msg%n
</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/access-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>64 MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
日志文件中的结果条目看起来像
2019-02-06 16:51:04 ping: HttpRequest(HttpMethod(GET),http://localhost:8080/ping,List(Host: localhost:8080, Connection: keep-alive, Cache-Control: max-age=0, Upgrade-Insecure-Requests: 1, User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36, Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, image/apng, */*;q=0.8, Accept-Encoding: gzip, deflate, br, Accept-Language: en-GB, en-US;q=0.9, en;q=0.8, Cookie: xxx, Timeout-Access: <function1>),HttpEntity.Strict(none/none,ByteString()),HttpProtocol(HTTP/1.1))
如何使我的日志条目看起来像
2019-02-06 16:51:04 GET /ping <time_taken>
答案 0 :(得分:1)
您可以定义一个将产生LogEntry
的方法,并以所需的方式设置日志消息。
def myLogEntry(req: HttpRequest): LogEntry = {
LogEntry(s"${req.method} /${req.uri.path}", Logging.InfoLevel)
}
logRequest(myLogEntry _) {
complete("pong")
}
问题出在诸如<time_taken>
之类的信息上,因为在执行指令logRequest
时该信息不可用。指令被触发时,此指令在传入的http请求上打印日志消息。尚无关于内部路线结果的信息,该信息已完成或被拒绝。
查看此blog post,了解如何计算时间。
答案 1 :(得分:0)
检查Documentation中是否有pattern
。
我们对此很满意:
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />
...
<appender ...>
<encoder>
<pattern>%date %coloredLevel %logger{15} - %message%n%xException{10}</pattern>
</encoder>
</appender>
这是另一个示例(我在其中找到了结果):
<appender name="ACCESS_FILE" ...>
....
<encoder>
<pattern>%date [%level] from %logger - %message%n%xException</pattern>
</encoder>
</appender>
这给您:
2018-06-26 12:13:50,377 [INFO] from access-filter - 6. GET /assets/javascripts/versioned.js took 113ms and returned 200
2018-06-26 12:13:50,377 [INFO] from access-filter - 6. GET /assets/scala-adapters-client-jsdeps.js took 113ms and returned 200