日志逻辑优化

v1.3
xueli.xue 9 years ago
parent 63edb6d848
commit 0c4491a1b8

@ -4,9 +4,10 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -133,48 +134,89 @@ public class XxlJobFileAppender extends AppenderSkeleton {
String logFileName = trigger_log_id.concat(".log"); String logFileName = trigger_log_id.concat(".log");
File logFile = new File(filePathDateDir, logFileName); File logFile = new File(filePathDateDir, logFileName);
if (!logFile.exists()) { if (!logFile.exists()) {
try { return null;
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return null;
}
} }
String logData = readLines(logFile);
return logData;
}
/**
* read log data
* @param logFile
* @return
*/
public static String readLines(File logFile){
BufferedReader reader = null;
try { try {
InputStream ins = null; reader = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "utf-8"));
BufferedReader reader = null; if (reader != null) {
try { StringBuilder sb = new StringBuilder();
ins = new FileInputStream(logFile); String line = null;
reader = new BufferedReader(new InputStreamReader(ins, "utf-8")); while ((line = reader.readLine()) != null) {
if (reader != null) { sb.append(line).append("\n");
String content = null;
StringBuilder sb = new StringBuilder();
while ((content = reader.readLine()) != null) {
sb.append(content).append("\n");
}
return sb.toString();
} }
} finally { return sb.toString();
if (ins != null) { }
try { } catch (IOException e) {
ins.close(); e.printStackTrace();
} catch (IOException e) { } finally {
e.printStackTrace(); if (reader != null) {
} try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
} }
if (reader != null) { }
try { }
reader.close(); return null;
} catch (IOException e) { }
e.printStackTrace();
} /**
* read data from line num
* @param sourceFile
* @param fromLineNum
* @return
* @throws Exception
*/
public static String readLinesFrom(File logFile, int fromLineNum) {
LineNumberReader reader = null;
try {
reader = new LineNumberReader(new FileReader(logFile));
// sBuffer
StringBuffer sBuffer = new StringBuffer();
String line = null;
int maxLineNum = 0;
while ((line = reader.readLine())!=null) {
maxLineNum++;
if (reader.getLineNumber() >= fromLineNum) {
sBuffer.append(line).append("\n");
} }
} }
} catch (Exception e) {
System.out.println("maxLineNum : " + maxLineNum);
return sBuffer.toString();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return null; return null;
}
/*
// it will return the number of characters actually skipped
reader.skip(Long.MAX_VALUE);
int maxLineNum = reader.getLineNumber();
maxLineNum++; // 最大行号
*/
}
} }

Loading…
Cancel
Save