Is there a way to replace the standard Log in Andr

2019-04-29 00:40发布

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

4条回答
混吃等死
2楼-- · 2019-04-29 01:15

Because Log is final, I think you will have to write your own logging system and implement that if you want to change anything.

查看更多
放荡不羁爱自由
3楼-- · 2019-04-29 01:19

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.

查看更多
放我归山
4楼-- · 2019-04-29 01:33

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

查看更多
成全新的幸福
5楼-- · 2019-04-29 01:34

I think your best solution would be to replace all the Log calls in your application with your own class, MyLog, then call Log 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.

查看更多
登录 后发表回答