[JAVA] Correct log output method on Android (standard Log class vs AnkoLogger)

background

Introducing AnkoLogger developed by JetBrains as OSS for Java-> Kotlin migration. I tried, but even if I output verbose and debug log, it was not output to logcat of Android Studio. While investigating the cause, I noticed that I had misunderstood the behavior of the standard Log class ʻandroid.util.Log` rather than the problem of AnkoLogger. ..

Log level and behavior of each library

According to my own recognition so far, if I write Log.d (TAG," foo "), the log will be output when the log level is debug or lower (that is, debug, verbose), and verbose / is always in the development environment. I was wondering if I was looking at the debug log because the log level was verbose. In conclusion, the recognition of ↑ was wrong, and the default log level was info. Then, the reason why the verbose / debug log is output is that even if you write ** Log.d (TAG," foo "), it seems that the log is actually output regardless of the log level **. It is necessary to judge the log level by another method, and to separate it, it is necessary to control it with the if statement as follows. (By the way, the log level is set for each tag)

if ( Log.isLoggable(TAG, DEBUG) ) Log.d(TAG, "foo");

Why is it such a redundant specification? ..

On the other hand, Anko Logger

debug("foo") //The tag defaults to the calling Activity name

If you write, it will determine the log level internally. The user side is output according to the log level without writing an if statement. Personally, this is by far the most intuitive. The same question was raised on the issue on Github, so I'm not the only one who misunderstood the specifications of the standard Log class.

Summary

If you organize it again,

Given the clarity and security of the code, I personally prefer to use AnkoLogger. If you're using Java or don't want to include useless dependent libraries, you might want to create an AnkoLogger-like standard Log wrapper class.

Recommended Posts

Correct log output method on Android (standard Log class vs AnkoLogger)
I want to simplify the log output on Android
Java standard log output sample