Android development and debugging log tools [support saving to SD card] And androidsd

Source: Internet
Author: User

Android development and debugging log tools [support saving to SD card] And androidsd

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] <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 "; public static boolean isDebugModel = true; // whether to output the public static boolean isSaveDebugInfo = tru E; // whether to save the 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 (fina L 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 ();}}




On android, how does one record background logs and write files to the SD card?

1. debug alarm manager example, modify the suffix) 2. Create a logger object private static final Logger logger = LoggerFactory. getLogger (main. class); 3. initialize the PropertyConfigurator in the oncreate method of the activit or service of the program. getaggregator (this ). configure (); 4. Set the microlo G. the properties file is placed in the assets folder. Note that the assets folder is the same as the res folder and then the microlog is changed. the properties file contains the following content: microlog. level = DEBUGmicrolog. appender = LogCatAppender; FileAppendermicrolog. formatter = PatternFormattermicrolog. formatter. patternFormatter. pattern = % c [% P] % m % T5, write log ger. debug (my debug); 6. In AndroidManifest. xml: Add SD card write permission <uses-permission android: name = android. permission. WRITE_EXTERNAL_STORAGE/start the program and go to the root directory of the SD card. You can find a microl The og.txt file contains the logs we recorded. 1. the above logs are called in multiple places. Sometimes we may find that only one record is saved, and the logs after each record overwrite the previous log, so we need to add additional code; 2. after Step 3: final FileAppender fa = logger. getAppender (1); fa. setAppend (true); OK, this is done.

In android development, files are downloaded and written to the SD card, but the downloaded files are incomplete each time.

Public File writeToSDFromInput (String path, String fileName,
InputStream inputStream ){
File file = null;
OutputStream outputStream = null;
CreateSDDir (path );
Try {
File = createFileInSDCard (fileName, path );
OutputStream = new FileOutputStream (file );
Byte buffer [] = new byte [4*1024];
Int temp;
While (temp = inputStream. read (buffer ))! =-1 ){
OutputStream. write (buffer, 0, temp );
}
OutputStream. flush ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
OutputStream. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
Return file;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.