Skip to content

Instantly share code, notes, and snippets.

@vicky7230
Created June 3, 2017 19:11
Show Gist options
  • Save vicky7230/a7794d1932748037086e0d93ec807abd to your computer and use it in GitHub Desktop.
Save vicky7230/a7794d1932748037086e0d93ec807abd to your computer and use it in GitHub Desktop.
public class FileLoggingTree extends Timber.DebugTree {
private static final String TAG = FileLoggingTree.class.getSimpleName();
private Context context;
public FileLoggingTree(Context context) {
this.context = context;
}
@Override
protected void log(int priority, String tag, String message, Throwable t) {
try {
File direct = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/YoScholarDeliveryLogs");
if (!direct.exists()) {
direct.mkdir();
}
String fileNameTimeStamp = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
String logTimeStamp = new SimpleDateFormat("E MMM dd yyyy 'at' hh:mm:ss:SSS aaa", Locale.getDefault()).format(new Date
());
String fileName = fileNameTimeStamp + ".html";
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/YoScholarDeliveryLogs" + File.separator + fileName);
file.createNewFile();
if (file.exists()) {
OutputStream fileOutputStream = new FileOutputStream(file, true);
fileOutputStream.write(("<p style=\"background:lightgray;\"><strong style=\"background:lightblue;\">&nbsp&nbsp" + logTimeStamp + " :&nbsp&nbsp</strong>&nbsp&nbsp" + message + "</p>").getBytes());
fileOutputStream.close();
}
//if (context != null)
//MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
} catch (Exception e) {
Log.e(TAG, "Error while logging into file : " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment