Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
android log to file, will create a log file in sdcard root directory.
public void appendLog(String text) {
String logFileName = "AppLog.txt";
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.getDefault());
String currentDateandTime = sdf.format(new Date());
File logFile = new File(Environment.getExternalStorageDirectory()
+ "/" + logFileName);
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(currentDateandTime + " " + text);
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.