调试与错误处理

Glide在加载过程中出现异常默认情况下不会打日志。Glide为你提供了两种方式查看和处理这些异常。

调试

仅仅为了查看异常的话,你可以为GenericRequest类打开Debug日志。这个类处理所有媒体资源的加载响应(response)。你可以在命令行里运行:

adb shell setprop log.tag.GenericRequest DEBUG

想要包括详细的请求时序信息,你可以把DEBUG缓存VERBOSE

关闭日志使用:

adb shell setprop log.tag.GenericRequest ERROR

调试工作流

为了查看内部引擎(engine)如何以及何时查找到资源,你可以打开日志:

adb shell setprop log.tag.Engine VERBOSE
adb shell setprop log.tag.EngineJob VERBOSE
adb shell setprop log.tag.DecodeJob VERBOSE

打开这个有助于你找出为什么某个资源没有从内存缓存中加载,为什么请求再次从外部的url下载数据。这也可以帮助你了解:如果想命中磁盘缓存,什么样的参数需要匹配。启用DecodeJob日志也可以帮助你去定位自定义transformation/decoder/encoder相关的问题。

监听请求-RequestListener

虽然启用debug日志很简单,但是只有在你可以连接到设备时才能这样干。为了把Glide集成到已有的更专业的错误日志系统。你可以使用RequestListener类的onException()。当请求失败时,该方法会告知你导致失败的异常(Exception),如果解码器(Decoder)不能从数据中解析出任何有用的信息,Exception也可能传null。你可以使用listener()API传一个你的监听器(listener)到每一个请求中。 请确保onException()返回false,以免覆盖了Glide的默认错误处理行为(例如,它默认会通知Target这个error)。 这是一个快速调试的例子:

// example usage: .listener(new LoggingListener<String, GlideDrawable>())
public class LoggingListener<T, R> implements RequestListener<T, R> {
    @Override public boolean onException(Exception e, Object model, Target target, boolean isFirstResource) {
        android.util.Log.d("GLIDE", String.format(Locale.ROOT,
                "onException(%s, %s, %s, %s)", e, model, target, isFirstResource), e);
        return false;
    }
    @Override public boolean onResourceReady(Object resource, Object model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
        android.util.Log.d("GLIDE", String.format(Locale.ROOT,
                "onResourceReady(%s, %s, %s, %s, %s)", resource, model, target, isFromMemoryCache, isFirstResource));
        return false;
    }
}

确保发版前移除相关代码

更多日志

这个列表是给3.6.0版本用的,可能不完整。

cd .../android-sdk/platform-tools
adb shell setprop log.tag.AnimatedGifEncoder VERBOSE
adb shell setprop log.tag.AssetUriFetcher VERBOSE
adb shell setprop log.tag.BitmapEncoder VERBOSE
adb shell setprop log.tag.BufferedIs VERBOSE
adb shell setprop log.tag.ByteArrayPool VERBOSE
adb shell setprop log.tag.CacheLoader VERBOSE
adb shell setprop log.tag.ContentLengthStream VERBOSE
adb shell setprop log.tag.DecodeJob VERBOSE
adb shell setprop log.tag.DiskLruCacheWrapper VERBOSE
adb shell setprop log.tag.Downsampler VERBOSE
adb shell setprop log.tag.Engine VERBOSE
adb shell setprop log.tag.EngineRunnable VERBOSE
adb shell setprop log.tag.GenericRequest VERBOSE
adb shell setprop log.tag.GifDecoder VERBOSE
adb shell setprop log.tag.GifEncoder VERBOSE
adb shell setprop log.tag.GifHeaderParser VERBOSE
adb shell setprop log.tag.GifResourceDecoder VERBOSE
adb shell setprop log.tag.Glide VERBOSE
adb shell setprop log.tag.ImageHeaderParser VERBOSE
adb shell setprop log.tag.ImageVideoDecoder VERBOSE
adb shell setprop log.tag.IVML VERBOSE
adb shell setprop log.tag.LocalUriFetcher VERBOSE
adb shell setprop log.tag.LruBitmapPool VERBOSE
adb shell setprop log.tag.MediaStoreThumbFetcher VERBOSE
adb shell setprop log.tag.MemorySizeCalculator VERBOSE
adb shell setprop log.tag.PreFillRunner VERBOSE
adb shell setprop log.tag.ResourceLoader VERBOSE
adb shell setprop log.tag.RMRetriever VERBOSE
adb shell setprop log.tag.StreamEncoder VERBOSE
adb shell setprop log.tag.TransformationUtils VERBOSE