Android development skills 2: Implementation of custom log tool XLog, Android xlog

Source: Internet
Author: User

Android development skills 2: Implementation of custom log tool XLog, Android xlog

During the development process, printing logs is an essential part of debugging. However, directly using the built-in Log class does not meet our actual project requirements: if we are developing a relatively large project, there must be a lot of logs to be printed. When we get online, we don't want the logs to be printed, the reason is very simple. It may not only affect the efficiency, but also lead to information leakage. Therefore, we need to cancel all the log printing statements, do we delete these log printing statements one by one? The project is so large that it takes a lot of time and effort to delete it! Don't worry, I will bring you an XLog tool for log printing on and off the control line. With this tool, we only need to change one statement, to control whether logs can be printed and output, paste the code of the entire tool class first:


Reprinted please indicate the source: http://blog.csdn.net/u011131296/article/details/41822589


Other development skills:

Android development tip 1: gain an in-depth understanding of Layout_weight attributes in the Android Layout

Android development Tip 2: Implementation of custom log tool XLog

Android development Tip 3: Activity Startup Mode

Android development Tip 4: Share a toolkit to quickly switch between Android UI threads and other background threads


Public final class XLog {private static boolean logable; private static int logLevel; public static enum LogPriority {VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT}/*** is the same as 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 (LogPriority. DEBUG, tag, msg);}/*** same as Log. I ** @ param tag * @ param msg * @ return */public static int I (String tag, String msg) {return println (LogPriority. INFO, tag, msg);}/*** same as Log. w ** @ param tag * @ param msg * @ return */public static int w (String tag, String msg) {return println (LogPriority. WARN, tag, msg);}/*** same as Log. e ** @ param tag * @ param msg * @ return */public static int e (String tag, String msg) {return println (LogPriority. ERROR, tag, msg);}/*** get whether to print logs ** @ return */public static boolean isLogable () {return logable ;} /*** set whether to print logs ** @ param logable */public static void setLogable (boolean logable) {XLog. logable = logable;}/*** set the Log level ** @ param level */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 principles of this tool class:

Step 1: first define a variable that can print logs and the priority of printing logs

Step 2: implement five static log printing methods based on the priority

Step 3: Set the method for obtaining printable logs

Step 4: you only need to set whether to print in the custom Application, and pass a Boolean value tightly.

Isn't it easy? There is another implementation method for this log tool class, which is simpler than this one. I will add another log tool class in the next time. Thank you!




Related Article

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.