2019-01-02 18:55:26 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2019-2022 Lightbend Inc. <https://www.lightbend.com>
|
2019-01-02 18:55:26 +08:00
|
|
|
*/
|
|
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
package org.apache.pekko
|
2018-02-05 17:53:51 +01:00
|
|
|
|
|
|
|
|
import java.io.PrintWriter
|
|
|
|
|
import java.time.LocalDateTime
|
|
|
|
|
import java.time.ZoneId
|
|
|
|
|
import java.time.format.DateTimeFormatter
|
2021-11-01 08:53:32 +01:00
|
|
|
import sbt.{ Def, _ }
|
2018-02-05 17:53:51 +01:00
|
|
|
import Keys._
|
2021-11-01 08:53:32 +01:00
|
|
|
import sbt.internal.{ AppenderSupplier, LogManager }
|
2018-02-05 17:53:51 +01:00
|
|
|
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)
|
|
|
|
|
|
2021-11-01 08:53:32 +01:00
|
|
|
LogManager.defaults(extraAppenders.value, myLogger)
|
2018-02-05 17:53:51 +01:00
|
|
|
} else
|
|
|
|
|
original
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|