| | |
| | | |
| | | import android.content.Context; |
| | | import android.os.Environment; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import java.io.BufferedWriter; |
| | |
| | | public class MyLog { |
| | | |
| | | |
| | | |
| | | private static final String TAG = "AYJiaKao"; |
| | | private static Boolean MYLOG_SWITCH = true; // 日志文件总开关 |
| | | |
| | | private static Boolean MYLOG_WRITE_TO_FILE = true;// 日志写入文件开关 |
| | | private static File file; |
| | | |
| | | // 输入日志类型,w代表只输出告警信息等,v代表输出所有信息 |
| | | private static char MYLOG_TYPE = 'v'; |
| | | |
| | | // 日志文件在sdcard中的路径 |
| | | private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/nvjiakaolog"; |
| | | private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/JiaKaolog"; |
| | | |
| | | private static int SDCARD_LOG_FILE_SAVE_DAYS = 3;// sd卡中日志文件的最多保存天数 |
| | | private static int SDCARD_LOG_FILE_SAVE_DAYS = 2;// sd卡中日志文件的最多保存天数 |
| | | |
| | | private static String MYLOGFILEName = "Log.txt";// 本类输出的日志文件名称 |
| | | |
| | |
| | | public Context context; |
| | | |
| | | |
| | | |
| | | public static void w(String tag, Object msg) { // 警告信息 |
| | | |
| | | log(tag, msg.toString(), 'w'); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | public static void e(String tag, Object msg) { // 错误信息 |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | 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 i(Object msg) {// |
| | | |
| | | log(TAG, msg.toString(), 'i'); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | public static void v(String tag, Object msg) { |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | public static void w(String tag, String text) { |
| | | |
| | | log(tag, text, 'w'); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | public static void e(String tag, String text) { |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | 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 i( String text) { |
| | | |
| | | public static void i(String text) { |
| | | |
| | | log(TAG, text, 'i'); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | public static void v(String tag, String text) { |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | |
| | | * 根据tag, msg和等级,输出日志 |
| | | |
| | | * |
| | | * @param tag |
| | | |
| | | * @param msg |
| | | |
| | | * @param level |
| | | |
| | | */ |
| | | |
| | | private static void log(String tag, String msg, char level) { |
| | | private static void log(final String tag, final String msg, final char level) { |
| | | |
| | | if (MYLOG_SWITCH) {//日志文件总开关 |
| | | |
| | |
| | | } |
| | | |
| | | if (MYLOG_WRITE_TO_FILE)//日志写入文件开关 |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | writeLogtoFile(String.valueOf(level), tag, msg); |
| | | } |
| | | } |
| | | ).start(); |
| | | |
| | | writeLogtoFile(String.valueOf(level), tag, msg); |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | public static void createIfNotExist() { |
| | | |
| | | |
| | | delFile(); |
| | | |
| | | |
| | | String needWriteFiel = logfile.format(System.currentTimeMillis()); |
| | | |
| | | |
| | | Log.i(TAG, "createIfNotExist: " + needWriteFiel); |
| | | |
| | | |
| | | File dirsFile = new File(MYLOG_PATH_SDCARD_DIR); |
| | | |
| | | if (!dirsFile.exists()) { |
| | | |
| | | dirsFile.mkdirs(); |
| | | |
| | | } |
| | | |
| | | //Log.i("创建文件","创建文件"); |
| | | |
| | | file = new File(dirsFile.toString(), needWriteFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR |
| | | |
| | | if (!file.exists()) { |
| | | |
| | | try { |
| | | |
| | | //在指定的文件夹中创建文件 |
| | | |
| | | file.createNewFile(); |
| | | |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | |
| | | * 打开日志文件并写入日志 |
| | | |
| | | * |
| | | * @param mylogtype |
| | | |
| | | * @param tag |
| | | |
| | | * @param text |
| | | |
| | | */ |
| | | |
| | | private static void writeLogtoFile(String mylogtype, final String tag, final String text) {// 新建或打开日志文件 |
| | | |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | delFile(); |
| | | Date nowtime = new Date(); |
| | | |
| | | String needWriteFiel = logfile.format(nowtime); |
| | | |
| | | String needWriteMessage = myLogSdf.format(nowtime) + " "+ tag + " " + text; |
| | | |
| | | |
| | | |
| | | |
| | | File dirsFile = new File(MYLOG_PATH_SDCARD_DIR); |
| | | |
| | | if (!dirsFile.exists()){ |
| | | |
| | | dirsFile.mkdirs(); |
| | | |
| | | } |
| | | |
| | | //Log.i("创建文件","创建文件"); |
| | | |
| | | File file = new File(dirsFile.toString(), needWriteFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR |
| | | |
| | | if (!file.exists()) { |
| | | |
| | | try { |
| | | |
| | | //在指定的文件夹中创建文件 |
| | | |
| | | file.createNewFile(); |
| | | |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | |
| | | } |
| | | String needWriteMessage = myLogSdf.format(System.currentTimeMillis()) + " " + tag + " " + text; |
| | | |
| | | |
| | | |
| | | try { |
| | | try { |
| | | /**后面这个参数代表是不是要接上文件中原来的数据,不进行覆盖**/ |
| | | FileWriter filerWriter = new FileWriter(file, true); |
| | | |
| | | BufferedWriter bufWriter = new BufferedWriter(filerWriter); |
| | | |
| | | bufWriter.write(needWriteMessage); |
| | | |
| | | bufWriter.newLine(); |
| | | |
| | | bufWriter.close(); |
| | | |
| | | filerWriter.close(); |
| | | |
| | | } catch (IOException e) { |
| | | |
| | | e.printStackTrace(); |
| | | |
| | | } |
| | | if (file == null) { |
| | | Log.i(TAG, "writeLogtoFile: file==null"); |
| | | createIfNotExist(); |
| | | } |
| | | }).start(); |
| | | FileWriter filerWriter = new FileWriter(file, true); |
| | | |
| | | BufferedWriter bufWriter = new BufferedWriter(filerWriter); |
| | | |
| | | bufWriter.write(needWriteMessage); |
| | | |
| | | bufWriter.newLine(); |
| | | |
| | | bufWriter.close(); |
| | | |
| | | filerWriter.close(); |
| | | |
| | | } catch (IOException e) { |
| | | |
| | | e.printStackTrace(); |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | |
| | | * 删除制定的日志文件 |
| | | |
| | | */ |
| | | |
| | | public static void delFile() {// 删除日志文件 |
| | | |
| | | String needDelFiel = logfile.format(getDateBefore()); |
| | | |
| | | String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/nvlog"; |
| | | String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JiaKaolog"; |
| | | |
| | | File file = new File(dirPath, needDelFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | |
| | | * 得到现在时间前的几天日期,用来得到需要删除的日志文件名 |
| | | |
| | | */ |
| | | |
| | | private static Date getDateBefore() { |