Skip to content

Commit

Permalink
Use the system configured timezone when formatting logs (linkerd#1833)
Browse files Browse the repository at this point in the history
Signed-off-by: fantayeneh <[email protected]>
  • Loading branch information
fantayeneh authored and siggy committed Mar 1, 2018
1 parent d32350b commit 4a9222d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
11 changes: 7 additions & 4 deletions admin/src/main/scala/io/buoyant/admin/TimeZoneLogFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package io.buoyant.admin
import com.twitter.app.{App => TApp}
import com.twitter.finagle.tracing.Trace
import com.twitter.logging.{Level => TwLevel}
import com.twitter.util.Time
import com.twitter.util.{Time, TimeFormat}
import java.util.logging.{Formatter, Level, LogRecord, Logger}
import java.io.{PrintWriter, StringWriter}
import java.util.TimeZone

import scala.reflect.NameTransformer

/**
Expand All @@ -14,16 +16,17 @@ import scala.reflect.NameTransformer
*/

trait TimeZoneLogFormat { app: TApp =>
private val defaultFormat = new TimeFormat(" MMdd HH:mm:ss.SSS z", TimeZone.getDefault)
premain {
for (h <- Logger.getLogger("").getHandlers)
h.setFormatter(new LogFormatter)
h.setFormatter(new LogFormatter(defaultFormat))
}
}

/**
* Implements "glog" style log formatting. Adds a timezone after the timestamp.
*/
private class LogFormatter extends Formatter {
private class LogFormatter(timeFormat: TimeFormat) extends Formatter {
private val levels = Map[Level, Char](
Level.FINEST -> 'D',
Level.FINER -> 'D',
Expand Down Expand Up @@ -58,7 +61,7 @@ private class LogFormatter extends Formatter {

val str = new StringBuilder(msg.length + 30 + 150)
.append(levels.getOrElse(r.getLevel, 'U'))
.append(Time.fromMilliseconds(r.getMillis).format(" MMdd HH:mm:ss.SSS z"))
.append(timeFormat.format(Time.fromMilliseconds(r.getMillis)))
.append(" THREAD")
.append(r.getThreadID)

Expand Down
34 changes: 34 additions & 0 deletions admin/src/test/scala/io/buoyant/admin/LogFormatterTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.buoyant.admin

import java.util.TimeZone
import java.util.logging.{Level, LogRecord}

import com.twitter.util.TimeFormat
import org.scalatest.WordSpec

class LogFormatterTest extends WordSpec {

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")
}

}
}

0 comments on commit 4a9222d

Please sign in to comment.