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. ..
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.
If you organize it again,
Log.isLoggable
, all logs will be output even in production (log level: info), which will lead to performance degradation.
--If you do not explicitly control with Log.isLoggable
, you can check the log with logcat without doing anything. Meaning Easy during development
--The correct usage is here
--There seems to be another way to prevent ProGuard from logging only release builds. $ adb shell setprop log.tag.MainActivity VERBOSE
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.