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(" "); } if (null != tr) { tr.printStackTrace(logStream); if (null != logStream) { logStream.print(" "); } } } 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 } } }
使用:
private static final Logger log = Logger.getLogger("DownloadThread");
log.e("xxxx", "=============network data end");