Created
December 30, 2015 09:16
-
-
Save junlincao/4951e7540dabd3a4b072 to your computer and use it in GitHub Desktop.
android logUtil
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 class LogUtil { | |
private boolean debug; | |
private String tag; | |
private static final String LOG_FILE_NAME = "jz.log"; | |
public LogUtil() { | |
this(null); | |
} | |
public LogUtil(String tag) { | |
this(true, tag); | |
} | |
public LogUtil(boolean debug, String tag) { | |
this.debug = debug & JZApp.GLOBAL_DEBUG; | |
this.tag = tag; | |
} | |
public void v(String msg) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.v(m[0], m[1]); | |
} | |
} | |
public void v(String msg, Object... args) { | |
if (debug) { | |
String[] m = buildMessage(String.format(msg, args)); | |
Log.v(m[0], m[1]); | |
} | |
} | |
public void v(String msg, Throwable thr) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.v(m[0], m[1], thr); | |
} | |
} | |
public void d(String msg) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.d(m[0], m[1]); | |
} | |
} | |
public void d(String msg, Object... args) { | |
if (debug) { | |
String[] m = buildMessage(String.format(msg, args)); | |
Log.d(m[0], m[1]); | |
} | |
} | |
public void d(String msg, Throwable thr) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.d(m[0], m[1], thr); | |
} | |
} | |
public void i(String msg) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.i(m[0], m[1]); | |
} | |
} | |
public void i(String msg, Object... args) { | |
if (debug) { | |
String[] m = buildMessage(String.format(msg, args)); | |
Log.i(m[0], m[1]); | |
} | |
} | |
public void i(String msg, Throwable thr) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.i(m[0], m[1], thr); | |
} | |
} | |
public void e(String msg) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.e(m[0], m[1]); | |
} | |
} | |
public void e(String msg, Object... args) { | |
if (debug) { | |
String[] m = buildMessage(String.format(msg, args)); | |
Log.e(m[0], m[1]); | |
} | |
} | |
public void e(String msg, Throwable thr) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.e(m[0], m[1], thr); | |
} | |
} | |
public void w(String msg) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.w(m[0], m[1]); | |
} | |
} | |
public void w(String msg, Object... args) { | |
if (debug) { | |
String[] m = buildMessage(String.format(msg, args)); | |
Log.w(m[0], m[1]); | |
} | |
} | |
public void w(String msg, Throwable thr) { | |
if (debug) { | |
String[] m = buildMessage(msg); | |
Log.w(m[0], m[1], thr); | |
} | |
} | |
public void file(String msg, Throwable thr) { | |
if (!debug) { | |
return; | |
} | |
File f = new File(Environment.getExternalStorageDirectory(), LOG_FILE_NAME); | |
FileOutputStream fos = null; | |
try { | |
fos = new FileOutputStream(f, true); | |
String fm = new Date().toString() + "\n"; | |
fos.write(fm.getBytes(Charset.forName("utf-8"))); | |
thr.printStackTrace(new PrintStream(fos)); | |
fos.write("\n\n".getBytes()); | |
} catch (Exception e) { | |
e("write file log failed"); | |
i(msg, thr); | |
} finally { | |
if (fos != null) { | |
try { | |
fos.close(); | |
} catch (IOException e) { | |
// null | |
} | |
} | |
} | |
} | |
public void file(String msg, Object... args) { | |
if (!debug) { | |
return; | |
} | |
File f = new File(Environment.getExternalStorageDirectory(), LOG_FILE_NAME); | |
FileOutputStream fos = null; | |
try { | |
fos = new FileOutputStream(f, true); | |
String fm = new Date().toString() + "\n"; | |
fos.write(fm.getBytes(Charset.forName("utf-8"))); | |
} catch (Exception e) { | |
e("write file log failed"); | |
i(msg, args); | |
} finally { | |
if (fos != null) { | |
try { | |
fos.close(); | |
} catch (IOException e) { | |
// null | |
} | |
} | |
} | |
} | |
protected String[] buildMessage(String msg) { | |
StackTraceElement caller = new Throwable().fillInStackTrace().getStackTrace()[2]; | |
String msgTag = tag; | |
if (TextUtils.isEmpty(tag)) { | |
msgTag = caller.getClassName().substring(caller.getClassName().lastIndexOf('.') + 1); | |
} | |
String res = caller.getClassName() + ":" + caller.getMethodName() + "(): \n" + msg; | |
return new String[] { msgTag, res }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use file() method muse have permission of "android.permission.WRITE_EXTERNAL_STORAGE"