Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log.d and impact on performance

Tags:

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

like image 200
Andi Jay Avatar asked Sep 22 '10 20:09

Andi Jay


People also ask

Does log affect performance?

Logging does effect performance because: It means extra method calls. (Not an issue for 99% of apps). It often means String assembly work (Big impact)

Does logging slow down performance?

Logging does have its drawbacks also. It can slow down an application as it uses some CPU cycles and other resources (memory, etc). To answer these concerns, log4j is designed to be reliable, fast and extensible. Also, one should only log an error or something that must absolutely be logged.

What does D mean in log D?

Here is the one that you will use most often in this book: public static int d(String tag, String msg) The d stands for “debug” and refers to the level of the log message.


2 Answers

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

like image 80
MatteKarla Avatar answered Sep 21 '22 17:09

MatteKarla


My solution:

  • Add unguarded log statements wherever you like
  • Strip them out for release builds
like image 45
Christopher Orr Avatar answered Sep 19 '22 17:09

Christopher Orr