Created
June 3, 2017 19:11
-
-
Save vicky7230/a7794d1932748037086e0d93ec807abd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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;\">  " + logTimeStamp + " :  </strong>  " + 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