Skip to content

Instantly share code, notes, and snippets.

@ogatatsu
Forked from j5ik2o/gist:1518723
Created December 25, 2011 05:10
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 ogatatsu/1518754 to your computer and use it in GitHub Desktop.
Save ogatatsu/1518754 to your computer and use it in GitHub Desktop.
こんな構文で階層型のログ出力をしたい
def connect = {
log("connect") { // connect start
// ... 何かの処理 ...
log("login") { // connect : login start
// ... 何かの処理 ...
} // connect : login end
// ... 何かの処理 ...
} // connect end
}
import grizzled.slf4j.Logging
import scala.util.DynamicVariable
import scala.collection.immutable
object LogOps extends Logging {
val msgs = new DynamicVariable[Seq[String]](immutable.Queue.empty)
def log[T](msg: String)(f: => T): T = {
val newMsgs = msgs.value :+ msg
val str = newMsgs.mkString(" : ")
debug("%s : start".format(str))
val r = msgs.withValue(newMsgs){ f }
debug("%s : finish".format(str))
r
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment