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 は呼び出されません。次のように2つを組み合わせる必要があります。

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 に答える