diff --git a/app/org/maproulette/filters/Filters.scala b/app/org/maproulette/filters/Filters.scala index 5ec12e71..0a7b5b30 100644 --- a/app/org/maproulette/filters/Filters.scala +++ b/app/org/maproulette/filters/Filters.scala @@ -12,5 +12,8 @@ import play.filters.gzip.GzipFilter /** * @author cuthbertm */ -class Filters @Inject() (corsFilter: CORSFilter, gzipFilter: GzipFilter) - extends DefaultHttpFilters(corsFilter, gzipFilter) +class Filters @Inject() ( + corsFilter: CORSFilter, + gzipFilter: GzipFilter, + httpLoggingFilter: HttpLoggingFilter +) extends DefaultHttpFilters(corsFilter, gzipFilter, httpLoggingFilter) diff --git a/app/org/maproulette/filters/HttpLoggingFilter.scala b/app/org/maproulette/filters/HttpLoggingFilter.scala index 37a32f4d..d8fce2dc 100644 --- a/app/org/maproulette/filters/HttpLoggingFilter.scala +++ b/app/org/maproulette/filters/HttpLoggingFilter.scala @@ -2,56 +2,63 @@ package org.maproulette.filters import javax.inject.Inject import akka.stream.Materializer -import org.maproulette.filters.HttpLoggingFilter.logger -import org.slf4j.LoggerFactory +import org.slf4j.{LoggerFactory, MarkerFactory} import play.api.mvc.Result import play.api.mvc.RequestHeader import play.api.mvc.Filter -import play.api.routing.HandlerDef import play.api.routing.Router import scala.concurrent.Future import scala.concurrent.ExecutionContext /** - * Filter to provide an http request log at the service side. The HttpLoggingFilter is enabled by default within - * application.conf but will create no output until logback is set to the debug or trace levels. + * Filter to provide an http request log at the service side. */ -class HttpLoggingFilter @Inject() (implicit val mat: Materializer, ec: ExecutionContext) - extends Filter { +class HttpLoggingFilter @Inject() ( + implicit val mat: Materializer, + implicit val ec: ExecutionContext +) extends Filter { + private val logger = LoggerFactory.getLogger(getClass.getName) + private val accessLogger = LoggerFactory.getLogger("AccessLogger") + def apply( nextFilter: RequestHeader => Future[Result] )(requestHeader: RequestHeader): Future[Result] = { val startTime = System.currentTimeMillis + // Create a uuid and associate it with a Marker + val uuid = java.util.UUID.randomUUID.toString + val marker = MarkerFactory.getMarker(uuid) nextFilter(requestHeader).map { result => - val handlerDef: HandlerDef = requestHeader.attrs(Router.Attrs.HandlerDef) - val action = handlerDef.controller + "." + handlerDef.method - val endTime = System.currentTimeMillis - val requestTime = endTime - startTime - - logger.debug( - "id={} {}: '{}' took {}ms and returned {}", - requestHeader.id, - action, + val endTime = System.currentTimeMillis + val requestTotalTime = endTime - startTime + val handlerDef = requestHeader.attrs.get(Router.Attrs.HandlerDef) + val action = handlerDef match { + case Some(hd) => hd.controller + "." + hd.method + case None => "unknown" + } + + accessLogger.info( + marker, + "Request '{}' [{}] {}ms - Response {}", requestHeader.toString(), - requestTime, + action, + requestTotalTime, result.header.status ) - logger.trace( - "id={} Request Headers: {}", - requestHeader.id, - requestHeader.headers.headers - .map({ case (k, v) => s"${k}=${v}" }) - .mkString(" ;; ") - ) + if (logger.isTraceEnabled()) { + logger.trace( + marker, + "id={} Request Headers: {}", + requestHeader.id, + requestHeader.headers.headers + .map({ case (k, v) => s"${k}=${v}" }) + .mkString(" ;; ") + ) + } - result + result.withHeaders("maproulette-request-id" -> uuid) } } } - -object HttpLoggingFilter { - private val logger = LoggerFactory.getLogger(getClass.getName) -} diff --git a/conf/application.conf b/conf/application.conf index 0beb3d4d..88bce228 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -339,17 +339,4 @@ osm { # You can disable evolutions if needed # evolutionplugin=disabled -# Logger -# ~~~~~ -# You can also configure logback (https://logback.qos.ch/), -# by providing an application-logger.xml file in the conf directory. - -# Root logger: -#logger.root=ERROR - -# Logger used by the framework: -#logger.play=INFO - -# Logger provided to your application: -#logger.application=DEBUG api.version="2.0" diff --git a/conf/dev.conf.example b/conf/dev.conf.example index 702cc2f6..82eaee65 100644 --- a/conf/dev.conf.example +++ b/conf/dev.conf.example @@ -1,12 +1,5 @@ include "application.conf" -# Change logging levels to see more interesting things while developing. -logger { - root=DEBUG - play=TRACE - application=TRACE -} - db.default { url="jdbc:postgresql://localhost:5432/mp_dev" url=${?MR_DATABASE_URL} diff --git a/conf/logback-dev.xml b/conf/logback-dev.xml index 94174a36..5e092efa 100644 --- a/conf/logback-dev.xml +++ b/conf/logback-dev.xml @@ -3,11 +3,30 @@ - %date{"HH:mm:ss,SSS"} %coloredLevel [%thread] %class{15}.%M\(%file:%line\) - %message%n%xException + %date{"HH:mm:ss.SSS"} %coloredLevel[%marker][%thread] %logger{15}.%M\(%file:%line\) - %message%n%xException true + + + + 256 + 0 + + + + + + 512 + 0 + + + + + + + @@ -19,20 +38,19 @@ + + - - - - + diff --git a/conf/logback.xml b/conf/logback.xml index 03e066c4..98c429e2 100644 --- a/conf/logback.xml +++ b/conf/logback.xml @@ -1,12 +1,37 @@ - + - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + %date{"HH:mm:ss.SSS"} %-5level [%marker][%thread][%logger{36}] - %message%n + - + + + + 256 + 0 + + + + + + 512 + 0 + + + + + + + + + + + + +