如何将异常堆栈跟踪记录到调试级别?

时间:2015-05-19 04:34:20

标签: scala logging akka slf4j

以下是akka中使用的调试级别函数列表:

def debug(message : scala.Predef.String)
def debug(template : scala.Predef.String, arg1 : scala.Any)
def debug(template : scala.Predef.String, arg1 : scala.Any, arg2 : scala.Any)
def debug(template : scala.Predef.String, arg1 : scala.Any, arg2 : scala.Any, arg3 : scala.Any)
def debug(template : scala.Predef.String, arg1 : scala.Any, arg2 : scala.Any, arg3 : scala.Any, arg4 : scala.Any)

此处错误级别功能列表:

def error(cause : scala.Throwable, message : scala.Predef.String)
def error(cause : scala.Throwable, template : scala.Predef.String, arg1 : scala.Any)
def error(cause : scala.Throwable, template : scala.Predef.String, arg1 : scala.Any, arg2 : scala.Any)
def error(cause : scala.Throwable, template : scala.Predef.String, arg1 : scala.Any, arg2 : scala.Any, arg3 : scala.Any)
def error(cause : scala.Throwable, template : scala.Predef.String, arg1 : scala.Any, arg2 : scala.Any, arg3 : scala.Any, arg4 : scala.Any)

因为在高负载系统中使用的代码我只需要在调试级别记录异常堆栈跟踪。要在调试级别期间记录,我必须写:

//some where in try catch block
val log : akka.event.LoggingAdapter
val e : Exception

if (log.isDebugEnabled) {
    log.error(e, "Unexpected error")
}

除了混合水平之外,akka有更好的替代品吗?

1 个答案:

答案 0 :(得分:0)

这是一个扩展Akka日志记录的类型类,可以在warninginfodebug级别上记录throwable:

AkkaThrowableLogging

import java.io.{PrintWriter, StringWriter}
import akka.event.LoggingAdapter

object AkkaThrowableLogging {
  implicit class LoggingAdapterOps(logger: LoggingAdapter) {

    private def throwableStr(t: Throwable): String = {
      val sw = new StringWriter()
      val pw = new PrintWriter(sw, true)
      t.printStackTrace(pw)
      sw.getBuffer.toString
    }

    private def concat(cause: Throwable, message: String) = s"$message\n${throwableStr(cause)}"

    /**
      * Log message at warning level, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def warning(cause: Throwable, message: String): Unit =
      logger.warning(concat(cause, message))

    /**
      * Message template with 1 replacement argument, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def warning(cause: Throwable, template: String, arg1: Any): Unit =
      logger.warning(concat(cause, template), arg1)

    /**
      * Message template with 2 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def warning(cause: Throwable, template: String, arg1: Any, arg2: Any): Unit =
      logger.warning(concat(cause, template), arg1, arg2)

    /**
      * Message template with 3 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def warning(cause: Throwable, template: String, arg1: Any, arg2: Any, arg3: Any): Unit =
      logger.warning(concat(cause, template), arg1, arg2, arg3)

    /**
      * Message template with 4 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def warning(cause: Throwable, template: String, arg1: Any, arg2: Any, arg3: Any, arg4: Any): Unit =
      logger.warning(concat(cause, template), arg1, arg2, arg3, arg4)

    /**
      * Log message at info level, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def info(cause: Throwable, message: String): Unit =
      logger.info(concat(cause, message))

    /**
      * Message template with 1 replacement argument.
      * @see [[LoggingAdapter]]
      */
    def info(cause: Throwable, template: String, arg1: Any): Unit =
      logger.info(concat(cause, template), arg1)

    /**
      * Message template with 2 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def info(cause: Throwable, template: String, arg1: Any, arg2: Any): Unit =
      logger.info(concat(cause, template), arg1, arg2)

    /**
      * Message template with 3 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def info(cause: Throwable, template: String, arg1: Any, arg2: Any, arg3: Any): Unit =
      logger.info(concat(cause, template), arg1, arg2, arg3)

    /**
      * Message template with 4 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def info(cause: Throwable, template: String, arg1: Any, arg2: Any, arg3: Any, arg4: Any): Unit =
      logger.info(concat(cause, template), arg1, arg2, arg3, arg4)

    /**
      * Log message at debug level, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def debug(cause: Throwable, message: String): Unit =
      logger.debug(concat(cause, message))

    /**
      * Message template with 1 replacement argument.
      * @see [[LoggingAdapter]]
      */
    def debug(cause: Throwable, template: String, arg1: Any): Unit =
      logger.debug(concat(cause, template), arg1)

    /**
      * Message template with 2 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def debug(cause: Throwable, template: String, arg1: Any, arg2: Any): Unit =
      logger.debug(concat(cause, template), arg1, arg2)

    /**
      * Message template with 3 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def debug(cause: Throwable, template: String, arg1: Any, arg2: Any, arg3: Any): Unit =
      logger.debug(concat(cause, template), arg1, arg2, arg3)

    /**
      * Message template with 4 replacement arguments, including the exception that caused the error.
      * @see [[LoggingAdapter]]
      */
    def debug(cause: Throwable, template: String, arg1: Any, arg2: Any, arg3: Any, arg4: Any): Unit =
      logger.debug(concat(cause, template), arg1, arg2, arg3, arg4)
  }
}

用法

import AkkaThrowableLogging._
log.warning(new Exception("FooBarBaz"), "Hello {}", "world")

输出:

[WARN] [10/16/2016 19:05:50.614] [ScalaTest-run-running-AkkaThrowableLoggingTest] [AkkaThrowableLoggingTest(akka://AkkaTest)] Hello world
java.lang.Exception: FooBarBaz
    at test.AkkaThrowableLoggingTest$$anonfun$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(AkkaThrowableLoggingTest.scala:12)
    at test.AkkaThrowableLoggingTest$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(AkkaThrowableLoggingTest.scala:11)
    at test.AkkaThrowableLoggingTest$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(AkkaThrowableLoggingTest.scala:11)
    at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
    at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    at org.scalatest.Transformer.apply(Transformer.scala:22)
    ...

警告:错误级别的日志输出可能与较低级别

不同

异常无法传递给底层日志框架(例如,log4j,logback等),因为Akka的LoggingAdapter没有为此提供API。因此,异常不能在该级别上进行格式化,而是由throwableStr函数处理,该函数使用默认的Java格式(当您调用printStackTrace时获得的格式)。因此,log.error(e, ...) 的日志输出可能log.warning(, ...)log.info(e, ...)log.debug(e, ...)的日志输出不同。大多数情况下,没有注意到差异,因为异常格式化无论如何都是默认格式。