|
|
|
|
@ -1,17 +1,22 @@
|
|
|
|
|
package com.xxl.job.core.log;
|
|
|
|
|
|
|
|
|
|
import com.xxl.job.core.openapi.model.LogResult;
|
|
|
|
|
import com.xxl.tool.core.DateTool;
|
|
|
|
|
import com.xxl.tool.core.StringTool;
|
|
|
|
|
import com.xxl.tool.io.FileTool;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* store trigger log in each log-file
|
|
|
|
|
*
|
|
|
|
|
* @author xuxueli 2016-3-12 19:25:12
|
|
|
|
|
*/
|
|
|
|
|
public class XxlJobFileAppender {
|
|
|
|
|
@ -31,23 +36,19 @@ public class XxlJobFileAppender {
|
|
|
|
|
private static String logBasePath = "/data/applogs/xxl-job/jobhandler";
|
|
|
|
|
private static String glueSrcPath = logBasePath.concat(File.separator).concat("gluesource");
|
|
|
|
|
private static String callbackLogPath = logBasePath.concat(File.separator).concat("callbacklogs");
|
|
|
|
|
public static void initLogPath(String logPath){
|
|
|
|
|
public static void initLogPath(String logPath) throws IOException {
|
|
|
|
|
// init
|
|
|
|
|
if (StringTool.isNotBlank(logPath)) {
|
|
|
|
|
logBasePath = logPath.trim();
|
|
|
|
|
}
|
|
|
|
|
// mk base dir
|
|
|
|
|
File logPathDir = new File(logBasePath);
|
|
|
|
|
if (!logPathDir.exists()) {
|
|
|
|
|
logPathDir.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
FileTool.createDirectories(logPathDir);
|
|
|
|
|
logBasePath = logPathDir.getPath();
|
|
|
|
|
|
|
|
|
|
// mk glue dir
|
|
|
|
|
File glueBaseDir = new File(logPathDir, "gluesource");
|
|
|
|
|
if (!glueBaseDir.exists()) {
|
|
|
|
|
glueBaseDir.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
FileTool.createDirectories(glueBaseDir);
|
|
|
|
|
glueSrcPath = glueBaseDir.getPath();
|
|
|
|
|
}
|
|
|
|
|
public static String getLogPath() {
|
|
|
|
|
@ -63,25 +64,24 @@ public class XxlJobFileAppender {
|
|
|
|
|
/**
|
|
|
|
|
* log filename, like "logPath/yyyy-MM-dd/9999.log"
|
|
|
|
|
*
|
|
|
|
|
* @param triggerDate
|
|
|
|
|
* @param logId
|
|
|
|
|
* @return
|
|
|
|
|
* @param logId log id
|
|
|
|
|
* @return log file name
|
|
|
|
|
*/
|
|
|
|
|
public static String makeLogFileName(Date triggerDate, long logId) {
|
|
|
|
|
|
|
|
|
|
// filePath/yyyy-MM-dd
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // avoid concurrent problem, can not be static
|
|
|
|
|
File logFilePath = new File(getLogPath(), sdf.format(triggerDate));
|
|
|
|
|
if (!logFilePath.exists()) {
|
|
|
|
|
logFilePath.mkdir();
|
|
|
|
|
}
|
|
|
|
|
// "filePath/yyyy-MM-dd"
|
|
|
|
|
File logFilePath = new File(getLogPath(), DateTool.formatDate(triggerDate));
|
|
|
|
|
try {
|
|
|
|
|
FileTool.createDirectories(logFilePath);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("XxlJobFileAppender makeLogFileName error, logFilePath:"+ logFilePath.getPath(), e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// filePath/yyyy-MM-dd/9999.log
|
|
|
|
|
String logFileName = logFilePath.getPath()
|
|
|
|
|
.concat(File.separator)
|
|
|
|
|
.concat(String.valueOf(logId))
|
|
|
|
|
.concat(".log");
|
|
|
|
|
return logFileName;
|
|
|
|
|
// filePath/yyyy-MM-dd/9999.log
|
|
|
|
|
return logFilePath.getPath()
|
|
|
|
|
.concat(File.separator)
|
|
|
|
|
.concat(String.valueOf(logId))
|
|
|
|
|
.concat(".log");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -92,34 +92,17 @@ public class XxlJobFileAppender {
|
|
|
|
|
*/
|
|
|
|
|
public static void appendLog(String logFileName, String appendLog) {
|
|
|
|
|
|
|
|
|
|
// log file
|
|
|
|
|
if (logFileName==null || logFileName.trim().isEmpty()) {
|
|
|
|
|
// valid
|
|
|
|
|
if (StringTool.isBlank(logFileName) || appendLog == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
File logFile = new File(logFileName);
|
|
|
|
|
if (!logFile.exists()) {
|
|
|
|
|
try {
|
|
|
|
|
logFile.createNewFile();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// log
|
|
|
|
|
if (appendLog == null) {
|
|
|
|
|
appendLog = "";
|
|
|
|
|
}
|
|
|
|
|
appendLog += "\r\n";
|
|
|
|
|
|
|
|
|
|
// append file content
|
|
|
|
|
try (FileOutputStream fos = new FileOutputStream(logFile, true)) {
|
|
|
|
|
fos.write(appendLog.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
fos.flush();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
// append log
|
|
|
|
|
try {
|
|
|
|
|
FileTool.writeLines(logFileName, List.of(appendLog), true);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("XxlJobFileAppender appendLog error, logFileName:"+ logFileName, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -129,53 +112,52 @@ public class XxlJobFileAppender {
|
|
|
|
|
* @param fromLineNum from line num
|
|
|
|
|
* @return log content
|
|
|
|
|
*/
|
|
|
|
|
public static LogResult readLog(String logFileName, int fromLineNum){
|
|
|
|
|
public static LogResult readLog(String logFileName, final int fromLineNum){
|
|
|
|
|
|
|
|
|
|
// valid log file
|
|
|
|
|
if (logFileName==null || logFileName.trim().isEmpty()) {
|
|
|
|
|
// valid
|
|
|
|
|
if (StringTool.isBlank(logFileName)) {
|
|
|
|
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
|
|
|
|
|
}
|
|
|
|
|
File logFile = new File(logFileName);
|
|
|
|
|
if (!logFile.exists()) {
|
|
|
|
|
if (!FileTool.exists(logFileName)) {
|
|
|
|
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// read file
|
|
|
|
|
StringBuilder logContentBuilder = new StringBuilder();
|
|
|
|
|
LineNumberReader reader = null;
|
|
|
|
|
int toLineNum = 0;
|
|
|
|
|
/*int readLineCount = 0;*/
|
|
|
|
|
try {
|
|
|
|
|
reader = new LineNumberReader(new InputStreamReader(new FileInputStream(logFile), StandardCharsets.UTF_8));
|
|
|
|
|
String line = null;
|
|
|
|
|
while ((line = reader.readLine())!=null) {
|
|
|
|
|
// skip before lineNum
|
|
|
|
|
toLineNum = reader.getLineNumber(); // [from, to], start as 1
|
|
|
|
|
if (toLineNum < fromLineNum) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// append log
|
|
|
|
|
logContentBuilder.append(line).append("\n");
|
|
|
|
|
// Limit return less than 1000 rows per query request // todo
|
|
|
|
|
/*if(++readLineCount >= 5) {
|
|
|
|
|
break;
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (reader != null) {
|
|
|
|
|
try {
|
|
|
|
|
reader.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// read data
|
|
|
|
|
StringBuilder logContentBuilder = new StringBuilder();
|
|
|
|
|
// num: [from, to], start as 1
|
|
|
|
|
AtomicInteger toLineNum = new AtomicInteger(0);
|
|
|
|
|
AtomicInteger currentLineNum = new AtomicInteger(0);
|
|
|
|
|
/*int readLineCount = 0;*/
|
|
|
|
|
|
|
|
|
|
// do read
|
|
|
|
|
try {
|
|
|
|
|
FileTool.readLines(logFileName, new Consumer<String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void accept(String line) {
|
|
|
|
|
// refresh line num
|
|
|
|
|
currentLineNum.incrementAndGet();
|
|
|
|
|
|
|
|
|
|
// valid
|
|
|
|
|
if (currentLineNum.get() < fromLineNum) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Limit return less than 1000 rows per query request // todo
|
|
|
|
|
/*if(++readLineCount >= 1000) {
|
|
|
|
|
break;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
// collect line data
|
|
|
|
|
toLineNum.set(currentLineNum.get());
|
|
|
|
|
logContentBuilder.append(line).append(System.lineSeparator()); // [from, to], start as 1
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("XxlJobFileAppender readLog error, logFileName:{}, fromLineNum:{}", logFileName, fromLineNum, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// result
|
|
|
|
|
return new LogResult(fromLineNum, toLineNum, logContentBuilder.toString(), false);
|
|
|
|
|
// result
|
|
|
|
|
return new LogResult(fromLineNum, toLineNum.get(), logContentBuilder.toString(), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|