Created
May 16, 2018 09:02
-
-
Save iBog/9d86fa3905ac07ad0894b3ed50cc4203 to your computer and use it in GitHub Desktop.
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
import android.os.Environment; | |
import android.util.Log; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
/** | |
* Created by alexander on 12/09/16. | |
*/ | |
public class Logger { | |
public static PrintWriter printWriter = null; | |
public static String LOG_DIR = "/MyLogger"; | |
public static String LOG_FILE = "test.log"; | |
private static void init() { | |
// Check if external media is writable | |
String state = Environment.getExternalStorageState(); | |
if (!Environment.MEDIA_MOUNTED.equals(state)) { | |
Log.e(Logger.class.getName(), "Cannot write to external media"); | |
return; | |
} | |
if (printWriter == null) { | |
try { | |
File dir = new File(Environment.getExternalStorageDirectory() + LOG_DIR); | |
dir.mkdirs(); | |
printWriter = new PrintWriter(new FileWriter(new File(dir, LOG_FILE), true)); // Line 34 | |
} catch (IOException e) { | |
Log.e(Logger.class.getName(), "initExternal() -> IOException", e); | |
} | |
} | |
} | |
private static synchronized int log(int priority, String tag, String msg) { | |
int res = Log.println(priority, tag, msg); | |
if (printWriter == null) { | |
init(); // May be called just once, depending on your requirements | |
} | |
printWriter.print(tag + " "); | |
printWriter.print(msg + "\r\n"); | |
printWriter.flush(); | |
return res; | |
} | |
// Duplicates of standard android.util.Log methods: | |
public static int v(String tag, String msg) { | |
return log(Log.VERBOSE, tag, msg); | |
} | |
public static int v(String tag, String msg, Throwable tr) { | |
return log(Log.VERBOSE, tag, msg + '\n' + Log.getStackTraceString(tr)); | |
} | |
public static int d(String tag, String msg) { | |
return log(Log.DEBUG, tag, msg); | |
} | |
public static int d(String tag, String msg, Throwable tr) { | |
return log(Log.DEBUG, tag, msg + '\n' + Log.getStackTraceString(tr)); | |
} | |
public static int i(String tag, String msg) { | |
return log(Log.INFO, tag, msg); | |
} | |
public static int i(String tag, String msg, Throwable tr) { | |
return log(Log.INFO, tag, msg + '\n' + Log.getStackTraceString(tr)); | |
} | |
public static int w(String tag, String msg) { | |
return log(Log.WARN, tag, msg); | |
} | |
public static int w(String tag, String msg, Throwable tr) { | |
return log(Log.WARN, tag, msg + '\n' + Log.getStackTraceString(tr)); | |
} | |
public static int e(String tag, String msg) { | |
return log(Log.ERROR, tag, msg); | |
} | |
public static int e(String tag, String msg, Throwable tr) { | |
return log(Log.ERROR, tag, msg + '\n' + Log.getStackTraceString(tr)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment