Created
January 26, 2014 07:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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