All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jp.co.cyberagent.aeromock.server.AccessLog.scala Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package jp.co.cyberagent.aeromock.server

import io.netty.handler.codec.http.{HttpHeaders, HttpRequest}
import org.slf4j.LoggerFactory

/**
 *
 * @author stormcat24
 */
object AccessLog {

  val LOG = LoggerFactory.getLogger("access")

  type RequestInfo = (HttpRequest, Long)
  val threadLocal = new ThreadLocal[RequestInfo]

  def initialize(request: HttpRequest) {
    threadLocal.remove()
    threadLocal.set((request, System.currentTimeMillis()))
  }

  def writeAccessLog(statusCode: Int) {
    val requestInfo = threadLocal.get()
    val request = requestInfo._1
    val protocol = request.getProtocolVersion().protocolName()
    val uri = request.getUri()
    val headers = request.headers()
    val ua = headers.get(HttpHeaders.Names.USER_AGENT)
    val time = (System.currentTimeMillis - requestInfo._2).toDouble / 1000
    LOG.info(s"$protocol\t$uri\t$statusCode\t$ua\t$time")
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy