From 4cc772228321b046713da9f3576c23ede1804f5c Mon Sep 17 00:00:00 2001 From: "youhong.ai" Date: Sat, 10 Jun 2023 17:49:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=81=E5=85=94=E6=97=A5=E5=BF=97=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=B8=8A=E6=9F=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../selfdev/util/log/LogTool.java | 111 ++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/main/youhong_ai_jitu_src/selfdev/util/log/LogTool.java diff --git a/.gitignore b/.gitignore index 4be73d9..97caa67 100644 --- a/.gitignore +++ b/.gitignore @@ -48,6 +48,7 @@ src/test/resources/font src/main/resources/WEB-INF/vm/outFile target/ *.back +!/src/main/youhong_ai_jitu_src/selfdev/util/log # 老项目代码 #/lib/jitulib/ #/lib/classbean diff --git a/src/main/youhong_ai_jitu_src/selfdev/util/log/LogTool.java b/src/main/youhong_ai_jitu_src/selfdev/util/log/LogTool.java new file mode 100644 index 0000000..22a8a3b --- /dev/null +++ b/src/main/youhong_ai_jitu_src/selfdev/util/log/LogTool.java @@ -0,0 +1,111 @@ +package selfdev.util.log; + +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.IOException; +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; + +import weaver.general.BaseBean; +import weaver.general.GCONST; + +/** + * 日志工具类 + * @author KangMD + * 1、2019-01-29 add by KangMD + */ + +public class LogTool { + private BufferedWriter logPrint; + private String logFile = ""; + private String logPath=""; + private boolean systemlog=false;//是否写系统日记 + static SimpleDateFormat newDf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + static SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd"); + static BaseBean log=new BaseBean(); + public LogTool(){ + //if(logFile == null || logFile.trim().equals("") || !logFile.equals(getLogFile())){ + // newLog(); + //} + } + public LogTool(String logPath,boolean systemlog){ + this.logPath=logPath; + this.systemlog=systemlog; + } + private String getLogFile(){ + //获取当前系统路径 + String sysPath=GCONST.getRootPath(); + if(sysPath==null){ + sysPath=System.getProperty("user.dir"); + } + if(!"".equals(logPath)){ + if(logPath.endsWith("/")){ + sysPath += logPath+df.format(new Date())+".log"; + }else{ + sysPath += logPath+"/"+df.format(new Date())+".log"; + } + }else { + sysPath += "/log/dev/"+df.format(new Date())+".log"; + } + return sysPath; + } + + private void newLog(){ + logFile = getLogFile(); + try{ + //logPrint = new PrintWriter(new FileWriter(logFile, true), true); + logPrint = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (logFile,true),"UTF-8")); + }catch(IOException e){ + try{ + File file=new File(logFile); + if(!file.getParentFile().exists()) { + //如果目标文件所在的目录不存在,则创建父目录 + if(file.getParentFile().mkdirs()){ + file.createNewFile(); + } + } + //logPrint = new PrintWriter(new FileWriter(logFile, true), true); + logPrint = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (logFile,true),"UTF-8")); + }catch(IOException ex){ + log.writeLog("Log记录出错了",ex); + } + } + } + + public void writeLog(Object msg) { + if(systemlog){ + log.writeLog(msg); + } + newLog(); + try { + logPrint.write(newDf.format(new Date()) + ": " + msg); + logPrint.newLine();//每次换行 + logPrint.flush(); + logPrint.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + public void writeLog(String msg,Throwable e) { + if(systemlog){ + log.writeLog(msg,e); + } + newLog(); + try { + logPrint.write(newDf.format(new Date()) + ": " + msg); + logPrint.newLine();//每次换行 + //e.printStackTrace(logPrint); + logPrint.flush(); + logPrint.close(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } +} \ No newline at end of file