Pulp is a very simple logger tool for android using Kotlin programming language.
- Add the dependency code to your project
// mavenCentral() is needed
implementation "ir.malv.utils:pulp:$version"Initialize a LogHandler at the start of application
Pulp.addHandler(LogCatHandler())then use the log functions:
Pulp.info("TAG", "This is a message")Pulp.info("TAG", "Message, but not enough") {
"ExtraMessage1" to "Message..."
"ExtraMessage2" to "Message..."
// ...
}val t = Throwable("Error")
Pulp.error("TAG", "Failed", t) {
"Extra data" to "data"
}When Pulp triggers to print a log, it can be listened using callbacks.
Pulp.addHandler(object: Pulp.LogHandler {
override fun onLog(
level: Pulp.Level,
tags: List<String>,
message: String,
t: Throwable?,
data: Pulp.LogData
) {
// Get the data and do stuff
}
})
LogCatHandleris an implementation ofLogHandlerwhich prints to Logcat
Note: You can add multiple handlers and all of them will be called when logging.
- Handlers can also be disabled:
Pulp.setHandlerEnabled(enabled = false)Pulp can save logs into it's database and return the in a LiveData stream.
To enable database call:
Pulp.setDatabaseEnabled(true)And because interacting with database needs context, make sure you have called Pulp.init(context), or Pulp.setApplicationContext(context).
And to interact with database using this code:
val savedLogs: LiveData<List<PulpItems>> = Pulp.getSavedLogs(context)
savedLogs.observe(activity, Observer {
// it is List<PulpItem>
})and to clear the logs:
Pulp.clearLogs(context)-
D: Debug -
I: Info -
W: Warning -
E: Error -
WTF: Unexpected -
All config methods can be chained:
Pulp.init(this)
.setHandlerEnabled(true)
.setLogsEnabled(true)
.setDatabaseEnabled(true)
.addHandler(object : Pulp.LogHandler {
// ...
})