5

Why and when should I use the android Logging? Should it be used only for debugging purposes? It seems to me that if kept in a production application, it would slow it down considerably.

One key part of my question, is WHEN it should be used...in what cases should I need it? Are there any 'best practices' for using this?

This may be a very stupid or amateur question, but I've never understood the need or felt compelled to use it. I may be missing something.

http://developer.android.com/reference/android/util/Log.html

Also - I already know that logging for errors/verbose/info/etc are different, and some are discarded when not debugging. Please don't reiterate the information that's in the class overview without giving me an explanation why. Thanks!

Cody
  • 8,686
  • 18
  • 71
  • 126
  • 1
    On the publishing pages from the Android site, it says you should comment/remove all logging - so it wouldn't be used (or shouldn't at least) in production. – Ricky Dec 06 '11 at 10:49
  • 1
    It's fun to see how much logging are NOT turned off in the production phones. Very, very funny messages sometimes get logged in and those messages always give me hope that I'm maybe not the very worst Android programmer ever. – Alexander Kulyakhtin Dec 06 '11 at 11:48
  • Not a stupid question at all. Many people use it in their code (eg. in online tutorials etc.) and never mention its significance. – Flame of udun Nov 11 '15 at 22:13

3 Answers3

4

I agree with you, I never really used it either. I much prefer debugging over log reading (and unit-testing over debugging), when trying to figure out what's happening and getting an error.

Some people argue it can be useful to log "additional details" when your application crashes and get an exception, but I usually reply to them that the exception itself should hold that additional details when necessary, and expose them in its getMessage. Thus, the default stack trace that's included in the LogCat is more than enough to see what's going on.

But anyway, it's always nice to have the possibility to log something, even though I haven't found it really useful so far, you never know when it might help :)

Guillaume
  • 22,694
  • 14
  • 56
  • 70
0

You usually debug only when you know there is something wrong. (And when you know, you might write additional test cases.)

With logging (at the INFO level, for example), you can add some additional information to trace the data in the app. This allows you to find out that there is something wrong. Thus, it adds a higher-level overview.

Additionally, it can be easily disabled, does not slow the app down significantly (if done right), and might thus offer another avenue of approach to see if everything works correctly, with few disadvantages and some advantages. (See also Logging vs. Debugging, especially the links in @smonff's answer.)

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
0

Regarding my comment, see Preparing for Release. (Showing logging should be removed before release, hence not being used in production).

Turn off logging and debugging

Make sure you deactivate logging and disable the debugging option before you build your
application for release. You can deactivate logging by removing calls to Log methods
in your source files.

I used logging the other day, I fired off another thread to do some work but I needed to check some data being produced in the thread, without logging or displaying Toast's, how could I get the values? I'm tried debugging/stepping through code in Eclipse before and just run into several problems.

With logging, I can log each value, view the logcat and know exactly what my code is doing.

Ricky
  • 7,785
  • 2
  • 34
  • 46
  • The "several problems" you ran into while debugging could and should have been worked around: debugging is usually a far more efficient way than logging to know what's going on under the hood - even though it's sometimes a pain to get it correctly in multi-threaded environments. And in an ideal world, you need neither logging nor debugging, if your unit-tests coverage is good enough. – Guillaume Dec 06 '11 at 11:10
  • Point taken, but please explain: "debugging is usually a far more efficient way than logging to know what's going on under the hood", why is this? – Ricky Dec 06 '11 at 11:12
  • Well, for many different reasons: if you carefully go step by step, you can see exactly which path your thread is following (which can be quite difficult using plain logging), and when you step through, you can then look in details at any variable state, including following dependencies, or evaluate any expression at runtime, which is invaluable to check state of a specific instance, etc. – Guillaume Dec 06 '11 at 11:19
  • Also another nice thing you can have is conditional breakpoints: for example, you can run in debug mode and say "stop whenever a RuntimeException is thrown", and you'll see the location where it's thrown, the state of the variables, etc. – Guillaume Dec 06 '11 at 11:23