0

How can we create a verbose/logging mode in the app which when switched on will print logging statements from the app and on switching it off no log statements will be printed on the console? One way is to create a preferences option and do a ton of if and else, which does not sound very good. Is there a standard way to do it in Android?

Nitin
  • 1,451
  • 13
  • 17
  • I think this is the best solution [Deactivate any log before publishing](http://stackoverflow.com/questions/2446248/deactivate-any-calls-to-log-before-publishing-are-there-tools-to-do-this) – Vivart Jan 03 '12 at 05:42

3 Answers3

1

I've never done this myself but check here : http://developer.android.com/reference/android/util/Log.html

Under the isLoggable function it mentions how to enable/disable logging for different types (ERROR, INFO, VERBOSE, etc).

That is, if you're okay having this setting in one file and changing that when you need to.

nmjohn
  • 1,432
  • 7
  • 16
0

I created a simple Class to do my logs, so I can easily switch it off. I'm sure there's plenty of rooms for improvements, but it works :)

So use this Log Class instead of the android one:

public class Log {
private static String tag = "YOUR_LOG_TAG";

public static void d(String... params){
    for(String m : params){
        android.util.Log.d(tag, m);
    }
}

public static void d(int m){
    android.util.Log.d(tag, String.valueOf(m));
}

public static void d(String iTag, String m){
    android.util.Log.d(iTag, m);
}

public static void e(String iTag, String m, Throwable t){
    android.util.Log.e(iTag, m, t);
}

public static void e(String m, Throwable t){
    android.util.Log.e(tag, m, t);
}

public static void e(String iTag, String m){
    android.util.Log.e(iTag, m);
}

}
mseo
  • 3,881
  • 3
  • 22
  • 26
0

I agree with @nmjohn that using the android logger is the easiest way to do logging. Together with the Eclipse-adt-plugin-view "LogCat" you can decide at runtime, what you want to see from the logging when your device is connected to development-pc via usb.

If you want to write portable busineslogic you can use the Simple Logging Facade for Java(SLF4J). There is already a "do nothig" implementatin and a implementation that uses the android Log mechanism.

If you need a special kind of logging you can easily create your own SLF4J-implementation that can write to a disk file or do something else.

As far as i know the java standard logger log4j does not work for android because of missing dependencies and a memory footprint that is to big for android.

k3b
  • 14,517
  • 7
  • 53
  • 85