在运行android应用程序时将消息放入日志文件

时间:2014-10-23 06:41:57

标签: android

我有一个申请。 我想在启动我的Android应用程序时在SD卡中创建一个日志文件,并希望在该文件中写入错误,警告,信息。如何才能完成?如果有人知道请帮助我..

1 个答案:

答案 0 :(得分:0)

我是这样做的: 您可以将此代码放入开始活动或应用程序类

final String filePath = mediaFileService.getExternalCrashDir()
            + "LOG" + new Date().toString() + ".txt";
Runtime.getRuntime().exec(new String[]{
                "logcat", "-f", filePath, "ActivityManager:I", "LOG" + ":V",
                "*:S"
        });

制作特定的日志类并将其用于记录

public class L {

interface Logger {
    public int print(String tag, String msg);
}

private static final int MAX_STRING_SIZE = 2000;
private static final Logger verboseLogger = new Logger() {
    @Override
    public int print(final String tag, final String msg) {
        return Log.v(tag, msg);
    }
};
private static final Logger debugLogger = new Logger() {
    @Override
    public int print(final String tag, final String msg) {
        return Log.d(tag, msg);
    }
};
private static final Logger infoLogger = new Logger() {
    @Override
    public int print(final String tag, final String msg) {
        return Log.i(tag, msg);
    }
};
private static final Logger warnLogger = new Logger() {
    @Override
    public int print(final String tag, final String msg) {
        return Log.w(tag, msg);
    }
};
private static final Logger errorLogger = new Logger() {
    @Override
    public int print(final String tag, final String msg) {
        return Log.w(tag, msg);
    }
};

private static void printWithLogger(String s, Logger l) {
    if (s.length() > MAX_STRING_SIZE || s.contains("\n")) {
        long stringId = Math.abs(UUID.randomUUID().getMostSignificantBits());
        ArrayList<String> lines = new ArrayList<String>();
        for (String innerLine : s.split("\n")) {
            int aCursor = 0;
            while (innerLine.length() - aCursor >= MAX_STRING_SIZE) {
                lines.add(innerLine.substring(aCursor, aCursor + MAX_STRING_SIZE));
                aCursor += MAX_STRING_SIZE;
            }
            lines.add(innerLine.substring(aCursor));
        }
        int totalPartsCount = lines.size();
        for (int i = 1; i <= lines.size(); i++) {
            l.print("LOG", String.format("LongString_%s[%d/%d] %s", stringId, i, totalPartsCount,
                    lines.get(i - 1)));
        }
    } else {
        l.print("LOG", s);
    }
}

private static String format(String tag, String msg) {
    if (tag != null && tag.length() > 0) {
        return tag + " " + msg;
    } else {
        return msg;
    }
}

public static void v(String tag, String msg) {
    printWithLogger(format(tag, msg), verboseLogger);
}

public static void v(String tag, String msg, Throwable tr) {
    printWithLogger(format(tag, msg), verboseLogger);
    printWithLogger(Log.getStackTraceString(tr), verboseLogger);
}

public static void d(String tag, String msg) {
    printWithLogger(format(tag, msg), debugLogger);
}

public static void d(String tag, String msg, Throwable tr) {
    printWithLogger(format(tag, msg), debugLogger);
    printWithLogger(Log.getStackTraceString(tr), debugLogger);
}

public static void i(String tag, String msg) {
    printWithLogger(format(tag, msg), infoLogger);
}

public static void i(String tag, String msg, Throwable tr) {
    printWithLogger(format(tag, msg), infoLogger);
    printWithLogger(Log.getStackTraceString(tr), infoLogger);
}

public static void w(String tag, String msg) {
    printWithLogger(format(tag, msg), warnLogger);
}

public static void w(String tag, String msg, Throwable tr) {
    printWithLogger(format(tag, msg), warnLogger);
    printWithLogger(Log.getStackTraceString(tr), warnLogger);
}

public static void w(String tag, Throwable tr) {
    printWithLogger(Log.getStackTraceString(tr), warnLogger);
}

public static void e(String tag, String msg) {
    printWithLogger(format(tag, msg), errorLogger);
}

public static void e(String tag, String msg, Throwable tr) {
    printWithLogger(format(tag, msg), errorLogger);
    printWithLogger(Log.getStackTraceString(tr), errorLogger);
}

}