错误或在android.util.Log? - Log.isLoggable(调试)= FALSE,但Log.d()不是禁用 [英] Bug or feature in android.util.Log? - Log.isLoggable(DEBUG) = false but Log.d() is not disabled
问题描述
更新:新配方的问题和头:
Update: Reformulated the question and header:
我一直以为问如果日志被激活像这样昂贵的Android日志记录方法可以优化
I always thought that expensive android Logging methods can be optimized by asking if logging is active like this
import android.util.Log;
if (Log.isLoggable("MyContext", Log.DEBUG))
{
Log.d("MyContext", "my logging: " + callExpensiveCalculation());
}
不过了Android 2.2的模拟器尝试这种时候,我Log.d()永远不会被调用。
However when trying this with the android 2.2 emulator my Log.d() is never called.
所以,我想这code
So i tried this code
Log.v(MY_CONTEXT, "VERBOSE logging is active: " + Log.isLoggable(MY_CONTEXT, Log.VERBOSE));
Log.d(MY_CONTEXT, "DEBUG logging is active: " + Log.isLoggable(MY_CONTEXT, Log.DEBUG));
Log.i(MY_CONTEXT, "INFO logging is active: " + Log.isLoggable(MY_CONTEXT, Log.INFO));
Log.w(MY_CONTEXT, "WARN logging is active: " + Log.isLoggable(MY_CONTEXT, Log.WARN));
Log.e(MY_CONTEXT, "ERROR logging is active: " + Log.isLoggable(MY_CONTEXT, Log.ERROR));
和出乎我的意料,我得到
and to my surprise i got
02-27 19:05:43.015: V/MyContext(334): VERBOSE logging is active: false
02-27 19:05:43.015: D/MyContext(334): DEBUG logging is active: false
02-27 19:05:43.015: I/MyContext(334): INFO logging is active: true
02-27 19:05:43.015: W/MyContext(334): WARN logging is active: true
02-27 19:05:43.015: E/MyContext(334): ERROR logging is active: true
因此,如果日志被禁用日志记录也可使用。
这是Android还是在我的测试 - code错误?
so logging works even if logging is disabled. Is this a bug in android or in my test-code?
是否有其他的方式,以找出是否调试(或其他记录等级的一种)有效与否?
Is there an other way to find out if debugging (or one of the other loglevels) is active or not?
我用eclipse的logcat视图与日志级别冗长和运行在Android应用开始从日食测试
I use eclipse logcat-view with log-level verbose and started the test from eclipse with run as android-app
推荐答案
isLoggable
真的只是一个机制,为您提供方便某些标签提供一个标志。它实际上并没有做任何事情来禁用日志记录。您code的第一个块是正确的:
isLoggable
is really just a mechanism to provide a flag for certain tags for your convenience. It doesn't actually do anything to disable logging. Your first block of code is correct:
if (Log.isLoggable("MyContext", Log.DEBUG))
{
Log.d("MyContext", "my logging: " + callExpensiveCalculation());
}
这将记录或取决于如果 isLoggable
收益真正
或假无法登录
。但是,当你这样做(而不检查 isLoggable
)
This will log or not log depending on if isLoggable
returns true
or false
. However, when you do this (without checking isLoggable
):
Log.d(MY_CONTEXT, "DEBUG logging is active: " + Log.isLoggable(MY_CONTEXT, Log.DEBUG));
它会不管是什么 isLoggable
将返回已调用它记录。总之,你的需要以做检查无处不在,如果你想根据该标志启用/禁用日志记录您登录。在如果
语句,可以让你跳过不必要的日志中的一部分。如果如果
子句假
中,code里面永远不会运行。
It will log regardless of what isLoggable
would have returned had you called it. In short, you need to do that check everywhere that you log if you want to enable/disable logging based on that flag. The if
statement is the part that lets you skip unnecessary logs. If the if
clause is false
, the code inside is never run.
这篇关于错误或在android.util.Log? - Log.isLoggable(调试)= FALSE,但Log.d()不是禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!