Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created December 25, 2011 04:23
Show Gist options
  • Save j5ik2o/1518723 to your computer and use it in GitHub Desktop.
Save j5ik2o/1518723 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
}
// implicit parameterで実現しればいいのかな... でも、名前が2種類になるのはダサいなー、、、logという名前で統一的に実現できればいいけど無理かなー
import LogOps._
def connect = {
logHasChild("connect") { implicit ctx => // connect start
// ... 何かの処理 ...
logChild("login")/* (ctx) */ { // connect : login start
// ... 何かの処理 ...
} // connect : login end
// ... 何かの処理 ...
} // connect end
}
import grizzled.slf4j.Logging
object LogOps extends Logging {
def logHasChild[T](msg: String)(f: String => T): T = {
debug("%s : start".format(msg))
val r = f(msg)
debug("%s : finish".format(msg))
r
}
def logChild[T](msg: String)(f: => T)(implicit ctx: String): T = {
debug("%s : %s : start".format(ctx, msg))
val r = f
debug("%s : %s : finish".format(ctx, msg))
r
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment