极兔日志代码上柴

dev
youhong.ai 2023-06-10 17:49:37 +08:00
parent 00b7d994b6
commit 4cc7722283
2 changed files with 112 additions and 0 deletions

1
.gitignore vendored
View File

@ -48,6 +48,7 @@ src/test/resources/font
src/main/resources/WEB-INF/vm/outFile src/main/resources/WEB-INF/vm/outFile
target/ target/
*.back *.back
!/src/main/youhong_ai_jitu_src/selfdev/util/log
# 老项目代码 # 老项目代码
#/lib/jitulib/ #/lib/jitulib/
#/lib/classbean #/lib/classbean

View File

@ -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
* 12019-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();
}
}
}