Android日志包装器易于禁用并避免使用StringBuilder

时间:2016-09-16 13:31:09

标签: android android-log

在看到很多关于如何在发布时禁用Log.i / e / d调用的问题和答案后,答案会有所不同,要么使用proguard,要么使用日志包装来检查bool值。我实际上已经使用了一个日志包装器很长一段时间但是注意到在某些答案上有创建StringBuilder的实例,即使最终没有显示日志。

所以,我正在尝试以下方法:

public class Logger {

    static final boolean LOG = true;

    public static void printString(String string) {
        if (Logger.LOG) {
            Log.i("MyApp", string);
        }
    }

    public static void printStrings(String string1, String string2) {
        if (Logger.LOG) {
            Log.i("MyApp", string1 + " " + string2);
        }
    }

    public static void printObject(Object object) {
        if (Logger.LOG) {
            Log.i("MyApp", object.toString());
        }
    }

    public static void printException(Exception exception) {
        if (Logger.LOG) {
            Log.i("MyApp", exception.getLocalizedMessage());
        }
    }
}

所以我的问题是,这个实现是否有`StringBuilder问题?我意识到这仅限于记录的事物的数量,但可以扩展。我的主要范围是能够轻松禁用整个应用程序中的日志消息或对其进行更多控制。

0 个答案:

没有答案
相关问题