Android development and debugging log tools [support saving to SD card]
Directly run the Code:
Package com. example. callstatus; import java. io. file; import java. io. fileWriter; import java. io. IOException; import java. io. printWriter; import java. io. stringWriter; import java.net. unknownHostException; import java. text. simpleDateFormat; import java. util. date; import android. annotation. suppressLint; import android. OS. environment; import android. util. log;/*** Android development debugging Log tool class [supports saving to SD card]
*
** Some permissions are required:
*
*
** @ Author PMTOAM **/@ SuppressLint ("SimpleDateFormat") public class MyLog {public static final String CACHE_DIR_NAME = "dPhoneLog"; public static boolean isDebugModel = true; // whether to output the public static boolean isSaveDebugInfo = true; // whether to save the debug log public static boolean isSaveCrashInfo = true; // whether to save the public static void v (final String tag, final String msg) {if (isDebugModel) {Log. v (tag, "-->" + msg) ;}} publi C static void d (final String tag, final String msg) {if (isDebugModel) {Log. d (tag, "-->" + msg) ;}} public static void I (final String tag, final String msg) {if (isDebugModel) {Log. I (tag, "-->" + msg) ;}} public static void w (final String tag, final String msg) {if (isDebugModel) {Log. w (tag, "-->" + msg) ;}/ *** debug log for easy development and tracking. * @ Param tag * @ param msg */public static void e (final String tag, final String msg) {if (isDebugModel) {Log. e (tag, "-->" + msg);} if (isSaveDebugInfo) {new Thread () {public void run () {write (time () + tag + "-->" + msg + "\ n ");};}. start () ;}}/*** try catch. You can upload feedback for online products. * @ Param tag * @ param tr */public static void e (final String tag, final Throwable tr) {if (isSaveCrashInfo) {new Thread () {public void run () {write (time () + tag + "[CRASH] -->" + getStackTraceString (tr) + "\ n ");};}. start () ;}}/*** get the caught exception String * @ param tr * @ return */public static String getStackTraceString (Throwable tr) {if (tr = null) {return "";} Throwable t = tr; while (t! = Null) {if (t instanceof UnknownHostException) {return "";} t = t. getCause ();} StringWriter sw = new StringWriter (); PrintWriter pw = new PrintWriter (sw); tr. printStackTrace (pw); return sw. toString ();}/*** identifies the time when each log is generated * @ return */private static String time () {return "[" + new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). format (new Date (System. currentTimeMillis () + "]";}/*** use the year, month, and day as the log file name * @ return */private Static String date () {return new SimpleDateFormat ("yyyy-MM-dd "). format (new Date (System. currentTimeMillis ();}/*** save to log file * @ param content */public static synchronized void write (String content) {try {FileWriter writer = new FileWriter (getFile (), true); writer. write (content); writer. close ();} catch (IOException e) {e. printStackTrace () ;}}/*** Get log File path * @ return */public static String getFile () {File sd Dir = null; if (Environment. getExternalStorageState (). equals (android. OS. environment. MEDIA_MOUNTED) sdDir = Environment. getExternalStorageDirectory (); File cacheDir = new File (sdDir + File. separator + CACHE_DIR_NAME); if (! CacheDir. exists () cacheDir. mkdir (); File filePath = new File (cacheDir + File. separator + date () + ". txt "); return filePath. toString ();}}