Log 打印工具类

    xiaoxiao2021-03-25  67

    package com.xxx.patchgen.utils; import java.io.File; import java.io.FileWriter; import java.util.Calendar; public class LogPrinter { private static StringBuilder Logs = new StringBuilder(); private static boolean hasError = false; public static void resetLogger(){ hasError = false; Logs.delete(0, Logs.length()); } public static void info(int level,String log){ if (hasError) return; String base = ""; for (int i = 0; i < level; i++) { base+=" "; } Logs.append(base+log+"\n"); } public static void error(String log){ if (hasError) return; Logs.append("\n"+log+"\n"); //writeLog(); //System.exit(1); hasError = true; } public static void writeLog(String fileName,String filePath){ stringToFile(Logs.toString(), filePath, "["+getTime()+"]-"+fileName); } private static String getTime(){ Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH)+1; int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int min = calendar.get(Calendar.MINUTE); int sec = calendar.get(Calendar.SECOND); return Integer.valueOf(year+""+(month<10?"0"+month:month)+(day<10?"0"+day:day))+"."+ Integer.valueOf(hour+""+(min<10?"0"+min:min)+(sec<10?"0"+sec:sec)); } public static boolean hasError() { return hasError; } public static String getLog(){ return Logs.toString(); } public static void stringToFile(String content, String filePath, String fileName) { try{ File sqlFilePath = new File(filePath); if(!sqlFilePath.exists()){ sqlFilePath.mkdirs(); } File file = new File(sqlFilePath.getAbsolutePath()+"/"+fileName); if (!file.exists()){ file.createNewFile(); } FileWriter fileWriter = new FileWriter(file); fileWriter.append(content+"\n\n\n"); fileWriter.close(); }catch (Exception e){ e.printStackTrace(); } } }
    转载请注明原文地址: https://ju.6miu.com/read-15532.html

    最新回复(0)