Barmaley
Barmaley

Reputation: 16363

Logging strategy

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.

Upvotes: 5

Views: 1222

Answers (7)

KHanusova
KHanusova

Reputation: 197

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
}

Upvotes: 0

0xC0DED00D
0xC0DED00D

Reputation: 20348

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.

Upvotes: 1

Rolf Kulemann
Rolf Kulemann

Reputation: 211

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

Upvotes: 1

brain
brain

Reputation: 5546

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());
}

Upvotes: 0

pgsandstrom
pgsandstrom

Reputation: 14399

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");

Upvotes: 0

Octavian Helm
Octavian Helm

Reputation: 39604

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.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104178

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

Upvotes: 4

Related Questions