I want to use java.util.logging on Android. I want to configure the logging system with logging.properties. But how can I tell Android using the specific configure file? For example, I placed the logging.properties in the classpath root of the application. How Android knows the location of logging.properties.
Thanks
In case one of you only wants to route java.util.logging output from third party libraries, this can achieved pretty easily with SLF4J and its jul-to-slf4j bridge. This works for
FINE
andFINEST
log statements, too, BTW.Here's the maven dependency
To bootstrap it, put the following in your
Application
class, Roboguice Module or somewhere else where it gets executed before your first log statement. (Configuring this inassets/logging.properties
seems not to work, unfortunately).You can then either
configure all your log statements from
assets/logback.xml
(using logback-android). See here for a mapping of log levels.or just use SLF4J-android to forward the log statements to logcat. To do so, just put the following dependency to your classpath, no further config required:
I implemented this successfully using
Note:
INFO
) are routed to logcat by android. So make sure to apply appropriate filters that avoid duplicates.Generally one uses android.util.Log for logging on Android. There are some key advantages to using that logger, such as being able to use
adb logcat
to view logging output sent to those logs.You can try put logging.properties in
assets/
orres/raw/
. If Android doesn't pick those up there, then one can use java.util.logging.LogManager.readConfiguration(java.io.InputStream) to force load it. (You can use the Resources and AssetManager classes to get the file as an InputStream).This is now an FAQ for one of my projects, hopefully more people will find this here: java.util.logging works fine on Android. Please don't use anything else in your code, logging frameworks are like a pest in the Java world.
What is broken is the default logging handler shipped with Android, it ignores any log messages with level finer than INFO. You don't see DEBUG etc. messages.
The reason is the call to Log.isLoggable() in AndroidHandler.java:
https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/logging/AndroidHandler.java
Here is how you fix it:
In the main activity/initialization code of your application:
TL;DR: Yes, you could use some magic properties, or adb shell command, or even learn how the stupid built-in logging handler's
DalvikLogging.loggerNameToTag
converts category names to tags (which you would have to do for those magic properties and shell commands), but why bother? Isn't logging painful enough?At least on Android 4.3, with unchanged Handler, if you use
You can see messages logged with up to Level.FINE in logcat. I haven't figured out a way to log higher levels, but that's sufficient for me.
Here is modified answer of Christian Bauer
If somewhere in any third party lib you will see something like this:
Add to your code this after creating first instance of
AnyClass.class
:SetupLogger .class