Android output Logs log class

Source: Internet
Author: User

Android.util.Log commonly used methods have the following 5:


LOG.V () log.d () log.i () LOG.W () and LOG.E (). correspond to Verbose,debug,info,warn,error according to the first letter respectively.


1, LOG.V debugging color is black, any message will be output, here V for verbose wordy meaning, usually use is LOG.V ("," ");


2, LOG.D output color is blue, only the meaning of debug debugging output, but he will output the upper level of information, filtering up can be DDMS by the logcat tag to choose.


3, LOG.I output is green, general informational message information, it will not output LOG.V and LOG.D information, but will display I, W and e information.


4, log.w the meaning of orange, can be seen as a warning warning, we generally need to pay attention to optimize the Android code, while selecting it will also output LOG.E information.


5, LOG.E is red, you can think of error errors, here only the red error message, these errors need our careful analysis, view the stack of information.

1  Public classMyLog {2     Private StaticBoolean Mylog_switch =true;//log file Total switch3     Private StaticBoolean Mylog_write_to_file =true;//Log write file switch4     Private Static CharMylog_type = ' V ';//Enter the log type, W for only output alarm information, etc., V for output all information5     Private StaticString Mylog_path_sdcard_dir = "/sdcard/";//The path of the log file in SDcard6     Private Static intsdcard_log_file_save_days = 0;//maximum number of days to save log files in an SD card7     Private StaticString mylogfilename = "Log.txt";//log file name for this class of output8     Private StaticSimpleDateFormat MYLOGSDF =NewSimpleDateFormat (9"Yyyy-mm-dd HH:mm:ss");//output format of the logTen     Private StaticSimpleDateFormat logfile =NewSimpleDateFormat ("Yyyy-mm-dd");//log File Format One      Publiccontext context; A  -      Public Static voidW (String tag, Object msg) {//warning Message -Log (tag, msg.tostring (), ' W '); the     } -  -      Public Static voidE (String tag, Object msg) {//error Message -Log (tag, msg.tostring (), ' E '); +     } -  +      Public Static voidD (String tag, Object msg) {//Debugging Information ALog (tag, msg.tostring (), ' d '); at     } -  -      Public Static voidI (String tag, Object msg) {// -Log (tag, msg.tostring (), ' I '); -     } -  in      Public Static voidV (String tag, Object msg) { -Log (tag, msg.tostring (), ' V '); to     } +  -      Public Static voidW (string tag, string text) { theLog (tag, text, ' W ')); *     } $ Panax Notoginseng      Public Static voide (String tag, string text) { -Log (tag, text, ' E '); the     } +  A      Public Static voidd (String tag, string text) { theLog (tag, text, ' d '); +     } -  $      Public Static voidI (string tag, string text) { $Log (tag, text, ' I '); -     } -  the      Public Static voidV (string tag, string text) { -Log (tag, text, ' V '));Wuyi     } the  -     /** Wu * According to tag, MSG and level, output log -      *  About      * @paramTag $      * @parammsg -      * @param Level -      * @returnvoid -      * @sincev 1.0 A      */ +     Private Static voidLog (string tag, string msg,CharLevel ) { the         if(mylog_switch) { -             if(' e ' = = Level && (' e ' = = Mylog_type | | ' V ' = = Mylog_type)) {//Output error message $ LOG.E (tag, msg); the}Else if(' w ' = = Level && (' w ' = = Mylog_type | | ' V ' = =Mylog_type)) { the LOG.W (tag, msg); the}Else if(' d ' = = Level && (' d ' = = Mylog_type | | ' V ' = =Mylog_type)) { the LOG.D (tag, msg); -}Else if(' i ' = = Level && (' d ' = = Mylog_type | | ' V ' = =Mylog_type)) { in log.i (tag, msg); the}Else { the LOG.V (tag, msg); About             } the             if(Mylog_write_to_file) the Writelogtofile (level), Tag, msg (string.valueof); the         } +     } -  the     /**Bayi * Open log file and write to log the      *  the      * @return -      * **/ -     Private Static voidWritelogtofile (string mylogtype, string tag, string text) {//Create a new or open log file theDate Nowtime =NewDate (); theString Needwritefiel =Logfile.format (nowtime); theString needwritemessage = Mylogsdf.format (nowtime) + "" +Mylogtype the+ "+ tag +" "+text; -File Dirpath =environment.getexternalstoragedirectory (); theFile File =NewFile (Dirpath.tostring (), Needwritefiel + mylogfilename);//Mylog_path_sdcard_dir the         Try { theFileWriter Filerwriter =NewFileWriter (file,true);//The following parameter indicates whether to connect the original data in the file without overwriting94BufferedWriter Bufwriter =NewBufferedWriter (filerwriter); the Bufwriter.write (needwritemessage); the bufwriter.newline (); the bufwriter.close ();98 filerwriter.close (); About}Catch(IOException e) { - e.printstacktrace ();101         }102     }103 104     /** the * Delete the developed log files106      * */107      Public Static voidDelfile () {//Delete log Files108String Needdelfiel =Logfile.format (Getdatebefore ());109File Dirpath =environment.getexternalstoragedirectory (); theFile File =NewFile (Dirpath, Needdelfiel + mylogfilename);//Mylog_path_sdcard_dir111         if(File.exists ()) { the File.delete ();113         } the     } the  the     /**117 * Get the date of the day before the current time to get the log file name that needs to be deleted118      * */119     Private StaticDate Getdatebefore () { -Date Nowtime =NewDate ();121Calendar now =calendar.getinstance ();122 Now.settime (nowtime);123 Now.set (Calendar.date, Now.get (calendar.date)124-sdcard_log_file_save_days); the         returnnow.gettime ();126     }127}


Note: You also need to add permissions to the Androidmanifest.xml file
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Android output Logs log class

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.