Created
March 29, 2019 13:47
-
-
Save yudenzel/e65c90966250e9c14d43a473b7d8d2f7 to your computer and use it in GitHub Desktop.
FileLogger.java
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
| import android.util.SparseArray; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.io.PrintWriter; | |
| import java.io.Writer; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| import static android.util.Log.ASSERT; | |
| import static android.util.Log.DEBUG; | |
| import static android.util.Log.ERROR; | |
| import static android.util.Log.INFO; | |
| import static android.util.Log.VERBOSE; | |
| import static android.util.Log.WARN; | |
| public class FileLogger { | |
| private static final SparseArray<String> PRIORITY_TAG_MAP = initTagMap(); | |
| private static final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); | |
| private String mFilePath; | |
| private PrintWriter mWriter = null; | |
| public FileLogger(String filePath) { | |
| mFilePath = filePath; | |
| } | |
| public synchronized void close() { | |
| mFilePath = null; | |
| } | |
| public void v(String tag, String message) { | |
| printlnImpl(VERBOSE, tag, message, null); | |
| } | |
| public void v(String tag, String message, Throwable tr) { | |
| printlnImpl(VERBOSE, tag, message, tr); | |
| } | |
| public void d(String tag, String message) { | |
| printlnImpl(DEBUG, tag, message, null); | |
| } | |
| public void d(String tag, String message, Throwable tr) { | |
| printlnImpl(DEBUG, tag, message, tr); | |
| } | |
| public void i(String tag, String message) { | |
| printlnImpl(INFO, tag, message, null); | |
| } | |
| public void i(String tag, String message, Throwable tr) { | |
| printlnImpl(INFO, tag, message, tr); | |
| } | |
| public void w(String tag, String message, Throwable tr) { | |
| printlnImpl(WARN, tag, message, tr); | |
| } | |
| public void w(String tag, String message) { | |
| printlnImpl(WARN, tag, message, null); | |
| } | |
| public void e(String tag, String message) { | |
| printlnImpl(ERROR, tag, message, null); | |
| } | |
| public void e(String tag, String message, Throwable tr) { | |
| printlnImpl(ERROR, tag, message, tr); | |
| } | |
| public void wtf(String tag, String message) { | |
| printlnImpl(ASSERT, tag, message, null); | |
| } | |
| public void wtf(String tag, String message, Throwable tr) { | |
| printlnImpl(ASSERT, tag, message, tr); | |
| } | |
| private synchronized void printlnImpl(int priority, String tag, String message, Throwable tr) { | |
| Writer writer = getWriter(); | |
| if( writer != null ) { | |
| mWriter.format("%s %s %s: %s", | |
| mDateFormat.format(new Date()), | |
| PRIORITY_TAG_MAP.get(priority), | |
| tag, | |
| message | |
| ); | |
| if( tr != null ) { | |
| tr.printStackTrace(mWriter); | |
| } | |
| mWriter.print("\n"); | |
| mWriter.flush(); | |
| } | |
| } | |
| private synchronized PrintWriter getWriter() { | |
| if( mWriter == null && mFilePath != null ) { | |
| try { | |
| mWriter = new PrintWriter(new FileOutputStream(mFilePath, true)); | |
| } catch (FileNotFoundException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| return mWriter; | |
| } | |
| private static SparseArray<String> initTagMap() { | |
| SparseArray<String> map = new SparseArray<String>(); | |
| map.append(VERBOSE, "V"); | |
| map.append(DEBUG, "D"); | |
| map.append(INFO, "I"); | |
| map.append(ERROR, "E"); | |
| map.append(ASSERT, "WTF"); | |
| return map; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment