您可以像这样包装 android.utils.Log 以获得可扩展性:
public class Log {
// Constants
//--------------------------------------------------------------------------
private static final String TAG = Log.class.getName();
public static final int VERBOSE = android.util.Log.VERBOSE;
public static final int DEBUG = android.util.Log.DEBUG;
public static final int INFO = android.util.Log.INFO;
public static final int WARN = android.util.Log.WARN;
public static final int ERROR = android.util.Log.ERROR;
public static final int ASSERT = android.util.Log.ASSERT;
// Extension interface
//--------------------------------------------------------------------------
public interface LogExtension {
void onLog(int priority, String tag, String msg, Throwable tr);
}
private static ConcurrentHashMap<String, LogExtension> logExtensions = new ConcurrentHashMap<String, LogExtension>();
public static void addExtension(String tag, LogExtension extension) {
if (tag != null && extension != null) {
Log.logExtensions.put(tag, extension);
}
}
public static void removeExtension(String tag) {
Log.logExtensions.remove(tag);
}
// android.util.Log wrapper
//--------------------------------------------------------------------------
public static int d(String tag, String msg) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(DEBUG, tag, msg, null);
}
return android.util.Log.d(tag, msg);
}
public static int d(String tag, String msg, Throwable tr){
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(DEBUG, tag, msg, tr);
}
return android.util.Log.d(tag, msg, tr);
}
public static int e(String tag, String msg){
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(ERROR, tag, msg, null);
}
return android.util.Log.e(tag, msg);
}
public static int e(String tag, String msg, Throwable tr) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(ERROR, tag, msg, tr);
}
return android.util.Log.e(tag, msg, tr);
}
public static String getStackTraceString(Throwable tr) {
return android.util.Log.getStackTraceString(tr);
}
public static int i(String tag, String msg) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(INFO, tag, msg, null);
}
return android.util.Log.i(tag, msg);
}
public static int i(String tag, String msg, Throwable tr) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(INFO, tag, msg, tr);
}
return android.util.Log.i(tag, msg, tr);
}
public static boolean isLoggable(String tag, int level) {
return android.util.Log.isLoggable(tag, level);
}
public static int println(int priority, String tag, String msg) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(priority, tag, msg, null);
}
return android.util.Log.println(priority, tag, msg);
}
public static int v(String tag, String msg) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(VERBOSE, tag, msg, null);
}
return android.util.Log.v(tag, msg);
}
public static int v(String tag, String msg, Throwable tr) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(VERBOSE, tag, msg, tr);
}
return android.util.Log.v(tag, msg, tr);
}
public static int w(String tag, Throwable tr) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(WARN, tag, null, tr);
}
return android.util.Log.w(tag, tr);
}
public static int w(String tag, String msg, Throwable tr) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(WARN, tag, msg, tr);
}
return android.util.Log.w(tag, msg, tr);
}
public static int w(String tag, String msg) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(WARN, tag, msg, null);
}
return android.util.Log.w(tag, msg);
}
public static int wtf(String tag, Throwable tr) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(ASSERT, tag, null, tr);
}
return android.util.Log.wtf(tag, tr);
}
public static int wtf(String tag, String msg) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(ASSERT, tag, msg, null);
}
return android.util.Log.wtf(tag, msg);
}
public static int wtf(String tag, String msg, Throwable tr) {
for (LogExtension extension : Log.logExtensions.values()) {
extension.onLog(ASSERT, tag, msg, tr);
}
return android.util.Log.wtf(tag, msg, tr);
}
// Convenience
//--------------------------------------------------------------------------
public static String acronymForPriority(int priority) {
switch (priority) {
case VERBOSE: {
return "V";
}
case DEBUG: {
return "D";
}
case INFO: {
return "I";
}
case WARN: {
return "W";
}
case ERROR: {
return "E";
}
case ASSERT: {
return "WTF";
}
default: {
return "?";
}
}
}
}
然后使用此 Wrapper 的完全限定名称查找/替换 android.util.Log。LogExtension
您可以通过实现接口来简单地扩展 Logger :
public class LogCatBuffer implements Log.LogExtension{
// Attributes
//--------------------------------------------------------------------------
private int linesOfLog;
private Queue<String> logCatContents;
private DateFormat dateFormatter = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
// Construction
//--------------------------------------------------------------------------
private LogCatBuffer(int linesOfLog) {
this.linesOfLog = linesOfLog;
this.logCatContents = new ConcurrentLinkedQueue<String>();
}
// LogExtension implementation
//--------------------------------------------------------------------------
@Override
public void onLog(int priority, String tag, String msg, Throwable tr) {
Date now = new Date();
String loggingMessage = this.dateFormatter.format(now) +
" " +
android.os.Process.myPid() +
" " +
Log.acronymForPriority(priority) +
"/" +
tag +
": " +
(msg != null ? msg : "") +
(msg != null && tr != null ? "\n" : "") +
(tr != null ? Log.getStackTraceString(tr) : "");
this.logCatContents.add(loggingMessage);
if (this.logCatContents.size() > this.linesOfLog) {
this.logCatContents.poll();
}
}
// Export
//--------------------------------------------------------------------------
@Override
public String toString() {
StringBuilder builder = new StringBuilder(256 * this.linesOfLog);
for (String line : this.logCatContents) {
builder.append(line).append("\n");
}
return builder.toString();
}
}
然后,您可以将LogCatBuffer.toString()
输出转储到一个文本文件中,该文件看起来非常类似于您在 LogCat 控制台中使用的内容。