Android Log.V(), Log.D(), Log.I(), Log.W(), Log.E() - When to Use Each One

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

Let's go in reverse order:

  • 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.

And as a bonus...

  • Log.wtf: Use this when stuff goes absolutely, horribly, holy-crap wrong. You know those catch blocks where you're catching errors that you never should get...yeah, if you wanna log them use Log.wtf

Using Log.d() or Log.e() in my code

Consider using this : isLoggable()

Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: setprop log.tag.<YOUR_LOG_TAG> <LEVEL> Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: log.tag.<YOUR_LOG_TAG>=<LEVEL> and place that in /data/local.prop

Personally for releases, I'd remove the debug logs and keep the error logs using Proguard.


It is better to wrap it like so:

public class MyLog {

public static void d(String tag, String msg) {
if (Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, msg);
}
}

public static void i(String tag, String msg) {
if (Log.isLoggable(tag, Log.INFO)) {
Log.i(tag, msg);
}
} // and so on...

You can set logging level by issuing an adb command

I don't know when I should use log.d() and log.w() methods in Android

Why did this particular code use one over the other? Because the person who wrote chose to. They could have used i (info) and e (error) instead.

The short answer is use whatever you want because it ultimately doesn't matter: at the end of the day your message goes into the LogCat output stream that you can look at.

The longer answer is that you want to log to a particular "stream" based on the type of information you're logging. You have your choice of 5 main "streams" of logging, listed here in order of how frequently you might expect to use them, from least to most: Error, Warn, Info, Debug, and Verbose.

Again, you can log to whichever your heart desires but generally speaking:

  1. Log to ERROR (Log.e()) when you detect a valid error state or exception that actually prevents your app from functioning correctly. For example, if you think you're handling every case of a switch statement, you might add an error log in the default case which shouldn't happen. These are cases that you as the developer want to know about and fix, even if they are not crashing your app. You might report these as "non-fatal" exceptions in Crashlytics.

  2. Log to WARN (Log.w()) for unexpected but non-critical errors that occur. These are to draw attention that something went wrong, but that the app continued as best as it could. In your posted example, the "failure listener" might be using WARN because that failure is a valid case to handle and you want to be aware of failures, but it's known that failure is possible so the app should handle it gracefully and be able to continue.

  3. Log to INFO (Log.i()) for information you would find generally always find useful but that doesn't get too noisy. These would be things that would help you track down bugs in a crash report. Logging Activity lifecycle events would be an example.

  4. Log to DEBUG (Log.d()) for information you would find temporarily helpful while in development to help you solve a particular problem or while trying to track down a specific bug. For example, if your app is crashing at a specific location, you might add a Log.d call just before that spot and log the state of the local variables or something to help you find out what's causing the crash. You would probably remove these once that issue or bug is resolved.

  5. Log to VERBOSE (Log.v()) when you need even more information even more frequently then you can get with DEBUG. As its name implies, this is intended to be verbose. It will spew a lot of text into LogCat making it so noisy as to be unusable. For example, you might log every iteration of a long loop to VERBOSE.

Once you're logging to any particular stream, you can filter the LogCat logs to aid in finding particular messages. For example, if something is going wrong with a library you're using in your app, you might filter to WARN to narrow the size of the LogCat logs and look for any warnings the library may have reported.

You filter in Android Studio by selecting the drop down in the LogCat tab. Android Studio also colors each stream (which you can configure in settings) to aid in differentiating the log messages).

Sample Image

Hope that helps!

What the ios equivalent of android Log.v, Log.d, Log.i, Log.e, etc?

Swift:

print("")

Objectve-C

NSLog(@"")

You can view the logs in the lower bottom corner of xcode. Refer the image below.
Sample Image

What is the meaning of alphabets in android studio logcat?

these alphabet is for various log options:

see this link : Log option

A is for ASSERT

D is for DEBUG

E is for ERROR

I is for INFO

V is for VERBOSE

W is for WARN

you can filter log message by above option by clicking here so you can see that particular log only:

Select log option

Why are Log.d() and Log.v() not printing

Android Studio filters lines that have already been logged but Log itself may filter some levels when logging. See Log.isLoggable:

The default level of any tag is set to INFO.

(However on many phone it is actually set to DEBUG or VERBOSE.)

Kotlin Log.i and Log.e errors

The Log methods in android.util.Log take 2 String parameters (and optionally a Throwable as a third param) - you should write it like this:

Log.i("banane", "Coucou")



Related Topics



Leave a reply



Submit