An easy to use and extensible logger for Kotlin.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Add the dependencies:
dependencies {
implementation 'com.github.merklol.KLogger:core:0.3'
//Printer for Android applicartions
implementation 'com.github.merklol.KLogger:androidprinter:0.3'
}
NOTE: By default, ConsolePrinter takes DebugRule as the default value. You can also use ReleaseRule instead. The rule hides all verbose, debug, and info log events.
fun main() {
KLogger.init(ConsolePrinter(rule = ReleaseRule()))
}
Initialize an instance of KLogger and start logging:
fun main() {
KLogger.init(ConsolePrinter())
KLogger.info("Hello there!")
KLogger.warn("Warning!")
KLogger.error("Error!", RuntimeException("Something happened"))
}
Initialize an instance of KLogger in the Application class:
class App: Application() {
override fun onCreate() {
super.onCreate()
KLogger.init(AndroidPrinter())
}
}
Now, you can define log statements in your View files:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
KLogger.info("Hello world!")
}
}
To create a custom printer, implement Printer interface:
class CustomPrinter(override val rule: Rule) : Printer {
override val tag: Tag
get() = super.tag
override fun log(priority: Int, message: String?, throwable: Throwable?) {
TODO("Not yet implemented")
}
}
If you want a custom Tag, then you need to implement Tag interface:
class CustomTag: Tag {
override fun toString(): String {
TODO("Not yet implemented")
}
}
In case you need a custom rule, you should implement Rule interface:
class CustomRule: Rule {
override fun isPrintable(priority: Int): Boolean {
TODO("Not yet implemented")
}
}
If you like this project, or are using it in your app, consider starring the repository to show your support. Contributions from the community are very welcome.