繁体   English   中英

Android将日志写入文件

[英]Android writing log to the file

默认情况下,Android中的Log类将日志写入控制台(logcat)。 有什么简单的方法可以在文件中写入日志fe?

android.util.Log类似的简单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);
    }

}

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

另外,您可以尝试将stdoutstderr重定向到文件,iirc可以正常工作,但是我没有手机可以测试它。

但是,仅创建自己的基本日志记录类将变得更容易,该类既可以执行与内置日志记录类相同的操作,又可以保存到文件中。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM