In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the ways to use Logger". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the ways to use Logger?"
By adding it to gradle in Android Studio, you can reference the dependent logger library:
Dependencies {compile 'com.orhanobut:logger:1.15'}
What the Logger library can provide:
Thread information class information method information format print json, xml, etc. Click on the link to jump to the source code print
The use of Logger
It is very easy to use:
String userName = "Jerry"; Logger.i (userName)
The effect of printing:
Log effect
This effect format is not very clear, you can see, the current printing thread name, method name, method location, printing information. At the same time, the location of the click method can also jump to the print location, so it is very convenient to debug. You can see that the TAG printed in the above figure is PRETTYLOGGER, which is the default tag of Logger. If you want to modify it, you can:
/ / modify the printed tag value Logger.init ("MainActivity"); String userName = "Jerry"; Logger.i (userName)
Modify the log effect of tag
Some friends think that I don't want to use only one tag all the time. Isn't it necessary to write a lot of Logger.init (tag) to modify it? we think the method is called init, and the author probably means to use it only once. The following can be done through:
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 printing effect
As can be seen from the figure, the use of Logger.t (tag) this method to modify the tag, and will not affect the rest of the print tag, is not flexible and convenient.
As we continue to look, we all know that the Log log that comes with android cannot be printed directly except for the value or variable object thought by the string.
The printing int that comes with the system reports an error
Sometimes it's really troublesome, and you have to be able to splice it into a string to print (Baby (baby). ◕ engineers have experienced the hardship of being Android engineers), and powerful Logger can do this:
Logger.i ("Hello everyone, my name is% s, this year% d, I'm glad you came to read my article!", "Jerry", 18)
Numerical printing effect of spliced int
Anyone who has studied the C language should know whether a print function like printf ("age:% d", 16) feels like deja vu. Let's change it again:
Logger.t ("are you only 16 fucking years old"). I ("Hello everyone, my name is% s, and I am% d years old this year. I'm glad you all came to read my article!", "Jerry", 16)
Poor Lizhijun, I feel like I've been spoiled.
In addition to these, Logger can also 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 level, tag, information and other formats 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 occured");} return null;}}
Beautiful printing effect in json format
There is another episode about printing json. When the blogger used it for the first time, it didn't work for a long time and didn't print it out, so I changed it to DDMS Logcat, which is still the same. Later, to track the source code to see how Logger.json () is printed, the source code is finally printed using the system's Log.d (tag, msg). So the blogger used the breakpoint debug and found that all the strings in the above format were passed into the msg of Log.d, but did not print (tired), so I directly:
Log.d ("MainActivity", "onActivityCreated: where is log.d...")
The log is still not displayed, which means that there may be a problem with my Meizu MX4 phone. After Google, it is said that the Log.d () log of Meizu phone does not print, because Meizu does not enable log printing in Debug mode by default. You need to go to the developer option-Advanced Log output-select all allowed, and the log is printed.
The Logger library can also be customized for display:
Settings setting = Logger.init ("MainActivity"); setting.logLevel (LogLevel.FULL) / / shows all logs. LogLevel.NONE does not show logs. Default is Full .methodCount (5) / / number of method stack prints. Default is 5. MethodOffset (0) / sets the function offset value of the call stack. If 0, the stack information is output from the function that prints the Log. The default is 0. HideThreadInfo (); / / hides thread information
.logAdapter (new AndroidLogAdapter ()); / / customize a print adapter, which adapts to Log printing of Android. You can also implement the LogAdapter API to adapt log printing for some special needs.
Custom log display
The thread display is hidden, and the method stack shows an offset of 0, indicating that the methods in the five stacks that count the methods from the printed Log are printed out.
Print object data such as array, List, map, etc.
String [] names = {"Jerry", "Emily", "Xiao Wu", "hongyang", "Seven Cats"}; Logger.d (names); / / print character array List users = new ArrayList (); for (int I = 0; I getName () {return name;} public void setName (String name) {this.name = name) } public int getAge () {return age;} public void setAge (int age) {this.age = age } / / the toString method of the object can be overridden to print out the complete log information @ Override public String toString () {return "User {" + "name='" + name +'/'+ ", age=" + age +'}';}}
Print character array and List (User does not override toString)
Print character array and List (User has overridden toString)
At this point, I believe you have a deeper understanding of "what is the use of Logger?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.