1

对于我的应用程序,我想创建一个可以将数据记录到文件中的类。在我的应用程序中,如果他们遇到问题,可以给我发送电子邮件,并且此文件中的数据可能会帮助我剖析问题。首先,我是不是走错了路?有没有更好的方法来记录发生的异常?

问题是如果我尝试使用另一个类的 log 方法:

Logger.log(0,"","","");

如果尚未创建文件,则无法找到该文件或确实创建该文件。代码附在下面。

package com.example.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;

public class Logger extends Activity {

final static String FileName = "Log";
static FileOutputStream fos;
static FileInputStream fis;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("logger", "oncreate");
    try {
        File f = getFileStreamPath(FileName);
        if (!f.exists()) {
            Log.d("logger", "file doesn't exist");
            fos = openFileOutput(FileName, Context.MODE_APPEND);
            fos.write(("Created on " + Build.TIME + "\nDevice name: "
                    + Build.MODEL + " \nAndroid Version" + Build.VERSION.SDK_INT)
                    .getBytes());
            fos.close();
        }
        fos = openFileOutput(FileName, Context.MODE_APPEND);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public static void log(int type, String TAG, String msg) {
    Log.d("logger", "file being written");
    Log.println(type, TAG, msg);
    Time now = new Time();
    now.setToNow();
    try {
        fos = new FileOutputStream(FileName);
        fos.write(("\n" + now.toString() + " " + type + " " + TAG + " " + msg).getBytes());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}
4

3 回答 3

2

您是否有理由不能使用像 log4j 或 slf4j 这样的标准日志库?我认为这些可以满足您的需要,而无需自己编写 Logger 类。看起来 Android 上的 slf4j 仍处于测试阶段,但传闻表明人们已经成功地使用了它。

于 2012-12-20T20:12:08.773 回答
1

这是我自己的应用程序的代码片段:

public static void writeLogToFile(String totalFilePath, String myError) {
    FileWriter fWriter;
try {

    fWriter = new FileWriter(totalFilePath, true);
    fWriter.append("\n");
    fWriter.append("{"
            + android.text.format.DateFormat.format(
                    "yyyy-MM-dd kk:mm:ss", new java.util.Date()) + "} ");
    fWriter.append(myError);
    fWriter.flush();
    fWriter.close();

} catch (IOException e) {
    CreateLog.addToLog(e.toString());
}




public static void checkExists(String totalPath, String fileName) {
    File path = new File(totalPath);
    if (!(path.exists())) {
        // Folder does not exists
            createFolder(totalPath);

    }

    File myFile = new File(totalPath + fileName);
    if (!(myFile.exists())) {
        // File does not exists
        writeToFile(totalPath, fileName, "%TEMP%");
    }       
}
于 2012-12-20T20:11:40.077 回答
0

文件名不足以写入文件。与使用 openFileOutput 时不同,使用 FileOutputStream 时需要提供目录。因此,如果您更改此行:

final static String FileName = "Log";

final static String FileName = "data/data/com.example.test/files/Log";

这将解决问题。最后,在访问类中的方法时不会调用 oncreate。您必须将两者结合起来,如下所示:

final static String FileName = "data/data/com.example.test/files/Log";
static FileOutputStream fos;

public static void log(int priority, String tag, String msg) {
    Log.println(priority, tag, msg);
    try {
        Time now = new Time();
        now.setToNow();
        File f = new File(FileName);
        if (!f.exists()) {
            Log.i("Logger", "Creating Log file");
            fos = new FileOutputStream(f, true);
            fos.write(("Created on " + now.toString().subSequence(0, 15)
                    + "\nDevice name: " + Build.MODEL
                    + " \nAndroid Version " + Build.VERSION.SDK_INT)
                    .getBytes());
            fos.close();
        }
        fos = new FileOutputStream(f, true);
        Log.d("file", now.toString());
        fos.write(("\n" + now.toString().substring(4, 15) + " " + priority
                + " " + tag + " " + msg).getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
于 2012-12-21T00:38:36.253 回答