9

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?

Yaron Y. Goland
  • 756
  • 6
  • 15

4 Answers4

8

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?

Community
  • 1
  • 1
skaffman
  • 398,947
  • 96
  • 818
  • 769
2

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

rve
  • 5,897
  • 3
  • 40
  • 64
1

You can also use https://github.com/mvysny/slf4j-handroid - a special fork which logs debug messages during development phase; it also contains workarounds for bugs in Android Studio 1.5 not logging certain exceptions.

Martin Vysny
  • 3,088
  • 28
  • 39
1

This worked for me. To change the default INFO level to DEBUG for the class that called LoggerFactory.getLogger(MyClass.class), type this at the command line:

adb shell setprop log.tag.MyClass DEBUG

Then all of the DEBUG and higher output for MyClass will be in logcat.

pigswig
  • 351
  • 3
  • 7