Reputation: 3453
For my application I want to create a class where I can log data into a file. Within my app there's the ability to email me if they're experiencing a problem and data from this file could potentially help me dissect the issue. Firstly, am I going about this the wrong way? Is there a better way to log exceptions that occur?
The problem is if I attempt to use the log method from another class using:
Logger.log(0,"","","");
It fails to find the file or indeed create the file if it isn't already created. The code is attached below.
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();
}
}
}
Upvotes: 1
Views: 2116
Reputation: 2387
This is a code snippet from my own application:
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%");
}
}
Upvotes: 1
Reputation: 3453
The file name is not suffice to write a file. You need to give a directory when using FileOutputStream unlike when using openFileOutput. Thus if you change this line:
final static String FileName = "Log";
To
final static String FileName = "data/data/com.example.test/files/Log";
This will fix the issue. Finally oncreate is not called when accessing a method within a class. You have to combine the two as follows:
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();
}
}
Upvotes: 0
Reputation: 180
Is there a reason you can't use the standard logging libraries like log4j or slf4j? I think these would do what you need without needing to write the Logger class yourself. It looks like slf4j on Android is still in beta, but anecdotal accounts show that people have used it with success.
Upvotes: 2