diff --git a/akka-actor/src/main/scala/akka/event/Logging.scala b/akka-actor/src/main/scala/akka/event/Logging.scala index a9bfd02e14..d53455cf0a 100644 --- a/akka-actor/src/main/scala/akka/event/Logging.scala +++ b/akka-actor/src/main/scala/akka/event/Logging.scala @@ -3,6 +3,8 @@ */ package akka.event +import java.time.Instant +import java.time.format.DateTimeFormatter import java.util.concurrent.TimeoutException import java.util.concurrent.atomic.AtomicInteger @@ -12,13 +14,13 @@ import akka.dispatch.RequiresMessageQueue import akka.event.Logging._ import akka.util.ReentrantGuard import akka.util.Helpers.toRootLowerCase -import akka.{ AkkaException, ConfigurationException } +import akka.{AkkaException, ConfigurationException} import scala.annotation.implicitNotFound import scala.collection.immutable import scala.concurrent.Await import scala.language.existentials -import scala.util.control.{ NoStackTrace, NonFatal } +import scala.util.control.{NoStackTrace, NonFatal} /** * This trait brings log level handling to the EventStream: it reads the log @@ -861,14 +863,11 @@ object Logging { */ class LoggerInitializationException(msg: String) extends AkkaException(msg) + private val formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss.SSS") + trait StdOutLogger { import StdOutLogger._ - import java.text.SimpleDateFormat - import java.util.Date - - private val date = new Date() - private val dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS") // format: OFF // FIXME: remove those when we have the chance to break binary compatibility @@ -879,10 +878,9 @@ object Logging { private val debugFormat = DebugFormat // format: ON - def timestamp(event: LogEvent): String = synchronized { - date.setTime(event.timestamp) - dateFormat.format(date) - } // SDF isn't threadsafe + def timestamp(event: LogEvent): String = { + formatter.format(Instant.ofEpochMilli(event.timestamp)) + } def print(event: Any): Unit = event match { case e: Error ⇒ error(e) diff --git a/akka-remote-tests/src/multi-jvm/scala/akka/remote/artery/BenchmarkFileReporter.scala b/akka-remote-tests/src/multi-jvm/scala/akka/remote/artery/BenchmarkFileReporter.scala index 9e9c997541..b44a262f44 100644 --- a/akka-remote-tests/src/multi-jvm/scala/akka/remote/artery/BenchmarkFileReporter.scala +++ b/akka-remote-tests/src/multi-jvm/scala/akka/remote/artery/BenchmarkFileReporter.scala @@ -5,8 +5,8 @@ package akka.remote.artery import java.io.File import java.nio.file.Files -import java.text.SimpleDateFormat -import java.util.Date +import java.time.Instant +import java.time.format.DateTimeFormatter import akka.actor.ActorSystem @@ -27,6 +27,8 @@ object BenchmarkFileReporter { target } + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss") + def apply(testName: String, system: ActorSystem): BenchmarkFileReporter = new BenchmarkFileReporter { val gitCommit = { @@ -34,8 +36,7 @@ object BenchmarkFileReporter { Try("git describe".!!.trim).getOrElse("[unknown]") } val testResultFile: File = { - val format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss") - val fileName = s"${format.format(new Date())}-Artery-$testName-$gitCommit-results.txt" + val fileName = s"${formatter.format(Instant.now())}-Artery-$testName-$gitCommit-results.txt" new File(targetDirectory, fileName) } val config = system.settings.config diff --git a/akka-remote/src/main/java/akka/remote/artery/AeronErrorLog.java b/akka-remote/src/main/java/akka/remote/artery/AeronErrorLog.java index a31170a49e..f2cbc0996e 100644 --- a/akka-remote/src/main/java/akka/remote/artery/AeronErrorLog.java +++ b/akka-remote/src/main/java/akka/remote/artery/AeronErrorLog.java @@ -25,8 +25,8 @@ import akka.event.LoggingAdapter; import java.io.File; import java.nio.MappedByteBuffer; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.Instant; +import java.time.format.DateTimeFormatter; import java.util.concurrent.atomic.AtomicLong; /** @@ -41,7 +41,7 @@ public class AeronErrorLog final DirectBuffer cncMetaDataBuffer; final int cncVersion; final AtomicBuffer buffer; - final SimpleDateFormat dateFormat; + final DateTimeFormatter formatter; public AeronErrorLog(File cncFile) { @@ -50,8 +50,7 @@ public class AeronErrorLog cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer); cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0)); buffer = CncFileDescriptor.createErrorLogBuffer(cncByteBuffer, cncMetaDataBuffer); - dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ"); - + formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSZ"); if (CncFileDescriptor.CNC_VERSION != cncVersion) { @@ -62,7 +61,7 @@ public class AeronErrorLog public long logErrors(LoggingAdapter log, long sinceTimestamp) { - // using AtomicLong because access from lambda, not because of currency + // using AtomicLong because access from lambda, not because of concurrency final AtomicLong lastTimestamp = new AtomicLong(sinceTimestamp); ErrorLogReader.read( @@ -71,8 +70,8 @@ public class AeronErrorLog log.error(String.format( "Aeron error: %d observations from %s to %s for:%n %s", observationCount, - dateFormat.format(new Date(firstObservationTimestamp)), - dateFormat.format(new Date(lastObservationTimestamp)), + formatter.format(Instant.ofEpochMilli(firstObservationTimestamp)), + formatter.format(Instant.ofEpochMilli(lastObservationTimestamp)), encodedException)); lastTimestamp.set(Math.max(lastTimestamp.get(), lastObservationTimestamp)); }, sinceTimestamp); diff --git a/project/TimeStampede.scala b/project/TimeStampede.scala index 4f98194b20..e997bb9bd3 100644 --- a/project/TimeStampede.scala +++ b/project/TimeStampede.scala @@ -3,6 +3,9 @@ */ package akka +import java.time.Instant +import java.time.format.DateTimeFormatter + import sbt._ import sbt.Keys._ @@ -26,8 +29,9 @@ object TimeStampede extends AutoPlugin { else version } + val formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss") + def timestamp(time: Long): String = { - val format = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss") - format.format(new java.util.Date(time)) + formatter.format(Instant.ofEpochMilli(time)) } }