How do you usually Tag log entries? (android)

后端 未结 13 1567
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 21:54

I assume most of you are aware of android.util.Log All logging methods accept \'String tag\' as a first argument.

And my question is How do you usually tag y

13条回答
  •  春和景丽
    2020-12-22 21:58

    It is a very old question, but even thought an updated answer for July 2018 it is more preferable to use Timber. In order to Log the correct logging, errors and warns can be send to third party crash libraries, such as Firebase or Crashlytics.

    In the class that implements Application you should add this:

    @Override
    public void onCreate() {
        super.onCreate();
        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        } else {
            Timber.plant(new CrashReportingTree());
        }
    }
    
    /** A tree which logs important information for crash reporting. */
    private static class CrashReportingTree extends Timber.Tree {
        @Override protected void log(int priority, String tag, String message, Throwable t) {
            if (priority == Log.VERBOSE || priority == Log.DEBUG) {
                return;
            }
    
            FakeCrashLibrary.log(priority, tag, message);
    
            if (t != null) {
                if (priority == Log.ERROR) {
                    FakeCrashLibrary.logError(t);
                } else if (priority == Log.WARN) {
                    FakeCrashLibrary.logWarning(t);
                }
            }
        }
    }
    

    Do not forget Timber dependency.

    implementation 'com.jakewharton.timber:timber:4.7.1'
    

提交回复
热议问题