This plugin allows you to use Logback's very flexible access framework within Play.
Add the following dependency for Play 2.4.x:
libraryDependencies += "org.databrary" %% "play-logback-access" % "0.4"
For Play 2.3.x on Scala 2.11, use version 0.3:
libraryDependencies += "org.databrary" %% "play-logback-access" % "0.3"
For Play 2.2.x on Scala 2.10, use version 0.1:
libraryDependencies += "org.databrary" %% "play-logback-access" % "0.1"
Add a configuration file to your conf/application.conf
with something like:
logbackaccess.config.resource=logback-access.xml
Then in conf/logback-access.xml
:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%t %D %s %h %b %A "%r" "%i{Referer}" "%i{User-Agent}"</pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
(You can alternatively use logbackaccess.config.file
or logbackaccess.config.url
.)
You can read more about the supported patterns. Note that patterns requiring access to the full request body or response body (including content-length) do not currently provide useful data.
There is also a logbackaccess.context
setting if you want it to use an execution context other than the default one.
The library will automatically initialize itself as a Play Module.
Inject PlayLogbackAccessApi
into any class to gain access to the API. This exposes:
filter
- Play Filter to log requests automaticallylog(requestTime: Long, request: RequestHeader, result: Result, user: Option[String])
- Manually log to the Access logger
In file: app/Filters.scala
import javax.inject.Inject
import org.databrary.PlayLogbackAccessApi
import play.api.http.HttpFilters
class Filters @Inject() (accessLogger: PlayLogbackAccessApi) extends HttpFilters {
val filters = Seq(accessLogger.filter)
}
Then, in file: conf/application.conf
play.http.filters=Filters