Skip to content

Instantly share code, notes, and snippets.

@liorgonnen
Last active August 29, 2015 14:06
Show Gist options
  • Save liorgonnen/81fa8c428601c263d7e9 to your computer and use it in GitHub Desktop.
Save liorgonnen/81fa8c428601c263d7e9 to your computer and use it in GitHub Desktop.
package utils
import play.api.LoggerLike
/**
* Created by Lior Gonnen on 9/4/14.
*/
object LogUtils {
/**
* A helper used to return a value and log a message without the need to create a local variable for it
*
* @example val a = 5
* val b = 10
* val c = a + b >> { sum => s"@a + @b = @sum" }
*
* OR
*
* val c = a + b >> "Complicated computation completed!"
*
* @param value The return value
* @tparam T The return value type
*/
implicit class LogAny2[T](val value : T) extends AnyVal {
// Needed to disambiguate the logger methods that take a single string as a parameter from the others when
// used passing as functions
def debug(implicit logger : LoggerLike) = logger.debug(_ : String)
def error(implicit logger : LoggerLike) = logger.error(_ : String)
// Log debug
def >>(str : String)(implicit logger : LoggerLike) : T = log(debug, str)
def >>(f : T => String)(implicit logger : LoggerLike) : T = log(debug, f(value))
// Log error
def @>>(str : String)(implicit logger : LoggerLike) : T = log(error, str)
def @>>(f : T => String)(implicit logger : LoggerLike) : T = log(error, f(value))
private def log(logFunc: String => Unit, str : String)(implicit logger : LoggerLike) : T = {
logFunc(str)
value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment