You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ma...@apache.org on 2018/01/26 10:02:58 UTC

[incubator-openwhisk] branch master updated: Move log message into a closure to defer string interpolation. (#3220)

This is an automated email from the ASF dual-hosted git repository.

markusthoemmes pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 2ca301f  Move log message into a closure to defer string interpolation. (#3220)
2ca301f is described below

commit 2ca301fba99a6baba235f3afa90ae0daa884f3ff
Author: rodric rabbah <ro...@gmail.com>
AuthorDate: Fri Jan 26 05:02:54 2018 -0500

    Move log message into a closure to defer string interpolation. (#3220)
    
    A lot of log messages are logged at debug level and use string
    interpolation. By moving the message into a closure, for debug level
    messages which are generally not logged, the interpolation is deferred
    and then eschewed.
    
    Apply the same to other logger methods as well as transaction markers.
---
 .../src/main/scala/whisk/common/Logging.scala      | 31 ++++++++++++----------
 .../main/scala/whisk/common/TransactionId.scala    | 24 ++++++++---------
 2 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/common/scala/src/main/scala/whisk/common/Logging.scala b/common/scala/src/main/scala/whisk/common/Logging.scala
index 873bcef..5a18ee3 100644
--- a/common/scala/src/main/scala/whisk/common/Logging.scala
+++ b/common/scala/src/main/scala/whisk/common/Logging.scala
@@ -34,9 +34,9 @@ trait Logging {
    * Prints a message on DEBUG level
    *
    * @param from Reference, where the method was called from.
-   * @param message Message to write to the log
+   * @param message Message to write to the log if not empty
    */
-  def debug(from: AnyRef, message: String)(implicit id: TransactionId = TransactionId.unknown) = {
+  def debug(from: AnyRef, message: => String)(implicit id: TransactionId = TransactionId.unknown) = {
     if (id.meta.extraLogging) {
       emit(InfoLevel, id, from, message)
     } else {
@@ -48,9 +48,9 @@ trait Logging {
    * Prints a message on INFO level
    *
    * @param from Reference, where the method was called from.
-   * @param message Message to write to the log
+   * @param message Message to write to the log if not empty
    */
-  def info(from: AnyRef, message: String)(implicit id: TransactionId = TransactionId.unknown) = {
+  def info(from: AnyRef, message: => String)(implicit id: TransactionId = TransactionId.unknown) = {
     emit(InfoLevel, id, from, message)
   }
 
@@ -58,9 +58,9 @@ trait Logging {
    * Prints a message on WARN level
    *
    * @param from Reference, where the method was called from.
-   * @param message Message to write to the log
+   * @param message Message to write to the log if not empty
    */
-  def warn(from: AnyRef, message: String)(implicit id: TransactionId = TransactionId.unknown) = {
+  def warn(from: AnyRef, message: => String)(implicit id: TransactionId = TransactionId.unknown) = {
     emit(WarningLevel, id, from, message)
   }
 
@@ -68,9 +68,9 @@ trait Logging {
    * Prints a message on ERROR level
    *
    * @param from Reference, where the method was called from.
-   * @param message Message to write to the log
+   * @param message Message to write to the log if not empty
    */
-  def error(from: AnyRef, message: String)(implicit id: TransactionId = TransactionId.unknown) = {
+  def error(from: AnyRef, message: => String)(implicit id: TransactionId = TransactionId.unknown) = {
     emit(ErrorLevel, id, from, message)
   }
 
@@ -80,19 +80,22 @@ trait Logging {
    * @param loglevel The level to log on
    * @param id <code>TransactionId</code> to include in the log
    * @param from Reference, where the method was called from.
-   * @param message Message to write to the log
+   * @param message Message to write to the log if not empty
    */
-  def emit(loglevel: LogLevel, id: TransactionId, from: AnyRef, message: String)
+  protected[common] def emit(loglevel: LogLevel, id: TransactionId, from: AnyRef, message: => String)
 }
 
 /**
  * Implementation of Logging, that uses Akka logging.
  */
 class AkkaLogging(loggingAdapter: LoggingAdapter) extends Logging {
-  def emit(loglevel: LogLevel, id: TransactionId, from: AnyRef, message: String) = {
+  def emit(loglevel: LogLevel, id: TransactionId, from: AnyRef, message: => String) = {
     if (loggingAdapter.isEnabled(loglevel)) {
-      val name = if (from.isInstanceOf[String]) from else Logging.getCleanSimpleClassName(from.getClass)
-      loggingAdapter.log(loglevel, s"[$id] [$name] $message")
+      val logmsg: String = message // generates the message
+      if (logmsg.nonEmpty) { // log it only if its not empty
+        val name = if (from.isInstanceOf[String]) from else Logging.getCleanSimpleClassName(from.getClass)
+        loggingAdapter.log(loglevel, s"[$id] [$name] $logmsg")
+      }
     }
   }
 }
@@ -101,7 +104,7 @@ class AkkaLogging(loggingAdapter: LoggingAdapter) extends Logging {
  * Implementaion of Logging, that uses the output stream.
  */
 class PrintStreamLogging(outputStream: PrintStream = Console.out) extends Logging {
-  def emit(loglevel: LogLevel, id: TransactionId, from: AnyRef, message: String) = {
+  override def emit(loglevel: LogLevel, id: TransactionId, from: AnyRef, message: => String) = {
     val now = Instant.now(Clock.systemUTC)
     val time = Emitter.timeFormat.format(now)
     val name = if (from.isInstanceOf[String]) from else Logging.getCleanSimpleClassName(from.getClass)
diff --git a/common/scala/src/main/scala/whisk/common/TransactionId.scala b/common/scala/src/main/scala/whisk/common/TransactionId.scala
index 72dde1e..09ca1fd 100644
--- a/common/scala/src/main/scala/whisk/common/TransactionId.scala
+++ b/common/scala/src/main/scala/whisk/common/TransactionId.scala
@@ -50,16 +50,16 @@ case class TransactionId private (meta: TransactionMetadata) extends AnyVal {
    *
    * @param from Reference, where the method was called from.
    * @param marker A LogMarkerToken. They are defined in <code>LoggingMarkers</code>.
-   * @param message An additional message that is written into the log, together with the other information.
+   * @param message An additional message to be written into the log, together with the other information.
    * @param logLevel The Loglevel, the message should have. Default is <code>InfoLevel</code>.
    */
-  def mark(from: AnyRef, marker: LogMarkerToken, message: String = "", logLevel: LogLevel = DebugLevel)(
+  def mark(from: AnyRef, marker: LogMarkerToken, message: => String = "", logLevel: LogLevel = DebugLevel)(
     implicit logging: Logging) = {
 
     if (TransactionId.metricsLog) {
       // marker received with a debug level will be emitted on info level
       logging.emit(InfoLevel, this, from, createMessageWithMarker(message, LogMarker(marker, deltaToStart)))
-    } else if (message.nonEmpty) {
+    } else {
       logging.emit(logLevel, this, from, message)
     }
 
@@ -75,18 +75,18 @@ case class TransactionId private (meta: TransactionMetadata) extends AnyVal {
    *
    * @param from Reference, where the method was called from.
    * @param marker A LogMarkerToken. They are defined in <code>LoggingMarkers</code>.
-   * @param message An additional message that is written into the log, together with the other information.
+   * @param message An additional message to be written into the log, together with the other information.
    * @param logLevel The Loglevel, the message should have. Default is <code>InfoLevel</code>.
    *
    * @return startMarker that has to be passed to the finished or failed method to calculate the time difference.
    */
-  def started(from: AnyRef, marker: LogMarkerToken, message: String = "", logLevel: LogLevel = DebugLevel)(
+  def started(from: AnyRef, marker: LogMarkerToken, message: => String = "", logLevel: LogLevel = DebugLevel)(
     implicit logging: Logging): StartMarker = {
 
     if (TransactionId.metricsLog) {
       // marker received with a debug level will be emitted on info level
       logging.emit(InfoLevel, this, from, createMessageWithMarker(message, LogMarker(marker, deltaToStart)))
-    } else if (message.nonEmpty) {
+    } else {
       logging.emit(logLevel, this, from, message)
     }
 
@@ -102,13 +102,13 @@ case class TransactionId private (meta: TransactionMetadata) extends AnyVal {
    *
    * @param from Reference, where the method was called from.
    * @param startMarker <code>StartMarker</code> returned by a <code>starting</code> method.
-   * @param message An additional message that is written into the log, together with the other information.
+   * @param message An additional message to be written into the log, together with the other information.
    * @param logLevel The Loglevel, the message should have. Default is <code>InfoLevel</code>.
    * @param endTime Manually set the timestamp of the end. By default it is NOW.
    */
   def finished(from: AnyRef,
                startMarker: StartMarker,
-               message: String = "",
+               message: => String = "",
                logLevel: LogLevel = DebugLevel,
                endTime: Instant = Instant.now(Clock.systemUTC))(implicit logging: Logging) = {
 
@@ -122,7 +122,7 @@ case class TransactionId private (meta: TransactionMetadata) extends AnyVal {
         this,
         from,
         createMessageWithMarker(message, LogMarker(endMarker, deltaToStart, Some(deltaToEnd))))
-    } else if (message.nonEmpty) {
+    } else {
       logging.emit(logLevel, this, from, message)
     }
 
@@ -136,10 +136,10 @@ case class TransactionId private (meta: TransactionMetadata) extends AnyVal {
    *
    * @param from Reference, where the method was called from.
    * @param startMarker <code>StartMarker</code> returned by a <code>starting</code> method.
-   * @param message An additional message that is written into the log, together with the other information.
+   * @param message An additional message to be written into the log, together with the other information.
    * @param logLevel The <code>LogLevel</code> the message should have. Default is <code>WarningLevel</code>.
    */
-  def failed(from: AnyRef, startMarker: StartMarker, message: String = "", logLevel: LogLevel = WarningLevel)(
+  def failed(from: AnyRef, startMarker: StartMarker, message: => String = "", logLevel: LogLevel = WarningLevel)(
     implicit logging: Logging) = {
 
     val endMarker =
@@ -152,7 +152,7 @@ case class TransactionId private (meta: TransactionMetadata) extends AnyVal {
         this,
         from,
         createMessageWithMarker(message, LogMarker(endMarker, deltaToStart, Some(deltaToEnd))))
-    } else if (message.nonEmpty) {
+    } else {
       logging.emit(logLevel, this, from, message)
     }
 

-- 
To stop receiving notification emails like this one, please contact
markusthoemmes@apache.org.