Created
November 22, 2017 08:56
-
-
Save ymkawb/bae56d9d87643fb9a7432cb52f71c3e0 to your computer and use it in GitHub Desktop.
Kotlin loggable trait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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