ecology_maven/customization/commons/Console.java

175 lines
7.0 KiB
Java
Raw Normal View History

2021-11-14 15:29:16 +08:00
package customization.commons;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author liutaihong
* @version 1.0.0
* @ClassName Console.java
* @Description UTF-8
* @createTime 2020-04-29 14:31:00
*/
public class Console {
public static void log(String logStr) {
StackTraceElement[] stacks = new Throwable().getStackTrace();
//int stacksLen = stacks.length;
String className = stacks[1].getClassName();
String method = stacks[1].getMethodName();
int number = stacks[1].getLineNumber();
String actionFileName = stacks[1].getFileName();
write(logStr,"log",className,method,number,actionFileName);
}
public static void info(String logStr) {
StackTraceElement[] stacks = new Throwable().getStackTrace();
//int stacksLen = stacks.length;
String className = stacks[1].getClassName();
String method = stacks[1].getMethodName();
int number = stacks[1].getLineNumber();
String actionFileName = stacks[1].getFileName();
write(logStr,"info",className,method,number,actionFileName);
}
/**
* action
* <p>
* NoPrintLog.properties classNameclass
* eg className=com.custompage.NodeBefore
* com.custompage.NodeBefore
* <p>
*
* 1:80Gecology
* 2:1
* 3:log/log/ecology*.log200m
* 4200M使访
* <p>
* action
* 1. extends BaseBeanwriteLog extends ActionUtil
* 使writeLog();
* 2.使
* <p>
*
* <<<<<<< HEAD
* /log/devlog/ java
* eg
* /log/devlog/NodeBefore.java_2017-06-18.log
* /log/devlog/NodeAfter.java_2017-06-18.log
* SendHrmResourceInfo.java_2017-06-28.log
* =======
* /log/devlog/ java
* eg
* /log/devlog/NodeBefore.java_2017-06-18.log
* /log/devlog/NodeAfter.java_2017-06-18.log
* >>>>>>> 84c78e050685929fbe1387a54a96dc120300b2a7
* <p>
*
* eg
* m=execute;n=14;t=10:06:23.743===>>
* <<<<<<< HEAD
* m=execute;n=14;t=10:10:08.774===>>NoPrintLog"com.customcode.action.ExportEg"
* =======
* m=execute;n=14;t=10:10:08.774===>>NoPrintLog"com.customcode.action.Export"
* >>>>>>> 84c78e050685929fbe1387a54a96dc120300b2a7
* <p>
*
* m=(method);n=(number);t=(time)==>>log
* execute 14 10:06:23.743
*
* @param logStr log
*/
private static void write(String logStr,String type,String className,String method,int number , String actionFileName ) {
actionFileName = actionFileName.substring(0, actionFileName.indexOf("."));
SimpleDateFormat CurrentDate = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat CurrentTime = new SimpleDateFormat("HH:mm:ss.SSS");
SimpleDateFormat CurrentHour = new SimpleDateFormat("HH");
Date date = new Date();
String thisHour = CurrentHour.format(date);
String thisDate = CurrentDate.format(date);
String thisTime = CurrentTime.format(date);
String log = type+":m=" + method + ";n=" + number + ";t=" + thisTime + "===>>";
String logPath = GCONST.getRootPath() + "log"+File.separatorChar +"devlog" ;
File dir = new File(logPath);
//writeLog(logPath);
if (!dir.exists()) {
dir.mkdir();
System.out.println("创建"+logPath);
}
logPath = GCONST.getRootPath() + "log"+File.separatorChar +"devlog" + File.separatorChar + thisDate;
dir = new File(logPath);
//writeLog(logPath);
if (!dir.exists()) {
dir.mkdir();
System.out.println("创建"+logPath);
}
logPath = GCONST.getRootPath() +"log"+File.separatorChar +"devlog" + File.separatorChar + thisDate + File.separator + actionFileName;
dir = new File(logPath);
//writeLog(logPath);
if (!dir.exists()) {
dir.mkdir();
System.out.println("创建"+logPath);
}
String dirStr = logPath + File.separatorChar + thisHour + ".log";
// writeLog("dirStr:"+dirStr);
File fileName = new File(dirStr);
if (!fileName.exists()) {
try {
fileName.createNewFile();
writeLog(fileName, "m=方法名(method);n=行数(number);t=执行时间(time)==>>打印日志log");
} catch (IOException e) {
e.printStackTrace();
}
}
RecordSet recordSet = new RecordSet();
String allNoPrintLogClass = recordSet.getPropValue("NoPrintLog", "className");
if (allNoPrintLogClass.contains(className)) {
// Log(fileName, log+ "日志未打印如需打开请从配置文件NoPrintLog中删除\""+className+"\"");
} else {
writeLog(fileName, log + logStr);
}
}
/**
* @param fileName
* @param log
*/
private static void writeLog(File fileName, String log) {
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), StandardCharsets.UTF_8));
writer.write(log + "\r\n");
if (writer != null)
writer.close();
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}