Android Log method

Source: Internet
Author: User
Tags date now

Package test;

Public Abstract classLogger {Private Staticclass<?extendsLogger> Mloggerclass =NULL; Public Static Final BooleanDBG =true; Public Static FinalString TAG =NULL; Public Static FinalString line = "------->"; PrivateString Mtag; PublicLogger () {} PublicLogger (String tag) { This. Mtag =tag; } Public Static voidRegisterlogger (class<?extendsLogger>Loggerclass) {Logger.mloggerclass=Loggerclass; } Public Static voidUnregisterlogger () {Logger.mloggerclass=NULL; } Public StaticLogger GetLogger (String tag) {tag= "[" + Tag + "]"; Logger Logger=NULL; if(Mloggerclass! =NULL) { Try{Logger=mloggerclass.newinstance (); Logger.mtag=tag; } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } } if(Logger = =NULL) {Logger=NewLog (tag); } returnlogger; } PublicString Gettag () {returnMtag; } Public voidd (string tag, string str) {Debug (Mtag+ Line +str); } Public voidI (string tag, string str) {info (Mtag+ Line +str); } Public voidW (string tag, string str) {warn (Mtag+ Line +str); } Public voide (string tag, string str) {error (Mtag+ Line +str); } Public voidd (string tag, string str, Throwable tr) {debug (Mtag+ Line +str, TR); } Public voidI (string tag, string str, Throwable tr) {info (Mtag+ Line +str, TR); } Public voidW (string tag, string str, Throwable tr) {warn (Mtag+ Line +str, TR); } Public voide (string tag, string str, Throwable tr) {error (Mtag+ Line +str, TR); } protected Abstract voidDebug (String str); protected Abstract voidinfo (String str); protected Abstract voidwarn (String str); protected Abstract voiderror (String str); protected Abstract voidDebug (String str, throwable tr); protected Abstract voidinfo (String str, throwable tr); protected Abstract voidwarn (String str, throwable tr); protected Abstract voiderror (String str, throwable tr);}
 Packagetest;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.PrintStream;Importjava.util.Date;Importandroid.os.Environment; Public classLogextendsLogger {Private Static FinalString App_tag = "Renwuto"; Private Static FinalString log_file_name = "Renwuto.txt"; Private StaticPrintStream LogStream; Private Static FinalString Log_entry_format = "[%tf%tt]%s";  PublicLog (String name) {Super(name); } @Overrideprotected voidDebug (String str) {ANDROID.UTIL.LOG.D (App_tag, str); Write (str,NULL); } @Overrideprotected voiderror (String str) {ANDROID.UTIL.LOG.E (App_tag, str); Write (str,NULL); } @Overrideprotected voidinfo (String str) {ANDROID.UTIL.LOG.I (App_tag, str); Write (str,NULL); } @Overrideprotected voidwarn (String str) {ANDROID.UTIL.LOG.W (App_tag, str); Write (str,NULL); } @Overrideprotected voidDebug (String str, throwable tr) {ANDROID.UTIL.LOG.D (App_tag, str);    Write (str, TR); } @Overrideprotected voiderror (String str, throwable tr) {ANDROID.UTIL.LOG.E (App_tag, str);    Write (str, TR); } @Overrideprotected voidinfo (String str, throwable tr) {android.util.log.i (App_tag, str);    Write (str, TR); } @Overrideprotected voidwarn (String str, throwable tr) {ANDROID.UTIL.LOG.W (App_tag, str);    Write (str, TR); }    Private voidWrite (String msg, throwable tr) {if(!log.dbg) {return; }        Try {            if(NULL==LogStream) {                synchronized(Log.class) {                    if(NULL==LogStream)                    {init (); }}} Date now=NewDate (); if(NULL!=LogStream)                {logstream.printf (Log_entry_format, now, now, MSG); Logstream.print ("\ n"); }            if(NULL!=TR)                {tr.printstacktrace (LogStream); if(NULL!=LogStream) {Logstream.print ("\ n"); }            }        } Catch(Throwable t) {//Empty Catch block        }    }     Public Static voidinit () {if(!log.dbg) {return; }        Try{File Sdroot=NULL; String State=environment.getexternalstoragestate (); if(Environment.MEDIA_MOUNTED.equals (state)) {Sdroot=environment.getexternalstoragedirectory (); }            if(Sdroot! =NULL) {File logFile=NewFile (Sdroot, log_file_name); ANDROID.UTIL.LOG.D (App_tag,"Log to File:" +logFile); LogStream=NewPrintStream (NewFileOutputStream (LogFile,true),true); }        } Catch(Throwable e) {//Empty Catch block}} @Overrideprotected voidFinalize ()throwsThrowable {Try {            Super. Finalize (); if(LogStream! =NULL) {logstream.close (); }        } Catch(Throwable t) {//Empty Catch block        }    }}

Android Log method

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.