zoukankan      html  css  js  c++  java
  • Android log 方法

    
    
    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");

  • 相关阅读:
    Java并发编程 LockSupport源码分析
    [No000010F]Git8/9-使用GitHub
    [No000010E]Git7/9-标签管理
    [No000010D]Git6/9-分支管理
    [No000010C]Git5/9-远程仓库
    [No000010B]Git4/9-时光机穿梭
    [No000010A]Git3/9-创建版本库
    [No0000109]Git2/9-安装Git
    [No0000108]Git1/9-Git简介与入门
    [No000011D].NETCORE1/19-.NET Core 指南
  • 原文地址:https://www.cnblogs.com/bigben0123/p/4286467.html
Copyright © 2011-2022 走看看