Android log 方法

來源:互聯網
上載者:User

標籤:


package test;

public abstract class Logger { private static Class<? extends Logger> mLoggerClass = null; public static final boolean DBG = true; public static final String TAG = null; public static final String LINE = "------->"; private String mTag; public Logger() { } public Logger(String tag) { this.mTag = tag; } public static void registerLogger(Class<? extends Logger> loggerClass) { Logger.mLoggerClass = loggerClass; } public static void unregisterLogger() { Logger.mLoggerClass = null; } public static Logger 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 = new Log(tag); } return logger; } public String getTag() { return mTag; } public void d(String tag, String str) { debug(mTag + LINE + str); } public void i(String tag, String str) { info(mTag + LINE + str); } public void w(String tag, String str) { warn(mTag + LINE + str); } public void e(String tag, String str) { error(mTag + LINE + str); } public void d(String tag, String str, Throwable tr) { debug(mTag + LINE + str, tr); } public void i(String tag, String str, Throwable tr) { info(mTag + LINE + str, tr); } public void w(String tag, String str, Throwable tr) { warn(mTag + LINE + str, tr); } public void e(String tag, String str, Throwable tr) { error(mTag + LINE + str, tr); } protected abstract void debug(String str); protected abstract void info(String str); protected abstract void warn(String str); protected abstract void error(String str); protected abstract void debug(String str, Throwable tr); protected abstract void info(String str, Throwable tr); protected abstract void warn(String str, Throwable tr); protected abstract void error(String str, Throwable tr);}
package test;import java.io.File;import java.io.FileOutputStream;import java.io.PrintStream;import java.util.Date;import android.os.Environment;public class Log extends Logger {    private static final String APP_TAG = "renwuto";    private static final String LOG_FILE_NAME = "renwuto.txt";    private static PrintStream logStream;    private static final String LOG_ENTRY_FORMAT = "[%tF %tT]%s";    public Log(String name) {        super(name);    }    @Override    protected void debug(String str) {        android.util.Log.d(APP_TAG, str);        write(str, null);    }    @Override    protected void error(String str) {        android.util.Log.e(APP_TAG, str);        write(str, null);    }    @Override    protected void info(String str) {        android.util.Log.i(APP_TAG, str);        write(str, null);    }    @Override    protected void warn(String str) {        android.util.Log.w(APP_TAG, str);        write(str, null);    }    @Override    protected void debug(String str, Throwable tr) {        android.util.Log.d(APP_TAG, str);        write(str, tr);    }    @Override    protected void error(String str, Throwable tr) {        android.util.Log.e(APP_TAG, str);        write(str, tr);    }    @Override    protected void info(String str, Throwable tr) {        android.util.Log.i(APP_TAG, str);        write(str, tr);    }    @Override    protected void warn(String str, Throwable tr) {        android.util.Log.w(APP_TAG, str);        write(str, tr);    }    private void write(String msg, Throwable tr) {        if (!Log.DBG) {            return;        }        try {            if (null == logStream) {                synchronized (Log.class) {                    if (null == logStream) {                        init();                    }                }            }            Date now = new Date();            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 void init() {        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 = new File(sdRoot, LOG_FILE_NAME);                android.util.Log.d(APP_TAG, "Log to file : " + logFile);                logStream = new PrintStream(new FileOutputStream(logFile, true), true);            }        } catch (Throwable e) {            // Empty catch block        }    }    @Override    protected void finalize() throws Throwable {        try {            super.finalize();            if (logStream != null) {                logStream.close();            }        } catch (Throwable t) {            // Empty catch block        }    }}

 

Android log 方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.