How do I enable/disable log levels in Android?

后端 未结 18 2169
无人共我
无人共我 2020-11-22 16:42

I am having lots of logging statements to debug for example.

Log.v(TAG, \"Message here\");
Log.w(TAG, \" WARNING HERE\");

while deploying t

18条回答
  •  误落风尘
    2020-11-22 17:18

    Stripping out the logging with proguard (see answer from @Christopher ) was easy and fast, but it caused stack traces from production to mismatch the source if there was any debug logging in the file.

    Instead, here's a technique that uses different logging levels in development vs. production, assuming that proguard is used only in production. It recognizes production by seeing if proguard has renamed a given class name (in the example, I use "com.foo.Bar"--you would replace this with a fully-qualified class name that you know will be renamed by proguard).

    This technique makes use of commons logging.

    private void initLogging() {
        Level level = Level.WARNING;
        try {
            // in production, the shrinker/obfuscator proguard will change the
            // name of this class (and many others) so in development, this
            // class WILL exist as named, and we will have debug level
            Class.forName("com.foo.Bar");
            level = Level.FINE;
        } catch (Throwable t) {
            // no problem, we are in production mode
        }
        Handler[] handlers = Logger.getLogger("").getHandlers();
        for (Handler handler : handlers) {
            Log.d("log init", "handler: " + handler.getClass().getName());
            handler.setLevel(level);
        }
    }
    

提交回复
热议问题