Directly on 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 Debug Log Tool class [support save to SD card]<br> * <br> * * requires some permissions: < br> * <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <br> * < Uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><br> * * @author Pmtoam * */@ Suppresslint ("SimpleDateFormat") public class Mylog{public static final String cache_dir_name = "Dphonelog";p ublic static Boolean Isdebugmodel = true;//whether to output log public static Boolean issavedebuginfo = true;//Save debug Log public static Boolean Issavecrashinfo = true;//Whether to save the error log public static void V (final string tag, final string msg) {if (Isdebugmodel) {LOG.V (tag, "--" + msg);}} public 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 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 is used. On-line products can upload feedback.
* @param tag * @param tr */public static void E (Final String tag, final throwable tr) {if (issavecrashinfo) {new Thread () {p ublic void Run () {Write (time () + tag + "[CRASH]-" + getstacktracestring (TR) + "\ n");}. Start ();}} /** * Gets the string that catches the exception * @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 when each log was generated * @return */private static String time () {return "[" + New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format ( New Date (System.currenttimemillis ())) + "]";} /** * Date as log file name * @return */private static String date () {return new SimpleDateFormat ("Yyyy-mm-dd"). Format (new date (Syst Em.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 Sddir = null;if (Environment.getexternalstoragestate (). Equa LS (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 ();}}
Android Development Debug Log Tool class [support save to SD card]