Mikhail Boyarsky
Mikhail Boyarsky

Reputation: 3038

How to configure slf4j-android?

I use slf4j-android in my Android application. The doc says if I write log.trace then it's the almost the same as I write Log.v. But the default logging level seems to be INFO, and logcat shows only log.info and above, and I can't see how can I change the defaults.

1) How can I configure slf4j-android

2) If I can't: are there any other slf4j implementations for Android, more configurable

Upvotes: 9

Views: 3707

Answers (2)

Paulo
Paulo

Reputation: 767

It's a pain, but I won't give up on slf4j. This is what I ended up doing:

  1. In my source code, I instantiate a logger per activity:
public class MyActivity extends AppCompatActivity {
    private final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(this.getClass().getName());
    ...
}
  1. In the project root, I place a shell script for setting the logging level of all activities in one go:
#!/bin/sh
# Add as many lines as needed to set the logging for 
# all the activities in your app
adb shell setprop log.tag.MyActivity "$1"
  1. Then, whenever I need to set the logging level for a session, I switch to the Terminal view in Android Studio and just run the script:
$ sh adb-setloglevel VERBOSE

Upvotes: 0

marekdef
marekdef

Reputation: 481

For example if your tag is MyClass type in command line

adb shell setprop log.tag.MyClass DEBUG

The way I found is this procedure Find logger slf4j-android source here

You should see that all methods are preceded by if (isLoggable(...)) that in turn call android.util.Log isLoggable which can be found on opengrok

The description of this method explains how to set a proper property.

Note that slf4j-android will rename longer tags i.e. WiFiDirectBroadcastReceiver will be changed to sth like *directBroadcastReceiver. As far as I know you cannot use * in setprop calls so you need to manage your tags carefully.

Upvotes: 2

Related Questions