You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/01/26 10:02:59 UTC

[GitHub] markusthoemmes closed pull request #3220: Move log message into a closure to defer string interpolation.

markusthoemmes closed pull request #3220: Move log message into a closure to defer string interpolation.
URL: https://github.com/apache/incubator-openwhisk/pull/3220
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/scala/src/main/scala/whisk/common/Logging.scala b/common/scala/src/main/scala/whisk/common/Logging.scala
index 873bcef81b..5a18ee3468 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 72dde1e05f..09ca1fd894 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)
     }
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services