How do I enable/disable log levels in Android?

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

    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 logLevels = new HashMap();
    
        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);
    

提交回复
热议问题