Skip to content

Instantly share code, notes, and snippets.

@retronym
Created July 31, 2010 20:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retronym/502572 to your computer and use it in GitHub Desktop.
Save retronym/502572 to your computer and use it in GitHub Desktop.
abstract class LogFormatter[-A] {
def format(a: A): String
}
object LogFormatter {
implicit object StringLogFormatter extends LogFormatter[String] {
def format(s: String) = s
}
implicit object ExceptionLogFormatter extends LogFormatter[Exception] {
def format(e: Exception) = e.getMessage
}
}
object Logger {
def log(level: Int) = new {
def apply[A: LogFormatter](a: A) {
val formatted = implicitly[LogFormatter[A]].format(a)
}
}
}
val x = Logger.log(0)
x("s")
x(new RuntimeException("bang!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment