如何告诉Timber哪个日志调用是针对哪个树的? [英] How to tell Timber which log call is for which Tree?

查看:126
本文介绍了如何告诉Timber哪个日志调用是针对哪个树的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Timber库,用于进行日志记录和与崩溃报告服务的协作,并且我的应用程序中同时包含了Crashlytics和Loggly服务.

I have Timber library for logging and cooperation with crash-reporting services and I have both Crashlytics and Loggly services in my app.

因此,我必须种两棵树:

Thus, I had to plant two trees:

Timber.plant(new CrashlyticsTree());
Timber.plant(new LogglyTree(BuildConfig.LOGGLY_TOKEN));

现在,每次我打电话:

Timber.e("bla bla");

我在Loggly中获得了所有日志,但是我希望其中一些日志进入Loggly,而另一些日志进入Crashlytics,那么我该怎么做?

I get all the logs in Loggly, but I want some of them to go to Loggly and some of them to go to Crashlytics, so how do I do that?

推荐答案

将对.e.w的每个调用都作为结果,遍历所有种植的树并调用其各自的.e.w实现.

Turns out every call to .e or .w for example, iterates through all planted trees and calls their respective .e and .w implementations.

这意味着如果我想将库A和库B分开,则需要为每个库使用不同的日志记录优先级.

This means that if I wanted to separate Library A and Library B I needed to use different logging priority for each.

所以我选择对库A使用.e,对库B使用.w.

So I chose to use .e for Library A and .w for Library B.

为此,我必须创建从Timber.HollowTree继承的自定义树,并且仅实现所需的日志调用,而将其余部分保留为空.

In order to do that, I had to create custom trees that inherit from Timber.HollowTree and only implement the needed log call, and leave the rest of them hollow.

public class LibraryATree extends Timber.HollowTree {
    @Override
    public void e(Args){
        // Do something
    }
}

public class LibraryBTree extends Timber.HollowTree {
    @Override
    public void w(Args){
        // Do something
    }
}

Timber.plant(new LibraryATree())
Timber.plant(new LibraryBTree())

现在在我的代码中,如果我想通过LibraryA记录某些内容,请执行以下操作:

Now in my code, if I want to log something via LibraryA, I do this:

Timber.e("Test Library A"); // calls LibraryA's Tree's `.e` method

如果我想使用库B的日志记录实用程序,请执行以下操作:

and if I wanted to use Library B's logging utilities I do this:

Timber.w("Test Library B"); // calls LibraryB's Tree's `.w` method

这篇关于如何告诉Timber哪个日志调用是针对哪个树的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆