Anroid 工具类 —— 日志

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

//这个类可以让maven根据dev或prod自动生成,里面只有一个isDebugEnabled变量
import com.newland.mtype.common.RunningModel;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

/**
 * 日志工厂

*/ public class DeviceLoggerFactory { enum Level { Debug, Info, Warn, Error } public static final byte NONE = 0x00; public static final byte CONSOLE = 0x01; public static final byte BROADCAST = 0x02; public static final byte FILE = 0x04; public static final byte ALL = (byte) 0xFF; public static final String ACTION_LOG = "LOG"; public static final String EXTRA_LEVEL = "LEVEL"; public static final String EXTRA_TAG = "TAG"; public static final String EXTRA_MSG = "MSG"; private static final SimpleDateFormat SDF_DATE = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault()); private static final SimpleDateFormat SDF_TIME = new SimpleDateFormat("hh:mm:ss.SSS", Locale.getDefault()); private static Context context; private static byte logType = CONSOLE; private static File file = null; public static void init(Context context, byte logType) { DeviceLoggerFactory.context = context; DeviceLoggerFactory.logType = logType; } private static void initFile() { //这里的path无论有没有申明权限都能拿到,通常值为 "/storage/emulated/0" if(file != null && file.exists()) return; String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath(); String logDir = sdPath + "/newland/log/"; String fileName = SDF_DATE.format(new Date()) + ".txt"; File dir = new File(logDir); //当路径不存在时,必须调用mkdirs主动创建,BufferedWriter和OutputStreamWriter都不会帮我们创建目录 if(!dir.exists()) { dir.mkdirs(); } file = new File(logDir + fileName); if(!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); file = null; } } } public static DeviceLogger getLogger(final String tag) { return new DeviceLogger() { private void recordLog(Level level, String msg) { //debug的日志只有在允许打印时,才会打印。其它日志是无论什么情况都打印 if(logType == NONE) return; if(RunningModel.isDebugEnabled && level == Level.Debug || level != Level.Debug) { if((logType & BROADCAST) != 0 && context != null) { Intent intent = new Intent(ACTION_LOG); //由于extra只能存放一些基本变量,因此需要将枚举类转为string intent.putExtra(EXTRA_LEVEL, level.toString()); intent.putExtra(EXTRA_TAG, tag); intent.putExtra(EXTRA_MSG, msg); context.sendBroadcast(intent); } if((logType & FILE) != 0) { initFile(); if(file != null) { try { //FileOutputStream的第二个参数表示是否append(如果文件存在,是追加还是清空重写) BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true))); bw.write(level + " " + SDF_TIME.format(new Date()) + " " + tag + " " + msg + "\r\n"); //close方法自带flush,所以前面就不用多此一举的去flush了 bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } if((logType & CONSOLE) != 0) { switch(level) { case Debug: Log.d(tag, msg); break; case Info: Log.i(tag, msg); break; case Warn: Log.w(tag, msg); break; case Error: Log.e(tag, msg); break; } } } } private void recordLog(Level level, String msg, Throwable e) { recordLog(level, msg + "\r\n\t" + e.getMessage()); } @Override public void debug(String msg) { recordLog(Level.Debug, msg); } @Override public void debug(String msg, Throwable e) { recordLog(Level.Debug, msg, e); } @Override public void info(String msg) { recordLog(Level.Info, msg); } @Override public void info(String msg, Throwable e) { recordLog(Level.Info, msg, e); } @Override public void warn(String msg) { recordLog(Level.Warn, msg); } @Override public void warn(String msg, Throwable e) { recordLog(Level.Warn, msg, e); } @Override public void error(String msg) { recordLog(Level.Error, msg); } @Override public void error(String msg, Throwable e) { recordLog(Level.Error, msg, e); } }; } }

你可能感兴趣的:(——,Android,工具类)