5

I'm about to finish my Android application. In the end I have found that I've been using bunch of logging statements, like:

Log.d(TAG, "Blah-blah");

The question is: for production release what should I do with them?

  1. Just comment/stripe log statements
  2. Do something else more sophisticated? Like as I used to do with Log4J properties or so

Please share your experience.

Barmaley
  • 16,638
  • 18
  • 73
  • 146

7 Answers7

4

You can remove the logging statements in build time using an obfuscation tool. See here for details.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • OK, I will investigate option. Anyway, are there any possibilities like properties in Log4J? – Barmaley Dec 02 '10 at 13:52
  • From what I know, all messages are logged. Then the viewer of the log can select, which messages to appear. You can't disable debugging messages to go into the log, by simply editng a property. – kgiannakakis Dec 02 '10 at 14:15
1

I have created a library for this specific purpose. It can be found here - LumberJack. You can install it using Jitpack and gradle (Please check README.md).

After installing, you'll have to change all Log calls to LumberJack calls (eg. LumberJack.d() instead of Log.d() etc.)

Tags are optional and by default set to "LumberJack". You can choose to set the default tag yourself.

You can change the filtering anytime using LumberJack.setLogLevel() method. To remove all the logs, you can just set the LogLevel to LogLevel.None.

LumberJack.setLogLevel(LogLevel.None);

So if you just want to remove all the logcat spamming logs, you will just have to set the log level filter.

Optionally you can choose to log into a text file instead of logcat with same filtering mechanism.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
1

There is a new project, which enables log4j on android. Using lo4gj over slf4j is possible. It also provides an appender for LogCat. See project android-logging-log4j or log4j support in android

Community
  • 1
  • 1
Rolf Kulemann
  • 213
  • 2
  • 6
0

Depends. If you expect the application to crash often then include one of the crash reporting libraries for example but whatever you decide to do just don't release it with the Log.d() methods.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
0

I do it like this, making the compiler remove all logging if DEBUG is false:

if (Constant.DEBUG) Log.d(TAG, "mein gott, state is roflcopter");
pgsandstrom
  • 14,361
  • 13
  • 70
  • 104
  • 1
    This method is not really wrong neither it is a best practice if you ask me as it bloats your code. – Octavian Helm Dec 02 '10 at 09:06
  • Logging is supposed to be as simple as possible. Later someone may change Log.d to Log.w, but forget to change Constant.DEBUG (or vice versa). – inazaruk Dec 02 '10 at 09:13
  • You may have a point, everything should be as simple as possible. But it is better to solve the problem with a bit of complexity then to do nothing at all. This is the best solution I have found, since I find the bloat to be minimal. – pgsandstrom Dec 02 '10 at 09:24
  • what do you mean the compiler remove all logging? – Thupten Dec 21 '16 at 05:58
  • If the compiler sees that Constant.DEBUG always is false, it can conclude that the whole statement is superfluous and remove it. A call to Log.d() is not removed by the compiler. However, 6 years has passed since I answered this. I would now rather ignore the debug-logging than use this solution. The performance loss is negligible. – pgsandstrom Dec 21 '16 at 09:45
0

I've not experience on Android specifically but I'd just leave the logging staements in the code and turn off logging in the log4j properties file. You might even want to leave some logging turned on so that your app will generate useful logs in the event of a crash.

If you are worried about the log statement generation being too computational intensive (e.g. calling toString on a big collection) then you can use this pattern.

if (Log.isDebugEnabled()) {
   Log.Debug(bigCollection.toString());
}
brain
  • 5,496
  • 1
  • 26
  • 29
0

If you do not want to log something in Android release version, you can use automatically generated BuildConfig. See more about it here: https://developer.android.com/studio/build/gradle-tips.html. You can also read more information in this question: BuildConfig file in android - Purpose and Possibilities.

So in your code you simply write something like this:

if (BuildConfig.DEBUG) {
    //your code just for development goes here
}
KHanusova
  • 187
  • 2
  • 10