Unified management of Android development logs and unified management of android

Source: Internet
Author: User

Unified management of Android development logs and unified management of android

During development, we usually need to manage the log output in a unified manner. Below we recommend a log output class. In the development stage, we only need to set the DEBUG constant to true, set DEBUG to false in the production environment to control log output. If you don't want to talk about anything, you need to take it directly.

Package com. android. util;/*** unified Log output management ** @ author Qilian Mountains * @ date 2015-04-27 * @ version 1.0 **/public class Log {private static final String TAG = "com. android. app "; private static final boolean DEBUG = true; private static String getFunctionName () {StackTraceElement [] sts = Thread. currentThread (). getStackTrace (); if (sts = null) {return null;} for (StackTraceElement st: sts) {if (st. isNative Method () {continue;} if (st. getClassName (). equals (Thread. class. getName () {continue;} if (st. getClassName (). equals (Log. class. getName () {continue;} return "[" + Thread. currentThread (). getName () + "(" + Thread. currentThread (). getId () + "):" + st. getFileName () + ":" + st. getLineNumber () + "]";} return null;} private static String createMessage (String msg) {String functionName = getFunct IonName (); String message = (functionName = null? Msg: (functionName + "-" + msg); return message;} public static void I (String msg) {if (DEBUG) {String message = createMessage (msg ); android. util. log. I (TAG, message) ;}} public static void v (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. log. v (TAG, message) ;}} public static void d (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. L Og. d (TAG, message) ;}} public static void e (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. log. e (TAG, message) ;}} public static void w (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. log. w (TAG, message) ;}} public static void e (Exception e) {if (DEBUG) {StringBuffer sb = new StringBuffer (); String name = getFunctionName (); StackTrace Element [] sts = e. getStackTrace (); if (name! = Null) {sb. append (name + "-" + e + "\ r \ n");} else {sb. append (e + "\ r \ n");} if (sts! = Null & sts. length> 0) {for (StackTraceElement st: sts) {if (st! = Null) {sb. append ("[" + st. getFileName () + ":" + st. getLineNumber () + "] \ r \ n") ;}} android. util. log. e (TAG, sb. toString ());}}}

 

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.