Reputation: 23
In my project, the Log function has been used in many places, and there isn't any "if statement" to control it.we use just like this:
...
String s = generateString();
Log.d(TAG, "result is : " + s);
...
When I see the consumer of time by the android profiler, the Log function seems to cost a lot of time.
Does anyone know the actual time consume of Log function?Thank you very much!
Upvotes: 0
Views: 1101
Reputation: 2443
Addition to Gils Answer which is correct: I recommend using Timber for Android Logging.
Besides a lot of helpful features Timber can add to your development, you can do something like that to avoid Log statements in your release app:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//Only log when we are in DEBUG mode
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
}
Upvotes: 1
Reputation: 16761
From the documentation:
The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
So, if you're looking to show logs while debugging only, you can rely on the DEBUG logs (Log.d), as they are removed for release builds (which may be bad if you rely on log dumbs from user feedback). Of course, if you're just using logs for debugging and plan to remove them once you're done, then it doesn't really matter.
As for how much time, I think that's the wrong question, as times will vary between devices. The question should be "Is there a significant cost?"
To answer this question - Yes, logs absolutely cause a heavy strain on performance and should be stripped from the code apart from the most important ones before releasing. Try to avoid printing logs on the main / render threads, as these will have a direct & substantial impact on your app's responsiveness.
Upvotes: 4