Skip to content

Instantly share code, notes, and snippets.

@youweit
Created January 26, 2014 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youweit/8629573 to your computer and use it in GitHub Desktop.
Save youweit/8629573 to your computer and use it in GitHub Desktop.
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