LogUtil 工具類

前言:全局日志管理類,可以將日志打印至SD卡中。

/**
 * @ClassName: LogUtil
 * @Description: 全局日志管理類
 * @author <a href="mailto:turlet@163.com">turlet@163.com</a>
 * @date 2012-2-10 下午9:50:35
 * @version V1.0
 */
public class LogUtil {

    public static final String TAG = LogUtil.class.getSimpleName();

    /**
     * 是否開啟測試模式
     */
    private static boolean debugger = true;

    /**
     * 是否保存到SD卡
     */
    private static boolean saveToSd = false;

    /**
     * 保存LOG日志的目錄
     */
    public static final String SAVE_LOG_DIR_PATH = Environment
            .getExternalStorageDirectory() + "/amp/log";

    /**
     * 保存LOG日志的路徑
     */
    private static String save_log_path = SAVE_LOG_DIR_PATH + "/amp.log";

    public static void d(String tag, String msg) {
        if (debugger) {
            Log.d(tag, msg);
            if (saveToSd && existSD()) {
                storeLog(tag, msg);
            }
        }
    }

    public static void i(String tag, String msg) {
        if (debugger) {
            Log.i(tag, msg);
            if (saveToSd && existSD()) {
                storeLog(tag, msg);
            }
        }
    }

    public static void w(String tag, String msg) {
        if (debugger) {
            Log.w(tag, msg);
            if (saveToSd && existSD()) {
                storeLog(tag, msg);
            }
        }
    }

    public static void e(String tag, String msg) {
        if (debugger) {
            Log.e(tag, msg);
            if (saveToSd && existSD()) {
                storeLog(tag, msg);
            }
        }
    }

    /**
     * 將日志信息保存至SD卡
     * 
     * @param tag
     *            LOG TAG
     * @param msg
     *            保存的打印信息
     */
    public static void storeLog(String tag, String msg) {
        File fileDir = new File(SAVE_LOG_DIR_PATH);
        // 判斷目錄是否已經存在
        if (!fileDir.exists()) {
            if (!fileDir.mkdir()) {
                Log.e(tag, "Failed to create directory " + SAVE_LOG_DIR_PATH);
                return;
            }
        }
        File file = new File(save_log_path);
        // 判斷日志文件是否已經存在
        if (!file.exists()) {
            try {
                if (!file.createNewFile()) {
                    Log.e(tag, "Failed to create log file " + save_log_path);
                    return;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            // 輸出
            FileOutputStream fos = new FileOutputStream(file, true);
            PrintWriter out = new PrintWriter(fos);
            out.println(" --module--" + tag + " " + msg + '\r');
            out.flush();
            out.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean existSD() {
        if (!Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            return false;
        }
        return true;
    }
       @SuppressWarnings("unchecked")
        public static String makeLogTag(Class cls) {
            return "Androidpn_" + cls.getSimpleName();
        }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容