How to get slf4j-android to evaluate Logcat logging level?

I use slf4j-android 1.6.1-RC1 via gradle / maven and when I call Log.debug nothing appears in Logcat under Android Studio 0.3.5 when I run the application in the emulator.

Just for fun, I tried the following:

private final Logger Log = LoggerFactory.getLogger(MainActivity.class); ... Log.debug("Got this far, woohoo!"); android.util.Log.d("blah","I am here!"); 

Log.d output appeared in Logcat, but Log.debug did not.

I checked Log.isDebugEnabled () and I'm sure it is set to false. But this seems odd since android.util.Log.d works just fine. Should slf4j use the same log level? Actually, shouldn't slf4j just call android.util.Log under the covers?

I also replaced Log.debug with Log.error and it worked. So the problem is that slf4j somehow decided that debugging events should not be issued, even if Log.d throws them out.

How do I get slf4j to honor the log level set in Logcat in Android Studio, e.g. android.util.Log?

+6
source share
3 answers

If you look at the source for slf4j-android, you will see that it calls android.util.Log#isLoggable to decide whether to write a log entry. Javadoc for isLoggable says (my emphasis):

Checks if the log is logged for the specified tag at the specified level. By default, any tag is set to INFO . This means that any level above and including INFO will be registered. Before making any calls to the logging method, you should check to see if your tag should be logged. You can change the default level to set the system property: 'setprop log.tag. 'Where is the level - VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT or SUPPRESS. SUPPRESS will disable all entries for your tag. You can also create a local.prop file that contains the following: 'log.tag. = 'and put this in /data/local.prop.

Therefore, by default, calling slf4j Logger.debug do nothing. On the other hand, android.util.Log.d does not call isLoggable , so isLoggable is done.

None of the options mentioned in javadoc are acceptable, which makes slf4j Logger.debug less useful. It would be nice if the registrar could be programmatically configured to ignore isLoggable , but at the time of this writing it cannot.

See also Does Log.isLoggable Return Wrong Values?

+4
source

I found this version much easier to use: http://noveogroup.imtqy.com/android-logger/

You can set the desired log level in the android-logger.properties configuration android-logger.properties . This doesn't exactly match the Logcat log level, but at least you can display debug messages without using setprop when using slf4j-android

0
source

You can also use https://github.com/mvysny/slf4j-handroid - a special plug that logs debugging messages at the development stage; it also contains workarounds for errors in Android Studio 1.5 that do not log specific exceptions.

0
source

Source: https://habr.com/ru/post/958095/


All Articles