Might I ask how you guys use a logging framework i...
# android
d
Might I ask how you guys use a logging framework in code that has to be unit tested? Standard Android Log. provokes the not mocked exception.
d
Just for clarification: Its a singleton and you can switch out the logging function if necessary? So before each test you would do that and in production code it defaults to the firebase implementation?
m
Yes, although this is more for functional tests.
d
That's very smart! Thank you for sharing that!
m
Works with everything really. Retrofit interfaces, GPS or even facebook login button (if you wrap your head around how to create a provider for that).
For unit tests I'd recommend natural dependency inversion tho.
I rarely log stuff, but for testing analytics events calls are made, it's good.
d
Its just amazing what you can do with functions in kotlin, storing a function in a property is just so science fiction if you come from java 6. And for logging: the same. Since I learned automated testing there is no need for it in my app - but the use case for logging mght arise somewhere. So its good to know (thank you for that!)
l
In my case, I use slf4j + timber for app, simplelogger for unittests. While you can inject logger, with slf4j you usually use static factory method:
Copy code
companion object {
        private val logger = LoggerFactory.getLogger(BaseActivity::class.java.simpleName)
    }
After that it can be used as usual:
Copy code
logger.debug("onCreate() called with: savedInstanceState = [{}]", savedInstanceState)