Skip to content

Commit

Permalink
Use system default timezone in the access logger (linkerd#1851)
Browse files Browse the repository at this point in the history

Signed-off-by: fantayeneh <[email protected]>
  • Loading branch information
fantayeneh authored and adleong committed Mar 20, 2018
1 parent 52f9351 commit 1425f0c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
38 changes: 19 additions & 19 deletions admin/src/test/scala/io/buoyant/admin/LogFormatterTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import java.util.TimeZone
import java.util.logging.{Level, LogRecord}

import com.twitter.util.TimeFormat
import org.scalatest.WordSpec
import io.buoyant.test.FunSuite

class LogFormatterTest extends WordSpec {
class LogFormatterTest extends FunSuite {

val record = new LogRecord(Level.INFO, "Logging useful info")
record.setMillis(1519468460239L)
record.setThreadID(10)

"LogFormatter" should {

"use UTC timezone" in {
val utcFormatter = new LogFormatter(
new TimeFormat(" MMdd HH:mm:ss.SSS z", TimeZone.getTimeZone("UTC"))
)
assert(utcFormatter
.format(record) == "I 0224 10:34:20.239 UTC THREAD10: Logging useful info\n")
}

"use non UTC timezone" in {
val utcFormatter = new LogFormatter(
new TimeFormat(" MMdd HH:mm:ss.SSS z", TimeZone.getTimeZone("GMT+8"))
)
assert(utcFormatter
.format(record) == "I 0224 18:34:20.239 GMT+08:00 THREAD10: Logging useful info\n")
}
test("uses UTC timezone") {
val utcFormatter = new LogFormatter(
new TimeFormat(" MMdd HH:mm:ss.SSS z", TimeZone.getTimeZone("UTC"))
)
assert(
utcFormatter
.format(record) == "I 0224 10:34:20.239 UTC THREAD10: Logging useful info\n"
)
}

test("uses non UTC timezone") {
val gmtFormatter = new LogFormatter(
new TimeFormat(" MMdd HH:mm:ss.SSS z", TimeZone.getTimeZone("GMT+8"))
)
assert(
gmtFormatter
.format(record) == "I 0224 18:34:20.239 GMT+08:00 THREAD10: Logging useful info\n"
)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.buoyant.linkerd.protocol.http

import java.util.TimeZone

import com.twitter.finagle.{Service, ServiceFactory, SimpleFilter, Stack, Stackable}
import com.twitter.finagle.http.{Request, Response}
import com.twitter.logging._
import com.twitter.util.{Time, TimeFormat}
import io.buoyant.router.RouterLabel

case class AccessLogger(log: Logger) extends SimpleFilter[Request, Response] {
case class AccessLogger(log: Logger, timeFormat: TimeFormat = AccessLogger.DefaultFormat) extends SimpleFilter[Request, Response] {

def apply(req: Request, svc: Service[Request, Response]) = {
val reqHeaders = req.headerMap
Expand All @@ -21,14 +23,15 @@ case class AccessLogger(log: Logger) extends SimpleFilter[Request, Response] {
svc(req).onSuccess { rsp =>
val statusCode = rsp.statusCode
val responseBytes = rsp.contentLength.map(_.toString).getOrElse("-")
val requestEndTime = new TimeFormat("dd/MM/yyyy:HH:mm:ss Z").format(Time.now)
val requestEndTime = timeFormat.format(Time.now)
log.info("""%s %s %s %s [%s] "%s" %d %s "%s" "%s"""", hostHeader, remoteHost, identd, user, requestEndTime,
reqResource, statusCode, responseBytes, referer, userAgent)
}
}
}

object AccessLogger {
private val DefaultFormat = new TimeFormat("dd/MM/yyyy:HH:mm:ss z", TimeZone.getDefault)

object param {
case class File(path: String)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.buoyant.linkerd.protocol.http

import java.util.{TimeZone, logging => javalog}

import com.twitter.finagle.Service
import com.twitter.finagle.http._
import com.twitter.util.{Future, Promise, Time}
import com.twitter.logging._
import com.twitter.util.{Future, Promise, Time}
import io.buoyant.test.Awaits
import java.util.{logging => javalog}
import org.scalatest.FunSuite

class AccessLoggerTest extends FunSuite with Awaits {

object StringLogger extends Logger("string", javalog.Logger.getAnonymousLogger()) {
val handler = new StringHandler(new Formatter {
override def format(record: javalog.LogRecord): String = formatText(record)
Expand Down Expand Up @@ -41,8 +41,20 @@ class AccessLoggerTest extends FunSuite with Awaits {
req.contentType = "text/plain"

val f = service(req)
assert(StringLogger.getLoggedLines() ==
"""monkeys 0.0.0.0 - - [06/01/2016:21:21:26 +0000] "HEAD /foo?bar=bah HTTP/1.1" 402 304374 "-" "-"""")
assert(
StringLogger.getLoggedLines() ==
"""monkeys 0.0.0.0 - - [06/01/2016:21:21:26 GMT] "HEAD /foo?bar=bah HTTP/1.1" 402 304374 "-" "-""""
)
}
}

override protected def withFixture(test: NoArgTest) = {
val currentTimezone = TimeZone.getDefault
TimeZone.setDefault(TimeZone.getTimeZone("GMT"))
try {
super.withFixture(test)
} finally {
TimeZone.setDefault(currentTimezone)
}
}
}

0 comments on commit 1425f0c

Please sign in to comment.