Log.d and impact on performance

。_饼干妹妹 提交于 2019-12-28 14:05:10

问题


I'm not entirely sure about what I'm reading in the documentation. Is it ok to leave a bunch of log.d pieces of code scattered about, or should I comment them out so that they don't impact my app's performance.

Thanks,

I'm a little confused because if you read about the log object (documentation) you see this:

"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. "

It almost sounded like it's ok to leave debug messages in there because they are "stripped." Anyway, thanks for the answers, I'll comment them out when I'm done. Not like I need them in there once the app is completed.

Thanks


回答1:


Log has impact on performance, so it's recommended that you comment it out or log with conditional statements.

For example

public class MyActivity extends Activity {
// Debugging
 private static final String TAG = "MyApp";
 private static final boolean D = true;
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(D) Log.e(TAG, "MyActivity.onCreate debug message");
 }

Then in when you publish your release version just change "D" to false




回答2:


My solution:

  • Add unguarded log statements wherever you like
  • Strip them out for release builds



回答3:


Definitely comment them out. They add up quickly and could noticeably slow down your app, especially if you have them in loops.




回答4:


Simply use code guard methods.

if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
Log.d(LOG_TAG, "Your log here");
}


来源:https://stackoverflow.com/questions/3773252/log-d-and-impact-on-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!