refactor(log):优化日志读取逻辑并修复潜在问题

- 使用 StringBuilder 替代 StringBuffer 提升性能- 采用 StandardCharsets.UTF_8 确保字符编码一致性
-优化行号判断逻辑,提升代码可读性- 移除冗余注释和无用代码片段-修复日志文件读取时的行数跳过逻辑
- 简化 LogResult 构造逻辑,移除不必要的注释块
pull/72/head
xuxueli 2 months ago
parent 47b341bd75
commit 36a27f7a01

@ -160,7 +160,7 @@ public class XxlJobHelper {
// appendlog // appendlog
String logFileName = xxlJobContext.getJobLogFileName(); String logFileName = xxlJobContext.getJobLogFileName();
if (logFileName!=null && logFileName.trim().length()>0) { if (logFileName!=null && !logFileName.trim().isEmpty()) {
XxlJobFileAppender.appendLog(logFileName, formatAppendLog); XxlJobFileAppender.appendLog(logFileName, formatAppendLog);
return true; return true;
} else { } else {

@ -84,17 +84,16 @@ public class XxlJobFileAppender {
/** /**
* append log * append log
* *
* @param logFileName * @param logFileName log file name
* @param appendLog * @param appendLog append log
*/ */
public static void appendLog(String logFileName, String appendLog) { public static void appendLog(String logFileName, String appendLog) {
// log file // log file
if (logFileName==null || logFileName.trim().length()==0) { if (logFileName==null || logFileName.trim().isEmpty()) {
return; return;
} }
File logFile = new File(logFileName); File logFile = new File(logFileName);
if (!logFile.exists()) { if (!logFile.exists()) {
try { try {
logFile.createNewFile(); logFile.createNewFile();
@ -111,29 +110,20 @@ public class XxlJobFileAppender {
appendLog += "\r\n"; appendLog += "\r\n";
// append file content // append file content
FileOutputStream fos = null; try (FileOutputStream fos = new FileOutputStream(logFile, true)) {
try { fos.write(appendLog.getBytes(StandardCharsets.UTF_8));
fos = new FileOutputStream(logFile, true); fos.flush();
fos.write(appendLog.getBytes("utf-8")); } catch (Exception e) {
fos.flush(); logger.error(e.getMessage(), e);
} catch (Exception e) { }
logger.error(e.getMessage(), e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
} }
/** /**
* support read log-file * support read log-file
* *
* @param logFileName * @param logFileName log file name
* @param fromLineNum from line num
* @return log content * @return log content
*/ */
public static LogResult readLog(String logFileName, int fromLineNum){ public static LogResult readLog(String logFileName, int fromLineNum){

Loading…
Cancel
Save