Android logging explained (differences between classic java logger and android logging)

Публикувано / posted 2010-08-09 в категория / in category: Android
  

If you are used to "classic" Java logger you may be unpleasantly surprised when you try to use Android's logging mechanism.

In classic Java usually you do things like:

Logger myLogger = Logger.getLogger("com.somedomain.someapp");
logger.setLevel(Level.FINE);

… and then you log with:

logger.warning("some warning");
or
logger.fine("some message");
etc.
At first glance things are the same in Android, i.e. you have android.util.Log, you have methods like Log.i("some message"); but there are two important differences:
1. You can't set log level at runtime. You must use:
adb shell setprop log.tag.<YOUR_LOG_TAG> <LEVEL>
Don't try to use System.setProperty("log.tag. …", …) -- it is useless.
2. As stated in Log docs, "Before you make any calls to a logging method you should check to see if your tag should be logged", this means -- android will not filter logs for you (level wise). You have to use something like:
if (Log.isLoggable(tag, Log.INFO)) {
 
Log.i(tag, msg);
 
}
i.e. you should take care for log level filtering. Luckily it is not difficult to create wrapper class around Log in which you overwrite all logging methods using checks like in the above code.

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Внимание: Моля, въведете само ПЪРВИТЕ ТРИ цифри от картинката
Important: Please enter just the first three digits from the image