【问题标题】:Android writing log to the fileAndroid将日志写入文件
【发布时间】:2012-01-16 19:19:38
【问题描述】:

默认情况下,android 中的 Log 类将日志写入控制台(logcat)。有没有简单的方法来写日志 f.e.在某个文件中?

【问题讨论】:

  • 如果您采取一种简单的方法,即打开文件句柄并在那里写入 - 您将写入 Android 设备上的文件系统,而您需要的可能是开发机器上的文件系统?
  • 您想将 logcat 消息导出到文件还是只在文件中写入日志消息?
  • 我想将 logcat 消息导出到文件中。
  • 这个库可能是你想要的:code.google.com/p/android-log-collector

标签: java android logging


【解决方案1】:

一个简单的Log 类(仅用于开发/测试目的)类似于android.util.Log,但记录到sdcard。仅对现有代码的修改是将import android.util.Log 更改为import com.example.Log。同时添加权限为

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

package com.example.logger;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;  

import android.os.Environment;

public class Log {
    private static final String NEW_LINE =  System.getProperty("line.separator") ; 
    public static boolean mLogcatAppender = true;
    final static File mLogFile;

    static {
        mLogFile = new File( Environment.getExternalStorageDirectory(),  "logs.log" ); 
        if ( !mLogFile.exists() ) {
            try {
                mLogFile.createNewFile();
            }
            catch (final IOException e) {
                e.printStackTrace();
            }
        }
        logDeviceInfo();
    }

    public static void i( String TAG, String message ){
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.i( TAG, message );
        }
    }

    public static void d( String TAG, String message ){
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.d( TAG, message );
        }
    }

    public static void e( String TAG, String message ){
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.e( TAG, message );
        }
    }

    public static void v(String TAG, String message ){
        appendLog(TAG + " : " + message);
        if ( mLogcatAppender ) {
            android.util.Log.v( TAG, message );
        }
    }

    public static void w( String TAG, String message ) {
        appendLog( TAG + " : " + message );
        if ( mLogcatAppender ) {
            android.util.Log.w( TAG, message ); 
        }
    }

    private static synchronized void appendLog( String text ) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        try {
            final FileWriter fileOut = new FileWriter( mLogFile, true );
            fileOut.append( sdf.format(new Date()) + " : " + text + NEW_LINE ); 
            fileOut.close();
        }
        catch ( final IOException e ) {
            e.printStackTrace();
        }
    }

    private static void logDeviceInfo() {
        appendLog("Model : " + android.os.Build.MODEL);
        appendLog("Brand : " + android.os.Build.BRAND);
        appendLog("Product : " + android.os.Build.PRODUCT);
        appendLog("Device : " + android.os.Build.DEVICE);
        appendLog("Codename : " + android.os.Build.VERSION.CODENAME);
        appendLog("Release : " + android.os.Build.VERSION.RELEASE);
    }

}

【讨论】:

    【解决方案2】:

    如果这只是出于开发目的,您可以在设备上调用 logcat 并将其输出重定向到文件中。例如:adb shell logcat > log.txt

    或者,您可以尝试将stdoutstderr 重定向到一个文件,iirc 这会起作用,但我没有带我的手机来测试它。

    但是,创建自己的基本日志记录类会更容易,它与内置日志类执行相同的操作,但也保存到文件中。

    【讨论】:

      【解决方案3】:

      在 Eclipse ddms 中有一个选项可以将日志保存到文件中。

      【讨论】:

        【解决方案4】:

        使用 FileInputStream 将所有错误消息重定向到文件

        【讨论】:

        • 他会怎么做呢?能否提供一些链接或示例代码?
        猜你喜欢
        • 2020-06-27
        • 2013-01-20
        • 2010-12-17
        • 2012-04-06
        • 2023-03-31
        • 2015-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多