每一次发版,都要去检查项目中的日至有没有打开,有没有泄露关键信息等等,繁琐的流程让你烦的一批
其实只要开发过程中对log进行封装,发版的时候改变参数就搞定了
一般的情况下都是测试环境下才打印日志,正式环境下就要关闭日志
/**
* Author:AND
* Time: 2019-3-5. 下午 5:37
* Email:2911743255@qq.com
* ClassName: LogUtils
* Description:TODO log工具
*/
public class LogUtils {
private static boolean sIsDebug = BuildConfig.DEBUG;
private static String sPath; // SDCard路径
private static FileOutputStream sOutputStream;
static {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
sPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WriteLogFile/";
File filePath = new File(sPath);
filePath.mkdirs();
File file = new File(sPath, "log.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
sOutputStream = new FileOutputStream(file, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public static void setDebug(boolean debug) {
sIsDebug = debug;
}
public static void d(String log) {
if (sIsDebug) {
Logger.d(log);
}
}
public static void e(String log) {
if (sIsDebug) {
Logger.e(log);
save2Sd(log);
}
}
public static void v(String log) {
if (sIsDebug) {
Logger.v(log);
}
}
public static void i(String log) {
if (sIsDebug) {
Logger.i(log);
}
}
public static void w(String log) {
if (sIsDebug) {
Logger.w(log);
}
}
public static void w(String tag, String log) {
if (sIsDebug) {
Logger.t(tag).w(log);
}
}
/**
* 将错误信息保存到SD卡中去。
*/
public static void save2Sd(String msg) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd : HHmm");
String time = sdf.format(date);
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
if (sOutputStream != null) {
try {
sOutputStream.write(time.getBytes());
sOutputStream.write(msg.getBytes());
sOutputStream.write("\r\n".getBytes());
sOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
使用工具类将日志信息保存到SD卡的指定位置,然后根据自己需求去查找日志
由于我使用的是Logger这个库,所以只需简单的控制一下日志打印的环境就行了,logger库用起来太爽了,功能很丰富,所以无需在工具类中进行特殊的处理
使用BuildConfig.DEBUG进行环境处理,打包无忧