Some logging techniques in android

Source: Internet
Author: User

[Java] view plaincopyprint?
For development work on the android platform, some Log output debugging information is often used.

As we all know, there are five types of android Log, v, d, I, w, e here will not be described (if you do not know these friends, we recommend reading android_Tutor blog http://www.bkjia.com/kf/201205/132161.html, the above is very detailed)

This article describes how to control Log output and shutdown in a unified manner.

Generally, log is output in the debug version, and log output is disabled in the release version. How is this done? Let's look at the code below.

Create a new class file Log. java in your current project. This class file is placed in the current Working Package (you can also create a separate package to store this class file)

In the following class file, we repackage the original 10 log functions in android. Defines a constant DEBUG and outputs Log information only when DEBUG is true.

[Java] package com. android. gallery3d. util;
 
Public class Log {
Private static final boolean DEBUG = true;

Public static void v (String tag, String msg ){
If (DEBUG ){
Android. util. Log. v (tag, msg );
}
}
Public static void v (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. v (tag, msg, tr );
}
}
Public static void d (String tag, String msg ){
If (DEBUG ){
Android. util. Log. d (tag, msg );
}
}
Public static void d (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. d (tag, msg, tr );
}
}
Public static void I (String tag, String msg ){
If (DEBUG ){
Android. util. Log. I (tag, msg );
}
}
Public static void I (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. I (tag, msg, tr );
}
}
Public static void w (String tag, String msg ){
If (DEBUG ){
Android. util. Log. w (tag, msg );
}
}
Public static void w (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. w (tag, msg, tr );
}
}
Public static void w (String tag, Throwable tr ){
If (DEBUG ){
Android. util. Log. w (tag, tr );
}
}
Public static void e (String tag, String msg ){
If (DEBUG ){
Android. util. Log. e (tag, msg );
}
}
Public static void e (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. e (tag, msg, tr );
}
}
}
Package com. android. gallery3d. util;

Public class Log {
Private static final boolean DEBUG = true;

Public static void v (String tag, String msg ){
If (DEBUG ){
Android. util. Log. v (tag, msg );
}
}
Public static void v (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. v (tag, msg, tr );
}
}
Public static void d (String tag, String msg ){
If (DEBUG ){
Android. util. Log. d (tag, msg );
}
}
Public static void d (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. d (tag, msg, tr );
}
}
Public static void I (String tag, String msg ){
If (DEBUG ){
Android. util. Log. I (tag, msg );
}
}
Public static void I (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. I (tag, msg, tr );
}
}
Public static void w (String tag, String msg ){
If (DEBUG ){
Android. util. Log. w (tag, msg );
}
}
Public static void w (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. w (tag, msg, tr );
}
}
Public static void w (String tag, Throwable tr ){
If (DEBUG ){
Android. util. Log. w (tag, tr );
}
}
Public static void e (String tag, String msg ){
If (DEBUG ){
Android. util. Log. e (tag, msg );
}
}
Public static void e (String tag, String msg, Throwable tr ){
If (DEBUG ){
Android. util. Log. e (tag, msg, tr );
}
}
}

 

 

In the current package, Log. v, Log. I, Log. w, Log. e, and Log. d can be used to Log.
It is also easy to Log in other packages, as long as the class name can be imported. The preceding class file is used as an example to import com. android. gallery3d. util. Log in the class file of other packages;

You can use the original Log function to create a Log.

 


Set DEBUG to false in the software version of Release to disable Log output.

 

 

From fulinwsuafcie's column

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.