Encapsulation of Log output logs of Android system LogUtils

Source: Internet
Author: User

Encapsulation of Log output logs of Android system LogUtils
MainActivity is as follows:

Package cc.cn. logutil; import android. OS. bundle; import android. app. activity;/*** Demo Description: * encapsulation of Log output logs of the Android system LogUtils ** 1. You can modify the LEVEL values in LogUtils to determine which levels of logs can be output. * It is convenient to output logs selectively or shield logs from output. * 2 the output logs include the thread, class, and method name of the log output, practical information such as the number of rows already in this method * 3 LogUtils call Method * 3.1 According to the system's original method * 3.2 or no TAG is set, the default is the file name ** remarks: * StackTrace and StackTraceElement are used in this example. * For details, see the StackTrace overview and StackTraceElement instance in the previous blog. ** PS: * today, I have a cold and fever. it is a consolation to complete this example after work * September 23, 2014 23:05:01. */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); LogUtils. I ("MainActivity", "onCreate1"); LogUtils. I (null, "onCreate2"); LogUtils. I ("", "onCreate3"); LogUtils. I ("onCreate4"); test ();} private void test () {LogUtils. I ("test () in MainActivity called"); TestLog. testLog (); new Thread () {public void run () {LogUtils. I ("subthread called ");};}. start () ;}@ Overrideprotected void onStart () {super. onStart (); LogUtils. I ("onStart") ;}@ Overrideprotected void onResume () {super. onResume (); LogUtils. I ("onResume") ;}@ Overrideprotected void onPause () {super. onPause (); LogUtils. I ("onPause") ;}@ Overrideprotected void onStop () {super. onStop (); LogUtils. I ("onStop") ;}@ Overrideprotected void onDestroy () {super. onDestroy (); LogUtils. I ("onDestroy ");}}

LogUtils is as follows:
Package cc.cn. logutil; import android. text. textUtils; import android. util. log;/*** LogUtils tool Description: * 1 only outputs logs with a LEVEL greater than or equal to the LEVEL *. Therefore, after development and product release, you can modify the LEVEL to output logs selectively. * When LEVEL = NOTHING, all logs are blocked. * 2 v, d, I, w, and e correspond to two methods. * If the TAG is not set or the TAG is empty, set the default TAG. **/public class LogUtils {public static final int VERBOSE = 1; public static final int DEBUG = 2; public static final int INFO = 3; public static final int WARN = 4; public static final int ERROR = 5; public static final int NOTHING = 6; public static final int LEVEL = VERBOSE; public static final String SEPARATOR = ","; public static void v (String message) {if (LEVEL <= VERBOSE) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; String tag = getDefaultTag (stackTraceElement); Log. v (tag, getLogInfo (stackTraceElement) + message) ;}} public static void v (String tag, String message) {if (LEVEL <= VERBOSE) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; if (TextUtils. isEmpty (tag) {tag = getDefaultTag (stackTraceElement);} Log. v (tag, getLogInfo (stackTraceElement) + message) ;}} public static void d (String message) {if (LEVEL <= DEBUG) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; String tag = getDefaultTag (stackTraceElement); Log. d (tag, getLogInfo (stackTraceElement) + message) ;}} public static void d (String tag, String message) {if (LEVEL <= DEBUG) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; if (TextUtils. isEmpty (tag) {tag = getDefaultTag (stackTraceElement);} Log. d (tag, getLogInfo (stackTraceElement) + message) ;}} public static void I (String message) {if (LEVEL <= INFO) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; String tag = getDefaultTag (stackTraceElement); Log. I (tag, getLogInfo (stackTraceElement) + message) ;}} public static void I (String tag, String message) {if (LEVEL <= INFO) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; if (TextUtils. isEmpty (tag) {tag = getDefaultTag (stackTraceElement);} Log. I (tag, getLogInfo (stackTraceElement) + message) ;}} public static void w (String message) {if (LEVEL <= WARN) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; String tag = getDefaultTag (stackTraceElement); Log. w (tag, getLogInfo (stackTraceElement) + message) ;}} public static void w (String tag, String message) {if (LEVEL <= WARN) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; if (TextUtils. isEmpty (tag) {tag = getDefaultTag (stackTraceElement);} Log. w (tag, getLogInfo (stackTraceElement) + message) ;}} public static void e (String tag, String message) {if (LEVEL <= ERROR) {StackTraceElement stackTraceElement = Thread. currentThread (). getStackTrace () [3]; if (TextUtils. isEmpty (tag) {tag = getDefaultTag (stackTraceElement);} Log. e (tag, getLogInfo (stackTraceElement) + message) ;}/ *** get the default TAG name. * For example, In MainActivity. the log output is called in java. * The TAG is MainActivity */public static String getDefaultTag (StackTraceElement stackTraceElement) {String fileName = stackTraceElement. getFileName (); String stringArray [] = fileName. split ("\\. "); String tag = stringArray [0]; return tag;}/*** output log information */public static String getLogInfo (StackTraceElement stackTraceElement) {StringBuilder logInfoStringBuilder = new StringBuilder (); // obtain the Thread name String threadName = Thread. currentThread (). getName (); // get the Thread IDlong threadID = Thread. currentThread (). getId (); // get the file name. xxx. javaString fileName = stackTraceElement. getFileName (); // get the class name. that is, package name + class name String className = stackTraceElement. getClassName (); // obtain the method name String methodName = stackTraceElement. getMethodName (); // get the number of output rows for the birthday int lineNumber = stackTraceElement. getLineNumber (); logInfoStringBuilder. append ("["); logInfoStringBuilder. append ("threadID =" + threadID ). append (SEPARATOR); logInfoStringBuilder. append ("threadName =" + threadName ). append (SEPARATOR); logInfoStringBuilder. append ("fileName =" + fileName ). append (SEPARATOR); logInfoStringBuilder. append ("className =" + className ). append (SEPARATOR); logInfoStringBuilder. append ("methodName =" + methodName ). append (SEPARATOR); logInfoStringBuilder. append ("lineNumber =" + lineNumber); logInfoStringBuilder. append ("]"); return logInfoStringBuilder. toString ();}}

TestLog is as follows:
Package cc.cn. logutil; public class TestLog {public static void testLog () {LogUtils. I ("TestLog () in testLog is called ()");}}

Main. xml is as follows:
     
  
 


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.