How do I enable/disable log levels in Android?

后端 未结 18 2013
无人共我
无人共我 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:15

    The easiest way is probably to run your compiled JAR through ProGuard before deployment, with a config like:

    -assumenosideeffects class android.util.Log {
        public static int v(...);
    }
    

    That will — aside from all the other ProGuard optimisations — remove any verbose log statements directly from the bytecode.

    0 讨论(0)
  • 2020-11-22 17:15

    Log4j or slf4j can also be used as logging frameworks in Android together with logcat. See the project android-logging-log4j or log4j support in android

    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 17:18

    For me it is often useful being able to set different log levels for each TAG.

    I am using this very simple wrapper class:

    public class Log2 {
    
        public enum LogLevels {
            VERBOSE(android.util.Log.VERBOSE), DEBUG(android.util.Log.DEBUG), INFO(android.util.Log.INFO), WARN(
                    android.util.Log.WARN), ERROR(android.util.Log.ERROR);
    
            int level;
    
            private LogLevels(int logLevel) {
                level = logLevel;
            }
    
            public int getLevel() {
                return level;
            }
        };
    
        static private HashMap<String, Integer> logLevels = new HashMap<String, Integer>();
    
        public static void setLogLevel(String tag, LogLevels level) {
            logLevels.put(tag, level.getLevel());
        }
    
        public static int v(String tag, String msg) {
            return Log2.v(tag, msg, null);
        }
    
        public static int v(String tag, String msg, Throwable tr) {
            if (logLevels.containsKey(tag)) {
                if (logLevels.get(tag) > android.util.Log.VERBOSE) {
                    return -1;
                }
            }
            return Log.v(tag, msg, tr);
        }
    
        public static int d(String tag, String msg) {
            return Log2.d(tag, msg, null);
        }
    
        public static int d(String tag, String msg, Throwable tr) {
            if (logLevels.containsKey(tag)) {
                if (logLevels.get(tag) > android.util.Log.DEBUG) {
                    return -1;
                }
            }
            return Log.d(tag, msg);
        }
    
        public static int i(String tag, String msg) {
            return Log2.i(tag, msg, null);
        }
    
        public static int i(String tag, String msg, Throwable tr) {
            if (logLevels.containsKey(tag)) {
                if (logLevels.get(tag) > android.util.Log.INFO) {
                    return -1;
                }
            }
            return Log.i(tag, msg);
        }
    
        public static int w(String tag, String msg) {
            return Log2.w(tag, msg, null);
        }
    
        public static int w(String tag, String msg, Throwable tr) {
            if (logLevels.containsKey(tag)) {
                if (logLevels.get(tag) > android.util.Log.WARN) {
                    return -1;
                }
            }
            return Log.w(tag, msg, tr);
        }
    
        public static int e(String tag, String msg) {
            return Log2.e(tag, msg, null);
        }
    
        public static int e(String tag, String msg, Throwable tr) {
            if (logLevels.containsKey(tag)) {
                if (logLevels.get(tag) > android.util.Log.ERROR) {
                    return -1;
                }
            }
            return Log.e(tag, msg, tr);
        }
    
    }
    

    Now just set the log level per TAG at the beginning of each class:

    Log2.setLogLevel(TAG, LogLevels.INFO);
    
    0 讨论(0)
  • 2020-11-22 17:19

    There is a tiny drop-in replacement for the standard android Log class - https://github.com/zserge/log

    Basically all you have to do is to replace imports from android.util.Log to trikita.log.Log. Then in your Application.onCreate() or in some static initalizer check for the BuilConfig.DEBUG or any other flag and use Log.level(Log.D) or Log.level(Log.E) to change the minimal log level. You can use Log.useLog(false) to disable logging at all.

    0 讨论(0)
  • 2020-11-22 17:20

    I took a simple route - creating a wrapper class that also makes use of variable parameter lists.

     public class Log{
            public static int LEVEL = android.util.Log.WARN;
    
    
        static public void d(String tag, String msgFormat, Object...args)
        {
            if (LEVEL<=android.util.Log.DEBUG)
            {
                android.util.Log.d(tag, String.format(msgFormat, args));
            }
        }
    
        static public void d(String tag, Throwable t, String msgFormat, Object...args)
        {
            if (LEVEL<=android.util.Log.DEBUG)
            {
                android.util.Log.d(tag, String.format(msgFormat, args), t);
            }
        }
    
        //...other level logging functions snipped
    
    0 讨论(0)
提交回复
热议问题