|
|
@ -20,53 +20,47 @@ public class XxlJobFileAppender {
|
|
|
|
public static final InheritableThreadLocal<String> contextHolder = new InheritableThreadLocal<String>();
|
|
|
|
public static final InheritableThreadLocal<String> contextHolder = new InheritableThreadLocal<String>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String logPath = "/data/applogs/xxl-job/jobhandler/";
|
|
|
|
// log base path
|
|
|
|
|
|
|
|
private static String logBasePath = "/data/applogs/xxl-job/jobhandler";
|
|
|
|
public static void initLogPath(String logPath){
|
|
|
|
public static void initLogPath(String logPath){
|
|
|
|
// init
|
|
|
|
// init
|
|
|
|
if (logPath!=null && logPath.trim().length()>0) {
|
|
|
|
if (logPath!=null && logPath.trim().length()>0) {
|
|
|
|
/*if (!logPath.endsWith("/")) {
|
|
|
|
logBasePath = logPath;
|
|
|
|
logPath = logPath.concat("/");
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
XxlJobFileAppender.logPath = logPath;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// mk dir
|
|
|
|
// mk base dir
|
|
|
|
File logPathDir = new File(XxlJobFileAppender.logPath);
|
|
|
|
File logPathDir = new File(logBasePath);
|
|
|
|
if (!logPathDir.exists()) {
|
|
|
|
if (!logPathDir.exists()) {
|
|
|
|
logPathDir.mkdirs();
|
|
|
|
logPathDir.mkdirs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XxlJobFileAppender.logPath = logPathDir.getPath();
|
|
|
|
logBasePath = logPathDir.getPath();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// mk glue dir
|
|
|
|
|
|
|
|
File glueBaseDir = new File(logPathDir, "gluesource");
|
|
|
|
|
|
|
|
if (!glueBaseDir.exists()) {
|
|
|
|
|
|
|
|
glueBaseDir.mkdirs();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static String getLogPath() {
|
|
|
|
public static String getLogPath() {
|
|
|
|
return logPath;
|
|
|
|
return logBasePath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* log filename: yyyy-MM-dd/9999.log
|
|
|
|
* log filename, like "logPath/yyyy-MM-dd/9999.log"
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param triggerDate
|
|
|
|
* @param triggerDate
|
|
|
|
* @param logId
|
|
|
|
* @param logId
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String makeLogFileName(Date triggerDate, int logId) {
|
|
|
|
public static String makeLogFileName(Date triggerDate, int logId) {
|
|
|
|
|
|
|
|
// filePath/yyyy-MM-dd/9999.log
|
|
|
|
// filePath/
|
|
|
|
|
|
|
|
File filePathDir = new File(logPath);
|
|
|
|
|
|
|
|
if (!filePathDir.exists()) {
|
|
|
|
|
|
|
|
filePathDir.mkdirs();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// filePath/yyyy-MM-dd/
|
|
|
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // avoid concurrent problem, can not be static
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // avoid concurrent problem, can not be static
|
|
|
|
|
|
|
|
String logFileName = getLogPath()
|
|
|
|
String nowFormat = sdf.format(new Date());
|
|
|
|
.concat("/")
|
|
|
|
File filePathDateDir = new File(filePathDir, nowFormat);
|
|
|
|
.concat(sdf.format(triggerDate))
|
|
|
|
if (!filePathDateDir.exists()) {
|
|
|
|
.concat("/")
|
|
|
|
filePathDateDir.mkdirs();
|
|
|
|
.concat(String.valueOf(logId))
|
|
|
|
}
|
|
|
|
.concat(".log");
|
|
|
|
|
|
|
|
|
|
|
|
// filePath/yyyy-MM-dd/9999.log
|
|
|
|
|
|
|
|
String logFileName = sdf.format(triggerDate).concat("/").concat(String.valueOf(logId)).concat(".log");
|
|
|
|
|
|
|
|
return logFileName;
|
|
|
|
return logFileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -78,17 +72,11 @@ public class XxlJobFileAppender {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void appendLog(String logFileName, String appendLog) {
|
|
|
|
public static void appendLog(String logFileName, String appendLog) {
|
|
|
|
|
|
|
|
|
|
|
|
// log
|
|
|
|
|
|
|
|
if (appendLog == null) {
|
|
|
|
|
|
|
|
appendLog = "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
appendLog += "\r\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// log file
|
|
|
|
// log file
|
|
|
|
if (logFileName==null || logFileName.trim().length()==0) {
|
|
|
|
if (logFileName==null || logFileName.trim().length()==0) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File logFile = new File(logPath, logFileName);
|
|
|
|
File logFile = new File(logFileName);
|
|
|
|
|
|
|
|
|
|
|
|
if (!logFile.exists()) {
|
|
|
|
if (!logFile.exists()) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -98,6 +86,12 @@ public class XxlJobFileAppender {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// log
|
|
|
|
|
|
|
|
if (appendLog == null) {
|
|
|
|
|
|
|
|
appendLog = "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
appendLog += "\r\n";
|
|
|
|
|
|
|
|
|
|
|
|
// append file content
|
|
|
|
// append file content
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -133,7 +127,7 @@ public class XxlJobFileAppender {
|
|
|
|
if (logFileName==null || logFileName.trim().length()==0) {
|
|
|
|
if (logFileName==null || logFileName.trim().length()==0) {
|
|
|
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
|
|
|
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File logFile = new File(logPath, logFileName);
|
|
|
|
File logFile = new File(logFileName);
|
|
|
|
|
|
|
|
|
|
|
|
if (!logFile.exists()) {
|
|
|
|
if (!logFile.exists()) {
|
|
|
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
|
|
|
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
|
|
|
|