Anko: Enable debug logging level for all classes in Anko Logger

Created on 19 Oct 2017  路  6Comments  路  Source: Kotlin/anko

Hi I am using anko logger in my android application. I see that I have to set like this setprop log.tag.LoginActivity DEBUG for my every class where I am doing logging.

Execute this command for each new class file is not a good solution. Is there any way to enable debug log level for entire application. I am doing logging like below

debug { "Registration Activity" }

I see that to solve this issue just create one class and do something like this

public class Logger {
    static String TAG = "MyApp";

    public static void v(String str) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, str);
        }
    }
    public static void v(String str, Throwable t) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, str, t);
        }
    }
}

Then use like this Logger.v("Hello World"); and enable debug level using below command for entire application.

setprop  log.tag.MyApp  VERBOSE

Does anko provide solution for this problem without making own Logger class and one TAG for entire application ?

Most helpful comment

I think this issue needs more attention. The AnkoLogger is not very user-friendly in it's current state. Maybe it would be possible to set this using BuildConfig. E.g If debug build print debug logs?

All 6 comments

I think this issue needs more attention. The AnkoLogger is not very user-friendly in it's current state. Maybe it would be possible to set this using BuildConfig. E.g If debug build print debug logs?

I agree that Anko is doing "the right thing" by checking isLoggable() behind the scenes before it calls Log. That's an important optimization to preserve.

The fact that, by default, isLoggable() returns false for VERBOSE and DEBUG logging levels isn't terribly convenient for devs. It is, effectively, acting as a pre-filter to the log entries shown in Logcat. (For dev builds, I'd prefer to have all log entries "written out" and then just filter once they get to Logcat.)

The general guidance for enabling VERBOSE and DEBUG in Anko is to change the isLoggable default behavior when initiating runs via ADB by setting a system property on the command line to affect this, something like log.tag.MyAppTag=WARN

HOWEVER

  1. Affecting the Anko log-level via a log.tag system property allows you to affect only a single tag-name at a time (Anko, with its "terse" logging statments auto-sets the tag to the classname). This means you cannot "enable debug logging" for the entire application using this technique, you need to enable it on a tag-by-tag basis.

  2. It's stupid-hard to find guidance on how to change the behavior of isLoggable (the log.tag System Property) when not initiating runs from adb but instead initiating runs via an Android Studio run configuration. Android studio run configurations provide no way I can see to "set system properties".

+1 for spending some more time getting this "right". In the least I would suggest the following bare-minimum functionality:

  1. Improving the documentation for Anko Logging such that it's clear how to override the isLoggable() defaults in a variety of situations/contexts (notably, when running via Android Studio Run Configurations).

OR (better yet)

  1. Change the underlying mechanism by which Anko determines what to "pre-filter". Let whatever the underlying flag is that's used as a pre-filter check be able to affect logging app-wide (not just a single tag) AND ensure that this flag is controllable via Android Studio Run Configurations. Perhaps do not use isLoggable() checks, they seem to be an arcane little corner of Android that's poorly documented, at least WRT to Android Studio.

Anything being done on this front or it's just generally not recommended to use AnkoLogger now?

Any news?

Also interested. We can't use it for now

Ya I wish there was an option to just skip the isLoggable check, it's such a pain other wise. Might have to switch to another logger.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nkanellopoulos picture nkanellopoulos  路  4Comments

vlonjatg picture vlonjatg  路  3Comments

Anilugale picture Anilugale  路  4Comments

jeantuffier picture jeantuffier  路  5Comments

SalomonBrys picture SalomonBrys  路  3Comments