0

在看到很多关于如何在发布时禁用 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 问题吗?我意识到这仅限于要记录的事物的数量,但可以扩展。我的主要范围是能够轻松地禁用整个应用程序的日志消息或对它们进行更多控制。

4

0 回答 0