https://kotlinlang.org logo
#kotlin-logging
Title
# kotlin-logging
t

Tower Guidev2

07/14/2022, 1:04 PM
Hi Im new to Kotlin Logging and need some help 😄
Im looking to replace
com.jakewharton.timber:timber:5.0.1
in my Android applications with "something" and currently looking at
Copy code
api 'io.github.microutils:kotlin-logging:2.1.23'
api 'org.slf4j:slf4j-android:1.7.36'
i have a very basic logger working fine in my application however a number of questions arise from what i am seeing in my
logcat
output 1). i would like to have one "logger" defined as follows in my base activity and use this to log messages in all the activities that extend it
Copy code
import android.os.Bundle
import androidx.activity.ComponentActivity
import dagger.hilt.android.AndroidEntryPoint
import mu.KotlinLogging

@AndroidEntryPoint
open class FoundationActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    companion object{
        val logger = KotlinLogging.logger {this@Companion}
    }
}
the above works fine, apart from the log messages contain the FoundationActivity name and not the extending activity
E/FoundationActivity: This is logging of - kotlin-logging error 2c5c1d75-2968-4cf0-a26f-64231a097ec6
W/FoundationActivity: This is logging of - kotlin-logging warn 474cf564-45eb-4f7b-97ab-e7a981fa675e
I/FoundationActivity: This is logging of - kotlin-logging INFO 88b8c2e0-706d-4170-bc4e-1ecab98f1606
do i have to have a logger defined in each seperate activity to achieve my desired output? or is there an approach that will allow me to have a single logger that reports each "calling" activity 2). i understand debug logs will only show when isDebugEnabled is true, how do i enable debug? what documentation have i missed?
o

oshai

07/18/2022, 1:36 AM
1. for logger to write the desired name you'll need to define one per file/ class (and make it private). this is the best practice when defining loggers as this helps to find the message (you know in what file it was written).
about debug logs check how to enable them in android
under the hood this facade is calling the android native logging to do the logging - nothing more than that
🙌 1
74 Views