The Material!!! Open Source Log Library logger usage cheats

Source: Internet
Author: User

Guide Log for development is very important, whether it is debugging Data View, bug tracking location, data collection statistics, daily routine operation and maintenance, etc., are heavily used. Today introduces the use of the famous open source log library logger, the address of the library: Https://github.com/orhanobut/logger

By joining Gradle in Android Studio, you can reference a dependent logger library:
dependencies {     compile ' com.orhanobut:logger:1.15 '     }
features that the logger library can provide:
    1. Thread's information
    2. Class of information
    3. Methods of information
    4. Format to print JSON, XML, etc.
    5. Click the link to go to the source print place
Use of Logger

Very simple to use:

String userName = "Jerry"; LOGGER.I (UserName);

Printing effect:

Log effects

The format of this effect is not very clear, you can see the current print at the thread name, method name, method location, printing information. At the same time click on the location of the method can also jump to the location of the printing, so debugging up is very convenient.
Can see the print tag is Prettylogger, this is the logger default tag, if you want to modify can:

Modify the print Tag Value logger.init ("mainactivity"); String userName = "Jerry"; LOGGER.I (UserName);

Modify the log effect of the tag

Some small partners think, I do not want to always use a tag, that is not to write a lot of logger.init (tag) to modify, we look at the method called Init, the author's meaning is estimated to use only once. The following can be done by:

Logger.init ("mainactivity"); String userName = "Jerry"; LOGGER.I (userName);//Change the current print to a separate tag name logger.t ("App"). I (UserName); LOGGER.E (UserName);

Individually modified tag Print effect

As can be seen from the figure, the use of LOGGER.T (tag) This method to modify the tag, but also does not affect the subsequent printing of the tag, is not flexible and convenient it.

We continue to look, we all know that Android comes with the log log, it is not possible to print directly in addition to string values or variable objects.

The system comes with printing int, error

Sometimes it is really troublesome, but also can be stitched into a string to print (Baby (old??????) Old psychological suffering as the Android Siege Lion have experienced), the powerful logger can do:

LOGGER.I ("Hello everyone, my name is%s, this year%d, very happy to see my article!!!", "Jerry", 18);

stitching int Value Printing effect

Learn C language should know: printf ("Age:%d", 16) such a printing function, is not a sense of déjà vu. To change it again:

LOGGER.T ("Are you 16 years old?"). I ("Hello everyone, my name is%s, this year%d years old, I am very happy to see my article!!!", "Jerry", 16);

Poor diary, you have a feeling of being played bad.

In addition to these, logger can print many forms of data, which greatly facilitates our development:

LOGGER.D ("Hello"); LOGGER.E ("Hello"); LOGGER.W ("Hello"); LOGGER.V ("Hello"); LOGGER.WTF ("Hello");//  print JSON format String json = createjson (). toString (); Logger.json (JSON);//  print XML Format logger.xml (xml_content);//  print custom levels, tags, information and other format logs Logger.log (debug,  " Tag ", " message ",  throwable);//  create JSON Data Private jsonobject createjson ()  {try      {Jsonobject person = new jsonobject ();       person.put ("Phone",  "12315");      Jsonobject address = new jsonobject ();      Address.put ("Country",  "China");      Address.put ("Province",  "Fujian");      Ddress.put ("City",  "Xiamen");      Person.put ("Address",  address);      Person.put ("Married",  true);   return person;  } catch  (jsonexception e)  {logger.e (e,  "create json error occure            D ");                         } return null;                 }               } 

Beautiful JSON-formatted print effect

About print JSON, there is also a small episode, Bo Master First use, half a day no effect, did not print out, so I changed into a Ddms logcat, or the same. Later to follow the source code, see Logger.json () is how to print, the source is ultimately using the system LOG.D (tag, msg) to print. So Bo master with break point debug, found that all the strings in the format has been passed to LOG.D msg, is not printing (heart tired), so I directly:

LOG.D ("Mainactivity", "ONACTIVITYCREATED:LOG.D Where to run ...");

Log or do not display, this indicates that there may be my Meizu MX4 mobile phone problems, Google a bit, sure enough to say Meizu mobile phone log.d () log does not print the problem, is because Meizu default does not turn on debug mode of log printing, You need to select all allow in the developer options---advanced log output---(Refer to resolution article: http://blog.csdn.net/u013175701/article/details/51428870), and the log prints.

The logger library can also be customized for display:

    • Settings setting = Logger.init ("mainactivity");
      Setting.loglevel (Loglevel.full)
      Show all logs, Loglevel.none do not display logs, default is full
      . Methodcount (5)
      Method stack print number, default is 5
      . Methodoffset (0)
      Set the function offset value of the call stack, 0 then the output stack information from the function that prints the log, which defaults to 0
      . Hidethreadinfo ();
      Hide Thread Information

. Logadapter (New Androidlogadapter ()); Customize a print adapter, which is suitable for Android log printing, you can also implement the Logadapter interface to do some special needs of the log printing adaptation

Custom Log Display

The thread display is hidden, and the method stack shows an offset of 0, which means that the methods in the 5 stacks that start counting the number of methods from where the log was printed are printed.

print Array, List, map, and other object data
 string[] names = {"Jerry",  "Emily",  "Small Five",  "Hongyang",  "Seven Cats"}; LOGGER.D (names);  //  print character array list<user> users = new arraylist<> ()        ; for  (int i = 0; i < names.length; i++)  {User user&nbs                P;= new user (Names[i], 10 + i);                 users.add (user); } logger.d (users);  //  Print List class user {private string                Name                 private int age;                             Public user (string name, int age)  {this.name = name;                             this.age = age;        } public string getname ()  { return name; } public void setname (String name)                  {this.name = name;               } public int getage ()  {return age;               } public void setage (int age)  {this.age = age; //  to overwrite the object's ToString method to print out the full log information @Override public string tostring ()  {retur n  "user{"  +  "name=" " + name + "/" +",  age= "                                 + age + '} '; }              }

Print character array and list (user does not overwrite ToString)

Print character array and list (user has overwrite ToString)

At this point, the introduction of the use of logger library is over, what fun, details of the use of the way, small partners have better suggestions welcome message! Thank you for watching!!!

Originally from: http://www.linuxprobe.com/open-logging-logger.html

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

The Material!!! Open Source Log Library logger usage cheats

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.