Android项目之日志管理

只有在debug下才打印日志

public class Logger {

    private String tag;

    public Logger(String tag) {
        super();
        this.tag = tag;
    }

    public Logger(Object object) {
        this(object.getClass());
    }

    public Logger(Class clazz) {
        this(clazz.getSimpleName());
    }

    public void d(String format, Object... args) {
        if (BuildConfig.DEBUG)
            Log.d(tag, String.format(format, args));
    }

    public void e(String format, Object... args) {
        if (BuildConfig.DEBUG)
            Log.e(tag, String.format(format, args));
    }

    public void e(Throwable throwable, String format, Object... args) {
        if (BuildConfig.DEBUG)
            Log.e(tag, String.format(format, args), throwable);
    }

    public void i(String format, Object... args) {
        if (BuildConfig.DEBUG) {
            String msg = String.format(format, args);
            // 日志太长 分段打印
            int count = msg.length() / 3600 + 1;
            for (int i = 0; i < count; i++) {
                int index = i * 3600;
                if (i == count - 1) {
                    if (i == 0) {
                        Log.i(tag, msg);
                    } else {
                        Log.i(tag, msg.substring(index));
                    }
                } else {
                    Log.i(tag, msg.substring(index, index + 3600));
                }
            }

        }
    }

    public void w(String format, Object... args) {
        if (BuildConfig.DEBUG)
            Log.w(tag, String.format(format, args));
    }
}

你可能感兴趣的:(Android项目)