I am using slf4j-android 1.6.1-RC1 via gradle/maven and when I call Log.debug nothing comes out in Logcat under Android Studio 0.3.5 when I run an 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!");
The Log.d's output did appear in Logcat but Log.debug did not.
I checked Log.isDebugEnabled() and sure enough it is set to false. But that seems weird since android.util.Log.d works just fine. Shouldn't slf4j be using the same log level? In fact, shouldn't slf4j just be calling android.util.Log under the covers?
I also replaced Log.debug with Log.error and that did work. So the problem seems to be that slf4j has somehow decided that debug events shouldn't be emitted even though Log.d will emit them.
How do I get slf4j to honor the log level set in Logcat in Android Studio like android.util.Log does?
defaultLogLevel=warn” then I will not see debug and info logs on the console. I hope this tip will help you.
The Log class allows you to create log messages that appear in logcat.
Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class. This page is about the command-line logcat tool, but you can also view log messages from the Logcat window in Android Studio.
If you look at the source for slf4j-android, you can see that it calls android.util.Log#isLoggable
to decide if the log entry should be made. The javadoc for isLoggable
says (my emphasis):
Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: 'setprop log.tag. ' Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: 'log.tag.=' and place that in /data/local.prop.
So by default calling slf4j's Logger.debug
will do nothing. On the other hand, android.util.Log.d
doesn't call isLoggable
, and so the log entry is made.
None of the options mentioned in the javadoc are palatable, which rather renders slf4j's Logger.debug
less than useful. It'd be nice if the logger could be programatically configured to ignore isLoggable
, but at the time of writing this it can't.
See also Does Log.isLoggable returns wrong values?
I found this version much easier to use: http://noveogroup.github.io/android-logger/
You can set the desired log level in an android-logger.properties
configuration file. It is not exactly honoring the Logcat log level but at least you can show debug messages without messing around with setprop
when using slf4j-android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With