Is there a way to somehow intercept calls to the standard Log in android and perform some other action?
In desktop Java one would usually get some logger so there are ways to install a different logging handler / implementation. However, Android seems to have a static call to Log, and I could not find any information about replacing the default behavior.
I realize I can monitor the device log through ADB, but I'd like to have a different behavior in running apps on a device who opt in (e.g., by executing a certain instruction when the program starts out).
Because
Log
is final, I think you will have to write your own logging system and implement that if you want to change anything.As AedonEtLIRA points out Log is final so you need to roll/borrow a new one. I've been using Prasanta Paul's "MLog": http://prasanta-paul.blogspot.com/2010/07/custom-logger-for-android.html
It makes it easy to save logs to a text file and/or disable logging completely. If it doesn't already do what you need, it is a good base for managing logging.
In addition to MLog proposed by @Darrell, there are several other logging frameworks for Android, including:
Of the three, logback (the next-gen log4j) seems to be the most capable and have the richest API (and lots of documentation), while microlog is the most compact (but limited in features...but maybe it does all you need). All three support the SLF4J logging facade, so you can easily swap out microlog for logback, log4j, jul, or some newer framework down the road.
As with MLog, these frameworks require replacing the calls to
android.os.util.Log
(it doesn't "intercept" the calls as you might be after).I think your best solution would be to replace all the
Log
calls in your application with your own class,MyLog
, then callLog
if they don't opt-in, and call your special logging feature if they opt-in.Replacing all the Log calls shouldn't be hard, just a find and replace.