Skip to content

Instantly share code, notes, and snippets.

@ymkawb
Created November 22, 2017 08:56
Show Gist options
  • Save ymkawb/bae56d9d87643fb9a7432cb52f71c3e0 to your computer and use it in GitHub Desktop.
Save ymkawb/bae56d9d87643fb9a7432cb52f71c3e0 to your computer and use it in GitHub Desktop.
Kotlin loggable trait
interface Loggable {
val logTag: String
fun debug(message: String) = Log.d(logTag, message)
fun debug(message: String, vararg args: String) = Log.d(logTag, message, args)
fun debug(function: () -> String) = logIfEnabled(Log.DEBUG, function) {
Log.d(logTag, it)
}
fun info(message: String) = Log.i(logTag, message)
fun info(template: String, vararg args: String) = Log.i(logTag, template, args)
fun info(function: () -> String) = logIfEnabled(Log.INFO, function) {
Log.i(logTag, it)
}
private fun logIfEnabled(level: Int,
messageProducer: () -> String,
logFunction: (String) -> Unit) {
if (Log.isEnabled(level)) logFunction(messageProducer())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment