package com.example.myapplication.utils; /** * Created by 徐嘉健 on 2018/11/2. */ import android.app.Activity; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.util.Log; import com.example.myapplication.app.MyApp; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 將日志寫入進SD卡 */ public class MyLog { private static Boolean MYLOG_SWITCH = true; // 日志文件总开关 private static Boolean MYLOG_WRITE_TO_FILE = true;// 日志写入文件开关 private static char MYLOG_TYPE = 'v';// 输入日志类型,w代表只输出告警信息等,v代表输出所有信息 private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/DCIM/";// 日志文件在sdcard中的路径 private static int SDCARD_LOG_FILE_SAVE_DAYS = 7 * 24 * 60 * 60 * 1000;// sd卡中日志文件的最多保存天数 private static String MYLOGFILEName = "xjj.txt";// 本类输出的日志文件名称 private static SimpleDateFormat myLogSdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");// 日志的输出格式 private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式 public static void w(String tag, Object msg) { // 警告信息 log(tag, msg.toString(), 'w'); } public static void e(String tag, Object msg) { // 错误信息 log(tag, msg.toString(), 'e'); } public static void d(String tag, Object msg) {// 调试信息 log(tag, msg.toString(), 'd'); } public static void i(String tag, Object msg) {// log(tag, msg.toString(), 'i'); } public static void v(String tag, Object msg) { log(tag, msg.toString(), 'v'); } public static void w(String tag, String text) { log(tag, text, 'w'); } public static void e(String tag, String text) { log(tag, text, 'e'); } public static void d(String tag, String text) { log(tag, text, 'd'); } public static void i(String tag, String text) { log(tag, text, 'i'); } public static void v(String tag, String text) { log(tag, text, 'v'); } private static void log(String tag, String msg, char level) { //是否開啓日志 if (MyApp.isDebug) { if ('e' == level && ('e' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { // 输出错误信息 Log.e(tag, msg); } else if ('w' == level && ('w' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { Log.w(tag, msg); } else if ('d' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { Log.d(tag, msg); } else if ('i' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { Log.i(tag, msg); } else { Log.v(tag, msg); } if (MYLOG_WRITE_TO_FILE){ writeLogtoFile(String.valueOf(level), tag, msg); } } } private static void writeLogtoFile(String mylogtype, String tag, String text) {// 新建或打开日志文件 Date nowtime = new Date(); String needWriteFiel = logfile.format(nowtime); String needWriteMessage = myLogSdf.format(nowtime) + " " + mylogtype + " " + tag + " " + text; try { File file = new File(MYLOG_PATH_SDCARD_DIR, needWriteFiel + MYLOGFILEName); FileWriter filerWriter = new FileWriter(file, true);//后面这个参数代表是不是要接上文件中原来的数据,不进行覆盖 BufferedWriter bufWriter = new BufferedWriter(filerWriter); bufWriter.write(needWriteMessage); bufWriter.newLine(); bufWriter.close(); filerWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void delFile() {// 删除日志文件 String needDelFiel = logfile.format(getDateBefore()); File file = new File(MYLOG_PATH_SDCARD_DIR, needDelFiel + MYLOGFILEName); if (file.exists()) { file.delete(); } } private static Date getDateBefore() { Date nowtime = new Date(); Calendar now = Calendar.getInstance(); now.setTime(nowtime); now.set(Calendar.DATE, now.get(Calendar.DATE) - SDCARD_LOG_FILE_SAVE_DAYS); return now.getTime(); } /** * <pre> * 判断date和当前日期是否在同一周内 * 注: * Calendar类提供了一个获取日期在所属年份中是第几周的方法,对于上一年末的某一天 * 和新年初的某一天在同一周内也一样可以处理,例如2012-12-31和2013-01-01虽然在 * 不同的年份中,但是使用此方法依然判断二者属于同一周内 * </pre> * * @param date * @return */ public static boolean isSameWeekWithToday(Date date) { if (date == null) { return false; } // 0.先把Date类型的对象转换Calendar类型的对象 Calendar todayCal = Calendar.getInstance(); Calendar dateCal = Calendar.getInstance(); todayCal.setTime(new Date()); dateCal.setTime(date); // 1.比较当前日期在年份中的周数是否相同 if (todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR)) { return true; } else { return false; } } }
将日志打入到SD卡
最新推荐文章于 2023-06-19 14:58:11 发布