Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Aug 16, 2016
1 parent 498e37f commit 4b61ba5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion wvlet-log/src/main/scala/wvlet/log/LogFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ object LogFormatter {
val log = s.result().mkString("\t")
record.cause match {
case Some(ex) =>
s"${log}\n${formatStacktrace(ex)}"
// Print only the first line of the exception message
s"${log}\n${formatStacktrace(ex).split("\n").head}"
case None =>
log
}
Expand Down
22 changes: 10 additions & 12 deletions wvlet-log/src/main/scala/wvlet/log/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,10 @@ object Logger {
* @param useParents
* @return
*/
def initLogger(name: String,
level: Option[LogLevel] = None,
handlers: Seq[Handler] = Seq.empty,
useParents: Boolean = true
): Logger = {

private[log] def initLogger(name: String,
level: Option[LogLevel] = None,
handlers: Seq[Handler] = Seq.empty,
useParents: Boolean = true): Logger = {
val logger = Logger.apply(name)
logger.clearHandlers
level.foreach(l => logger.setLogLevel(l))
Expand All @@ -172,7 +170,7 @@ object Logger {
logger
}

def of[A](implicit tag:ClassTag[A]) : Logger = {
def of[A](implicit tag: ClassTag[A]): Logger = {
apply(tag.runtimeClass.getName)
}

Expand All @@ -194,11 +192,11 @@ object Logger {
rootLogger.resetLogLevel
}

def getSuccinctLoggerName[A](cl:Class[A]) : String = {
def getSuccinctLoggerName[A](cl: Class[A]): String = {
val name =
if(cl.getName.contains("$anon$")) {
if (cl.getName.contains("$anon$")) {
val interfaces = cl.getInterfaces
if(interfaces != null && interfaces.length > 0) {
if (interfaces != null && interfaces.length > 0) {
// Use the first interface name instead of annonimized name
interfaces(0).getName
}
Expand All @@ -210,9 +208,9 @@ object Logger {
cl.getName
}

if(name.endsWith("$")) {
if (name.endsWith("$")) {
// Remove trailing $ of Scala Object name
name.substring(0, name.length-1)
name.substring(0, name.length - 1)
}
else {
name
Expand Down
24 changes: 23 additions & 1 deletion wvlet-log/src/test/scala/wvlet/log/LoggerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class MyAppClass extends LogSupport {
"""This is a multi-line
|log message!""".stripMargin)
info(Seq(1, 2, 3, 4))

warn("stack trace test", new Exception("stack trace test"))
}

trait Sample
Expand All @@ -56,6 +58,24 @@ class LoggerTest extends Spec {
}

"logger" should {
"use leaf logger name" in {
val l = Logger("leaf")
l.getName shouldBe "leaf"
l.info("leaf logger")
}

"have access to root logger" in {
val current = Logger.getDefaultLogLevel
Logger.resetDefaultLogLevel
Logger.rootLogger.getLogLevel shouldBe LogLevel.INFO
Logger.setDefaultLogLevel(current)
}

"create logger from class" in {
val l = Logger.of[MyAppClass]
l.getName shouldBe "wvlet.log.MyAppClass"
}

"display log messages" in {
info("logging test")
new MyAppClass
Expand Down Expand Up @@ -117,13 +137,16 @@ class LoggerTest extends Spec {
"display exception stack traces" in {
val e = new Exception("exception test")
warn("Running stack trace tests")
warn(e)
warn(new Error("error test"))
trace("error", e)
debug("error", e)
info("error", e)
warn("error", e)
error("error", e)

val l = Logger("org.sample")
l.warn(e)
l.trace("error", e)
l.debug("error", e)
l.info("error", e)
Expand Down Expand Up @@ -179,7 +202,6 @@ class LoggerTest extends Spec {
}
}


"ConsoleLogHandler" should {
"support flush and close" in {
val h = new ConsoleLogHandler(SourceCodeLogFormatter)
Expand Down

0 comments on commit 4b61ba5

Please sign in to comment.