Logger of Android Framework (debug code)

Source: Internet
Author: User

1. Logger

Logger is Android is a simple, beautiful, powerful Android log program.

The Log program provides:

    • Threading Information Thread Information
    • Class Information Classes Information
    • Methods Information Method Information
    • Beautiful printed JSON content Pretty-print for JSON contents
    • Clean output
    • Jump to Source feature
Gradle

Compile ' com.orhanobut:logger:1.3 '

2. Use the logger as follows:

1  PackageCom.himi.loggerdemo;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 ImportJava.io.InputStream;9 Ten Importorg.json.JSONException; One ImportOrg.json.JSONObject; A ImportOrg.json.JSONStringer; - ImportOrg.xmlpull.v1.XmlPullParser; - Importorg.xmlpull.v1.XmlPullParserException; the ImportOrg.xmlpull.v1.XmlSerializer; -  - ImportCom.orhanobut.logger.Logger; -  + Importandroid.app.Activity; - ImportAndroid.os.Bundle; + ImportAndroid.util.Log; A Importandroid.util.Xml; at  -  Public classMainactivityextendsActivity { -  -     Private Static FinalString TAG = "Mainactivity"; -  - @Override in     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); to Setcontentview (r.layout.activity_main); +  -LOG.D (TAG, "from Log"); the  *         //logger must be initialized, otherwise null pointer $Logger.init ("MyLogger");Panax Notoginseng  -LOGGER.D ("From Logger"); theLOGGER.E ("Hello"); +LOGGER.W ("Hello"); ALOGGER.V ("Hello"); the  +LOGGER.WTF ("Hello"); -  $         //generate a JSON data $Jsonobject JSON =NewJsonobject (); -         Try { -Json.put ("title", "Book1"). Put ("Price", "$11"); the Logger.json (json.tostring ()); -}Catch(jsonexception e) {Wuyi             //TODO auto-generated Catch block the e.printstacktrace (); -         } Wu  -         //generate an XML data About         Try { $File File =NewFile (Getfilesdir (), "Data.xml"); -FileOutputStream OS =Newfileoutputstream (file); -  -XmlSerializer serializer =Xml.newserializer (); A  +Serializer.setoutput (OS, "Utf-8")); theSerializer.startdocument ("Utf-8",true); -Serializer.starttag (NULL, "Student"); $Serializer.starttag (NULL, "name"); theSerializer.text ("Hebao"); theSerializer.endtag (NULL, "name"); theSerializer.starttag (NULL, "Num"); theSerializer.text ("24"); -Serializer.endtag (NULL, "Num"); in  theSerializer.endtag (NULL, "Student"); the serializer.enddocument (); About os.close (); the  theXmlpullparser parser =Xml.newpullparser (); the             //2. Setting parameters for the parser +InputStream InputStream =Newfileinputstream (file); -Parser.setinput (InputStream, "Utf-8"); the             intType =Parser.geteventtype ();BayiStringBuilder SB =NewStringBuilder (); the              while(Type! =xmlpullparser.end_document) { the                 Switch(type) { -                  CaseXmlpullparser.start_tag: -                     if("Name". Equals (Parser.getname ())) { the                         //This is the name node theString name =Parser.nexttext (); theSb.append ("--name:" +name); the}Else if("Num". Equals (Parser.getname ())) { -                         //This is the name node theString num =Parser.nexttext (); theSb.append ("--num:" +num); the                     }94                      Break; the                 } theType = Parser.next ();//Let the parser parse the next element the             }98 inputstream.close (); AboutString Text =sb.tostring (); - 101 logger.xml (text);102 103}Catch(FileNotFoundException e) {104             //TODO auto-generated Catch block the e.printstacktrace ();106}Catch(IllegalArgumentException e) {107             //TODO auto-generated Catch block108 e.printstacktrace ();109}Catch(IllegalStateException e) { the             //TODO auto-generated Catch block111 e.printstacktrace (); the}Catch(IOException e) {113             //TODO auto-generated Catch block the e.printstacktrace (); the}Catch(xmlpullparserexception e) { the             //TODO auto-generated Catch block117 e.printstacktrace ();118         }119  -     }121 122}

View the Logcatas follows:

Logger of Android Framework (debug code)

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.