couchbase / couchbase-lite-android-ce

The community edition of couchbase lite for android
Apache License 2.0
9 stars 1 forks source link

Mismatch between com.couchbase.lite.LogLevel and android.util.Log #29

Closed benjaminglatzeder closed 5 years ago

benjaminglatzeder commented 5 years ago

Setting LogLevel to verbose Database.log.getConsole().setLevel(LogLevel.VERBOSE); shows Couchbase logs as INFO logs in Logcat. Setting LogLevel to warning Database.log.getConsole().setLevel(LogLevel.WARNING); hides them completely. Others LogLevels seem to be wrong, too.

Constants in Couchbase Lite are:

package com.couchbase.lite.internal.core;

public final class C4Constants {
    private C4Constants() {}

    ////////////////////////////////////
    // c4Base.h
    ////////////////////////////////////
    public static final class LogLevel {
        private LogLevel() {}

        public static final int DEBUG = 0;
        public static final int VERBOSE = 1;
        public static final int INFO = 2;
        public static final int WARNING = 3;
        public static final int ERROR = 4;
        public static final int NONE = 5;

And Android: https://developer.android.com/reference/android/util/Log.html#DEBUG DEBUG = 3 ERROR = 6 INFO = 4 VERBOSE = 2 WARN = 5

Values might be assigned a different value in native Couchbase Lite module. I did not check this.

package com.couchbase.lite.internal.core;

public class C4Log {
    //...
    //-------------------------------------------------------------------------
    // native methods
    //-------------------------------------------------------------------------

    public static native void setLevel(String domain, int level);
}
bmeike commented 5 years ago

Sorry!

As you know, Couchbase Lite is a multi-platform product. There is absolutely no reason to believe that our internal implementations of the various log levels have anything to do with Android's internal representation of log levels. In fact, it would be completely impossible to do that, for all platforms.

We should probably not put getValue in the public API. Given that we do, we should document the fact that the number that it provides is utterly useless to client code. ... and, better yet, we should provide a library of useful tools, one of which maps CBLite log levels to their Android equivalents.

Several of these things are under consideration.

In the main time, you should use code like this:

    public void log(LogLevel level, LogDomain domain, String message) {
        switch (level) {
            case DEBUG:
                Log.d(domain, message);
                break;
            case VERBOSE:
                Log.v(domain, message);
                break;
            case INFO:
                Log.i(domain, message);
                break;
            case WARNING:
                Log.w(domain, message);
                break;
            case ERROR:
                Log.e(domain, message);
                break;
        }
    }

... which says exactly what you mean to say, about as clearly as it is possible to say it