-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddLogTimestamps.scala
71 lines (54 loc) · 1.76 KB
/
AddLogTimestamps.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (C) 2019-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package akka
import java.io.PrintWriter
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import sbt._
import Keys._
import sbt.Def
import sbt.internal.LogManager
import sbt.internal.util.ConsoleOut
object AddLogTimestamps extends AutoPlugin {
val enableTimestamps: Boolean = CliOption("akka.log.timestamps", false).get
override def requires: Plugins = plugins.JvmPlugin
override def trigger: PluginTrigger = allRequirements
private val UTC = ZoneId.of("UTC")
override def projectSettings: Seq[Def.Setting[_]] = {
logManager := {
val original = logManager.value
if (enableTimestamps) {
val myOut = new PrintWriter(System.out) {
val dateTimeFormat = DateTimeFormatter.ofPattern("MM-dd HH:mm:ss.SSS")
var lastWasNewline = true
override def print(s: String): Unit = {
maybePrintTimestamp()
super.print(s)
lastWasNewline = s.endsWith("\n")
}
override def println(): Unit = {
super.println()
lastWasNewline = true
}
override def println(x: String): Unit = {
maybePrintTimestamp()
super.println(x)
lastWasNewline = true
}
private def maybePrintTimestamp(): Unit =
if (lastWasNewline) {
super.print('[')
super.print(dateTimeFormat.format(LocalDateTime.now(UTC)))
super.print("] ")
lastWasNewline = false
}
}
val myLogger = ConsoleOut.printWriterOut(myOut)
LogManager.defaults(extraLoggers.value, myLogger)
} else
original
}
}
}