Android Development Tips Two: the implementation of the custom logging tool class Xlog

Source: Internet
Author: User

During the development process, the print log is an essential debugging link, however, directly using the system's own log log class, does not meet the needs of our actual project: If we are now in the development of a relatively large project, used to print the log is definitely a lot of places, When the real on-line, we do not want the log can still print out, the reason is very simple, not only some of the impact of efficiency, more likely to lead to the disclosure of information, so we need to all the log print statements are canceled, we really one to remove these print log statements? Project so big, just want to delete, also have to spend a lot of time and energy! We do not have to be afraid, the author of this gives you a control of the online version and offline version of the log Printing tool class XLog, through the tool, we can only need to change a statement, you can control whether the log can print output, Then put the code for the entire tool class first:


Other development tips:

Android Development Tip One: in-depth understanding of layout_weight properties in Android layouts

Android Development Tips Two: the implementation of the custom logging tool class Xlog

Android Development Skills III: Activation mode of activity

Android Development Tips Four: share a toolkit to quickly switch between Android UI threads and a toolkit for other background threads


Public final class XLog {private static boolean logable;private static int loglevel;public static enum logpriority {Verbos E, DEBUG, INFO, WARN, ERROR, assert}/** * with LOG.V * * @param tag * @param msg * @return */public static int V (String tag, String msg) {return println (logpriority.verbose, tag, msg);} /** * Same as LOG.D * * @param tag * @param msg * @return */public static int d (string tag, string msg) {return println (logprior ity. DEBUG, tag, msg);} /** * Same as LOG.I * * @param tag * @param msg * @return */public static int i (string tag, string msg) {return println (logprior Ity.info, tag, msg);} /** * Same as LOG.W * * @param tag * @param msg * @return */public static int W (string tag, string msg) {return println (logprior ity. WARN, tag, msg);} /** * Same as LOG.E * * @param tag * @param msg * @return */public static int E (string tag, string msg) {return println (logprior ity. ERROR, tag, msg);} /** * Gets whether the log can be printed * * @return */public static Boolean islogable () {return logable;} /** * Set whether to print the log * * @param logable */pubLic static void Setlogable (Boolean logable) {xlog.logable = logable;} /** * Set Log rank * * @param levels */public static void Setloglevel (Logpriority level) {xlog.loglevel = Level.ordinal ();} private static int println (Logpriority priority, string tag, String msg) {Int. level = Priority.ordinal (); if (Logable & & LogLevel <= level) {return log.println (level + 2, tag, msg);} else {return-1;}}}

Next, let's analyze the implementation principle of the tool class:

The first step: first define a variable that prints the log and the priority of the print log

Step two: Implement static methods of five print logs according to priority level

Step three: Set how to get a printable log

The fourth step: we just need to set the custom application inside whether or not to print it, tightly just pass a Boolean value.

is not very simple, for this log tool class there is another way to implement, and more than this is more simple, the author will be in the next time another log tool class to fill up, thank you!




Android Development Tips Two: the implementation of the custom logging tool class Xlog

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.