How to Enable/Disable Log Levels in Android

How do I enable/disable log levels in Android?

A common way is to make an int named loglevel, and define its debug level based on loglevel.

public static int LOGLEVEL = 2;
public static boolean ERROR = LOGLEVEL > 0;
public static boolean WARN = LOGLEVEL > 1;
...
public static boolean VERBOSE = LOGLEVEL > 4;

if (VERBOSE) Log.v(TAG, "Message here"); // Won't be shown
if (WARN) Log.w(TAG, "WARNING HERE"); // Still goes through

Later, you can just change the LOGLEVEL for all debug output level.

How to remove all debug logging calls before building the release version of an Android app?

I find a far easier solution is to forget all the if checks all over the place and just use ProGuard to strip out any Log.d() or Log.v() method calls when we call our Ant release target.

That way, we always have the debug info being output for regular builds and don't have to make any code changes for release builds. ProGuard can also do multiple passes over the bytecode to remove other undesired statements, empty blocks and can automatically inline short methods where appropriate.

For example, here's a very basic ProGuard config for Android:

-dontskipnonpubliclibraryclasses
-dontobfuscate
-forceprocessing
-optimizationpasses 5

-keep class * extends android.app.Activity
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
}

So you would save that to a file, then call ProGuard from Ant, passing in your just-compiled JAR and the Android platform JAR you're using.

See also the examples in the ProGuard manual.


Update (4.5 years later): Nowadays I used Timber for Android logging.

Not only is it a bit nicer than the default Log implementation — the log tag is set automatically, and it's easy to log formatted strings and exceptions — but you can also specify different logging behaviours at runtime.

In this example, logging statements will only be written to logcat in debug builds of my app:

Timber is set up in my Application onCreate() method:

if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}

Then anywhere else in my code I can log easily:

Timber.d("Downloading URL: %s", url);
try {
// ...
} catch (IOException ioe) {
Timber.e(ioe, "Bad things happened!");
}

See the Timber sample app for a more advanced example, where all log statements are sent to logcat during development and, in production, no debug statements are logged, but errors are silently reported to Crashlytics.

Android : How to enable logging Log.v values which are by default disabled

All logs are always default enabled.

I think you are viewing logs in error view.

Check

  1. You are viewing logs in Verbose
  2. Your filter is set to selected app.
  3. You have selected your app not other app. (where in below image no debuggable process is written)

Sample Image

Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?

Log.e: This is for when bad stuff happens. Use this tag in places like inside a catch statement. You know that an error has occurred and therefore you're logging an error.

Log.w: Use this when you suspect something shady is going on. You may not be completely in full on error mode, but maybe you recovered from some unexpected behavior. Basically, use this to log stuff you didn't expect to happen but isn't necessarily an error. Kind of like a "hey, this happened, and it's weird, we should look into it."

Log.i: Use this to post useful information to the log. For example: that you have successfully connected to a server. Basically use it to report successes.

Log.d: Use this for debugging purposes. If you want to print out a bunch of messages so you can log the exact flow of your program, use this. If you want to keep a log of variable values, use this.

Log.v: Use this when you want to go absolutely nuts with your logging. If for some reason you've decided to log every little thing in a particular part of your app, use the Log.v tag.

Explanation by link Kurtis Nusbaum

Update:

If above things does not work then you are facing an device setting issue. Some mobiles have set default log level to DEBUG or ERROR. Allow logging from phone setting.

You can check if log is loggable by Log.isLoggable()

Check

Settings -> Accessibility -> Developer options -> advanced logging->set "Allow all"

or

Settings->Accessibility - > Developer Options -> Performance optimization -> Advanced logging -> set "Allow all"

or for other phone search in "developers options": option "logging" and set "all".

  • also you can use Log.wtf when your Log.d is not working.
  • try restarting android studio also

Flexible enable/disable logging in Android app

You can check the DEBUG boolean in your BuildConfig:

if (BuildConfig.DEBUG) {
// Do what you need
}

Or else, you can have a debug variable, but instead or keeping it in every activity, declare it in you Application class, and check it's value whenever you need.

If your purpose of that variable is for logging, is a good practice to wrap your loggings into another class, which checks the DEBUG variable:

public class LogUtils {
public static void LOGD(final String tag, String message) {
if (BuildConfig.DEBUG) {
Log.d(tag, message);
}
}

public static void LOGV(final String tag, String message) {
if (BuildConfig.DEBUG) {
Log.v(tag, message);
}
}

public static void LOGI(final String tag, String message) {
if (BuildConfig.DEBUG) {
Log.i(tag, message);
}
}

public static void LOGW(final String tag, String message) {
if (BuildConfig.DEBUG) {
Log.w(tag, message);
}
}

public static void LOGE(final String tag, String message) {
if (BuildConfig.DEBUG) {
Log.e(tag, message);
}
}

}

Then, make log calls to this class:

LogUtils.LOGD(TAG, "MyActivity.onCreate debug message");

Setting Android Log Levels

setprop:

  • is temporary until you reboot your device, even on rooted phones.
  • you can persist properties through reboots if you write them into local.prop which is only possible on rooted phones.
  • some properties are read-only and can only be changed if you change some init files. That might be even impossible on rooted phones.
  • each device (or firmware) can have a different set of properties. A rooted phone wouldn't have automatically more.

Loglevels:

  • If the code that prints the log says Log.d() then it will be on "debug" level and you can't change that unless you change the code and recompile it. There is nothing that hides log messages if you execute a Log.? regardless of level.
  • the Android framework hides some log messages if you have a release build of your firmware. To show those you need to recompile your firmware as debug build. No chance to get those to show on a rooted phone either.
  • some messages are controlled by a local variable in the code like if (LOCAL_LOGV) Log.v(... - you need to change the code here to see those too.
  • some messages are controlled by Config.LOGV (= always false) see Config. No way to change the broken behaviour here either. You need to recompile.
  • some other logmessages are hidden until you enable a property:

example

public static final boolean DEBUG_SQL_CACHE =
Log.isLoggable("SQLiteCompiledSql", Log.VERBOSE);

// somewhere in code
if (SQLiteDebug.DEBUG_SQL_CACHE) {
Log.d(TAG, "secret message!");
}

if you do adb shell setprop log.tag.SQLiteCompiledSql VERBOSE you should see those messages popping up. Log#isLoggable()

There is no global loglevel I know of.

How to disable some Log functions when building an Android app?

You could create your own wrapper for the Log-class, maybe something like this:

static class MyLog{
private static int mLevel;
public final static void setLevel(int level){
mLevel = level;
}
public final static void v(String tag, String message){
if( mLevel > 0 ) return;
Log.v(tag, message);
}
public final static void d(String tag, String message){
if( mLevel > 1 ) return;
Log.d(tag, message);
}
public final static void i(String tag, String message){
if( mLevel > 2 ) return;
Log.d(tag, message);
}
//Same for w and e if neccessary..
}

Then you can use MyLog.setLevel(1); to disable any verbose logging (or a higher level to disable those logs as well).

Should logs be disabled before releasing app?

Logs are essential troubleshooting resources, even for deployed software.

I would limit logging for production releases (perhaps even limit it to "only log in emergencies"). But I would not categorically say "no logging".

Here is a good discussion (with some good guidelines):

  • How do I enable/disable log levels in Android?

PS:
Having said that, I hasten to add:

No, you should not have any "debug" or "informational" logging in a production release.

Which is exactly what the Android documentation, cited in the above links, also says.

How can I disable Android's internal logging for specific tags (e.g. AbsListView, GestureDetector, endeffect)

How can I disable specific log messages which are not produced by my
code?

if you are using Eclipse you can only show logs produced by your application by defining a filter. On the bottom left of the LogCat window click the plus icon to create a new filter. "By Application name" field needs the following format: com.yourpackage.yourapp. After saving this filter you can just click it to see only message related to your app

Decrease ORMlite's internal log verbosity or disable it

With method tracing we saw that LocalLog was being used. As is stated on LocalLog's documentation:

You can set the log level by setting the System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY,
"trace").

Acceptable values are: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL.

We couldn't set the property using adb shell so we added the following line to our Application.onCreate

System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "ERROR");

Finally we stop seeing ORMLite output on logcat and performance increased as expected.



Related Topics



Leave a reply



Submit