From 42faaa86994ec4d1a856d1780601f5b8b6ab7a02 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Wed, 24 May 2017 00:28:05 -0700 Subject: [PATCH] Log cause message of causes without stack traces (#23018) --- .../test/scala/akka/event/LoggerSpec.scala | 23 ++++++++++++++++++- .../src/main/scala/akka/event/Logging.scala | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/akka-actor-tests/src/test/scala/akka/event/LoggerSpec.scala b/akka-actor-tests/src/test/scala/akka/event/LoggerSpec.scala index afbce32b03..570ae318d1 100644 --- a/akka-actor-tests/src/test/scala/akka/event/LoggerSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/event/LoggerSpec.scala @@ -4,10 +4,14 @@ package akka.event import akka.testkit._ + +import scala.util.control.NoStackTrace import scala.concurrent.duration._ import com.typesafe.config.{ Config, ConfigFactory } import akka.actor._ -import java.util.{ Date, GregorianCalendar, TimeZone, Calendar } +import java.nio.charset.StandardCharsets +import java.util.{ Calendar, Date, GregorianCalendar, TimeZone } + import org.scalatest.WordSpec import org.scalatest.Matchers import akka.serialization.SerializationExtension @@ -283,6 +287,23 @@ class LoggerSpec extends WordSpec with Matchers { val expected = dateFormat.format(new Date(event.timestamp)) log.timestamp(event) should ===(expected) } + + "include the cause message in the log message even exceptions with no stack trace" in { + class MyCause(msg: String) extends RuntimeException(msg) with NoStackTrace + + val log = new StdOutLogger {} + val out = new java.io.ByteArrayOutputStream() + + val causeMessage = "Some details about the exact cause" + + Console.withOut(out) { + log.error(Error(new MyCause(causeMessage), "source", classOf[LoggerSpec], "message", Map.empty[String, Any])) + } + out.flush() + out.close() + new String(out.toByteArray, StandardCharsets.UTF_8) should include(causeMessage) + } + } "Ticket 3165 - serialize-messages and dual-entry serialization of LogEvent" must { diff --git a/akka-actor/src/main/scala/akka/event/Logging.scala b/akka-actor/src/main/scala/akka/event/Logging.scala index caccba252e..35752303a0 100644 --- a/akka-actor/src/main/scala/akka/event/Logging.scala +++ b/akka-actor/src/main/scala/akka/event/Logging.scala @@ -1020,7 +1020,7 @@ object Logging { */ def stackTraceFor(e: Throwable): String = e match { case null | Error.NoCause ⇒ "" - case _: NoStackTrace ⇒ " (" + e.getClass.getName + ")" + case _: NoStackTrace ⇒ s" (${e.getClass.getName}: ${e.getMessage})" case other ⇒ val sw = new java.io.StringWriter val pw = new java.io.PrintWriter(sw)