-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintLogger.scala
45 lines (35 loc) · 1.39 KB
/
PrintLogger.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
package frankenpaxos
class PrintLogger(logLevel: LogLevel = LogDebug) extends Logger(logLevel) {
private val formatter = java.time.format.DateTimeFormatter
.ofPattern("MMM dd HH:mm:ss.nnnnnnnnn")
.withZone(java.time.ZoneId.systemDefault())
private def colored(color: String, s: String) = {
s"${color}${s}${Console.RESET}"
}
private def time: String =
s"[${formatter.format(java.time.Instant.now())}]"
private def threadId: String =
s"[Thread ${Thread.currentThread().getId()}]"
override def fatalImpl(message: String): Nothing = {
def show(s: String): Unit = {
System.err.println(
colored(Console.WHITE + Console.RED_B, s"$time [FATAL] $threadId") + s
)
}
show(message)
val stackTraceElements =
for (e <- Thread.currentThread().getStackTrace())
yield e.toString()
show(stackTraceElements.mkString("\n"))
System.exit(1)
???
}
override def errorImpl(message: String): Unit =
println(colored(Console.RED, s"$time [ERROR] $threadId ") + message)
override def warnImpl(message: String): Unit =
println(colored(Console.YELLOW, s"$time [WARN] $threadId ") + message)
override def infoImpl(message: String): Unit =
println(colored(Console.BLUE, s"$time [INFO] $threadId ") + message)
override def debugImpl(message: String): Unit =
println(colored(Console.CYAN, s"$time [DEBUG] $threadId ") + message)
}