For the Android system log output log package Logutils

Source: Internet
Author: User

Mainactivity as follows:
Package Cc.cn.logutil;import Android.os.bundle;import android.app.activity;/** * Demo Description: * For the Android system log output Log package Logutils * 1 can modify the level value in the logutils to think which levels of logs can be output. * So convenient selective output log or screen log output * 2 output of the log in addition to the output of the message also contains the log output when the thread, class, method name, the number of rows already in the method, etc. * 3 Logutils Call method * 3.1 According to the system in the original way * 3.2 or do not set tag, the default is the file name * * Note Description: * In this example, StackTrace and stacktraceelement are used primarily. * For details, please refer to the previous blog "StackTrace Summary and Stacktraceelement use example" * PS: * Some colds today, some fever. It's a relief to finish 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 ("called Test () in mainactivity"); Testlog.testlog (); new Thread () {public void run () {logutils.i ("called Child Thread");};}. 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 as follows:
Package Cc.cn.logutil;import android.text.textutils;import android.util.log;/** * logutils Tool Description: * 1 logs with output levels greater than or equal to level * Therefore, after development and product release, by modifying the level to selectively output the log.  * All logs are blocked when level=nothing.  * 2 V,d,i,w,e corresponds to two methods. * If tag is not set or tag is blank, set 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 fina l int nothing = 6;public static final int level = Verbose;public static final String SEPARATOR = ",";p ublic 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.c Urrentthread (). 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.cur Rentthread (). 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.curr Entthread (). 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.curr Entthread (). 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.cur Rentthread (). Getstacktrace () [3];if (Textutils.isempty (tag)) {tag = Getdefaulttag (stacktraceelement);} LOG.E (Tag, getloginfo (stacktraceelement) + message);}}  /** * Gets the default tag name.  * For example, the log output is called in Mainactivity.java. * The tag is mainactivity */public static StringGetdefaulttag (stacktraceelement stacktraceelement) {String fileName = Stacktraceelement.getfilename (); String stringarray[] = filename.split ("\ \"); String tag = Stringarray[0];return tag;} /** * Output log contains information */public static String getloginfo (Stacktraceelement stacktraceelement) {StringBuilder Loginfostringbuilder = new StringBuilder ();//Gets the thread name string threadname = Thread.CurrentThread (). GetName ();//Get Thread Idlong ThreadID = Thread.CurrentThread (). GetId ();//Gets the filename. That is, xxx.javastring filename = Stacktraceelement.getfilename ();// Gets the class name. The package name + class name string className = Stacktraceelement.getclassname ();//Gets the method name string methodName = Stacktraceelement.getmethodname ();//Get Birthday output line number 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 as follows:
Package Cc.cn.logutil;public class Testlog {public     static void Testlog () {     logutils.i ("Testlog () in Testlog" is called) );     }}

Main.xml as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "   >    <textview        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:text= "Logutils" android:layout_centerinparent= "true" for the encapsulation of the Android system log output log         /></ Relativelayout>


For the Android system log output log package Logutils

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.