Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

android - Does Log.isLoggable returns wrong values?

When I was writing a log wrapper for my android application I noticed a strange behavior of androids Log.isLoggable method. Executing following code:

final String TAG = "Test";
Log.v(TAG, "verbose is active: " + Log.isLoggable(TAG, Log.VERBOSE));
Log.d(TAG, "debug is active: " + Log.isLoggable(TAG, Log.DEBUG));
Log.i(TAG, "info is active: " + Log.isLoggable(TAG, Log.INFO));
Log.w(TAG, "warn is active: " + Log.isLoggable(TAG, Log.WARN));
Log.e(TAG, "error is active: " + Log.isLoggable(TAG, Log.ERROR));

produces following LogCat output:

VERBOSE/Test(598): verbose is active: false
DEBUG/Test(598): debug is active: false
INFO/Test(598): info is active: true
WARN/Test(598): warn is active: true
ERROR/Test(598): error is active: true

Why do I get verbose and debug is not active although I produce these outputs using verbose and debug logging?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you read the information on Log.isLoggable(), you'll notice that the default logging level is INFO. Anything less than that (DEBUG and VERBOSE) will cause this method to return false. This is why your resulting output shows these two as false.

All Log.* calls are logged to the logcat. Calling Log.isLoggable() is simply a way for you to tune logging. It's not required. Typically, you'll call Log.isLoggable() prior to the actual Log.* call to determine whether to log it or not.

You can tune your logging per TAG if you choose, either with a prop file or via adb. The nice thing about this is you can dynamically turn logging up/down/off for each individual TAG in your app without the need for manually commenting out Log lines, or implementing your own checks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...