package com.customcode.util;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.TimeUtil;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日志生成位置在ecology\log\actionlog
* @Author:
* @Date:
*
**/
public class XmActionLog {
public BaseBean baseBean = new BaseBean();
private static char systemSeparator = File.separatorChar;
private static String logoPath = GCONST.getRootPath() + "log"+systemSeparator+"actionlog";
/**
*
* @param flag
* @return
*/
private boolean init(String flag) {
boolean state = false;
String flagVal = baseBean.getPropValue("XmActionLog", flag);
if ("true".equalsIgnoreCase(flagVal)) {
state = true;
}
return state;
}
public String debug(String actionName, String Msg) {
return writeLog("debug", actionName, Msg);
}
public String info(String actionName, String Msg) {
return writeLog("info", actionName, Msg);
}
public String warn(String actionName, String Msg) {
return writeLog("warn", actionName, Msg);
}
public String error(String actionName, String Msg) {
return writeLog("error", actionName, Msg);
}
/**
* 生成日志
*
* @param flag
* @param actionName
* @param message
* @return
*/
private String writeLog(String flag, String actionName, String message) {
boolean status = init(flag);
if (status) {
String currentDate = TimeUtil.getCurrentDateString();
String currentTime = TimeUtil.getOnlyCurrentTimeString();
message = currentTime + " "+flag+"===>>" + message;
if(flag.length()>4) {
message = currentTime + " "+flag+"==>>" + message;
}
File dir = new File(logoPath + systemSeparator + actionName);
if (!dir.exists()) dir.mkdirs();
File fileName = new File(logoPath + systemSeparator + actionName + systemSeparator + actionName+"_"+flag+"_"+currentDate + "_log.txt");
if (!fileName.exists()) {
try {
fileName.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter writer = null;
try {
writer = new FileWriter(fileName, true);
writer.write(message + "\r\n");
} catch (IOException e) {
e.printStackTrace();
try {
if (writer != null)
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public String outlog(String actionName, String meg) {
return outlog("debug", actionName, meg);
}
public String outlog(String flag, String actionName, String meg) {
boolean status = init(flag);
if(status) {
SimpleDateFormat CurrentDate = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat CurrentTime = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String thisDate = CurrentDate.format(date);
String thisTime = CurrentTime.format(date);
meg = "==>>" + thisTime + "==>>" + meg;
File dir = new File(logoPath);
if (!dir.exists()) dir.mkdir();
File fileName = new File(logoPath+"/"+thisDate+"_"+actionName+"_log.txt");
if (!fileName.exists()) {
try {
fileName.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter writer = null;
try {
writer = new FileWriter(fileName, true);
writer.write(meg + "\r\n");
} catch (IOException e) {
e.printStackTrace();
try {
if (writer != null)
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "0";
}
/**
* 输出日志到某个文件夹下
* @param meg 日志信息
* @param Directory 存放文件夹名称 (默认在ecology/log/下)
* @param type 日志类型
*/
public void outTolog( String meg,String Directory,String type){
SimpleDateFormat format_01= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String str = format_01.format(date);
meg=str+" "+meg;
File directory = new File(GCONST.getRootPath() +"/log"+ File.separator + Directory);//
if (!directory.exists()) {//目录不存在就创建
directory.mkdir();
}
SimpleDateFormat format_02= new SimpleDateFormat("yyyy-MM-dd");
String strDate=format_02.format(date);
File fileName = new File ("");
fileName = new File (directory+"/Record_log_"+strDate+".txt");//
if(!fileName.exists()){//文件不存在就创建
try {
fileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileWriter writer = null;
try {
writer = new FileWriter(fileName, true);
writer.write(meg+"\r\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}