
com.github.javaclub.cdl.client.util.LogUtils Maven / Gradle / Ivy
The newest version!
package com.github.javaclub.cdl.client.util;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class LogUtils {
private static String rootPath = "/midware";
private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static{
try {
File f = new File(rootPath);
if(f.exists() && f.canWrite()){
rootPath = rootPath + "/cdl-client";
FileUtils.forceMkdir(new File(rootPath));
}else{
rootPath = "cdl-client";
FileUtils.forceMkdir(new File("cdl-client"));
}
} catch (IOException e) {
System.out.println(e);
}
}
public static void printSqlLog(List log) {
try {
if (log == null || log.isEmpty()) {
return;
}
File today = new File(rootPath + "/sql.log");
if (!today.exists()) {
today.createNewFile();
} else {
Calendar lastDay = Calendar.getInstance();
if(lastDay.get(Calendar.HOUR_OF_DAY) == 0){ // 凌晨
lastDay.add(Calendar.DAY_OF_MONTH, -1);
File lastDayFile = new File(rootPath + "/sql.log." + sf.format(lastDay.getTime()));
if (!lastDayFile.exists()) {
FileUtils.moveFile(today, lastDayFile);
today = new File(rootPath + "/sql.log");
today.createNewFile();
}
}
}
FileUtils.writeLines(today, log, true);
} catch (Exception e) {
System.out.println(e);
}
}
public static void printSlowSqlLog(List log) {
try {
if (log == null || log.isEmpty()) {
return;
}
File today = new File(rootPath + "/slow-sql.log");
if (!today.exists()) {
today.createNewFile();
} else {
Calendar lastDay = Calendar.getInstance();
if(lastDay.get(Calendar.HOUR_OF_DAY) == 0){ // 凌晨
lastDay.add(Calendar.DAY_OF_MONTH, -1);
File lastDayFile = new File(rootPath + "/slow-sql.log." + sf.format(lastDay.getTime()));
if (!lastDayFile.exists()) {
FileUtils.moveFile(today, lastDayFile);
today = new File(rootPath + "/slow-sql.log");
today.createNewFile();
}
}
}
FileUtils.writeLines(today, log, true);
} catch (Exception e) {
System.out.println(e);
}
}
public static void printCdlLog(String log) {
try {
if (log == null || log.length() == 0) {
return;
}
File today = new File(rootPath + "/cdl.log");
if (!today.exists()) {
today.createNewFile();
} else {
Calendar lastDay = Calendar.getInstance();
if(lastDay.get(Calendar.HOUR_OF_DAY) == 0){ // 凌晨
lastDay.add(Calendar.DAY_OF_MONTH, -1);
File lastDayFile = new File(rootPath + "/cdl.log." + sf.format(lastDay.getTime()));
if (!lastDayFile.exists()) {
FileUtils.moveFile(today, lastDayFile);
today = new File(rootPath + "/cdl.log");
today.createNewFile();
}
}
}
StringBuilder sb = new StringBuilder();
sb.append(sf2.format(new Date()));
sb.append(" ");
sb.append(log);
sb.append("\n");
FileUtils.writeStringToFile(today, sb.toString(), true);
} catch (Exception e) {
System.out.println(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy