From 0c4491a1b87ee4a0bcac0f227a3b3039a34ec1bc Mon Sep 17 00:00:00 2001 From: "xueli.xue" Date: Wed, 8 Jun 2016 15:42:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E9=80=BB=E8=BE=91=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xxl/job/core/log/XxlJobFileAppender.java | 112 ++++++++++++------ 1 file changed, 77 insertions(+), 35 deletions(-) diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java b/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java index f549f886..d6b94ad2 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java @@ -4,9 +4,10 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; +import java.io.LineNumberReader; import java.text.SimpleDateFormat; import java.util.Date; @@ -133,48 +134,89 @@ public class XxlJobFileAppender extends AppenderSkeleton { String logFileName = trigger_log_id.concat(".log"); File logFile = new File(filePathDateDir, logFileName); if (!logFile.exists()) { - try { - logFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - return null; - } + return null; } + String logData = readLines(logFile); + return logData; + } + + /** + * read log data + * @param logFile + * @return + */ + public static String readLines(File logFile){ + BufferedReader reader = null; try { - InputStream ins = null; - BufferedReader reader = null; - try { - ins = new FileInputStream(logFile); - reader = new BufferedReader(new InputStreamReader(ins, "utf-8")); - if (reader != null) { - String content = null; - StringBuilder sb = new StringBuilder(); - while ((content = reader.readLine()) != null) { - sb.append(content).append("\n"); - } - return sb.toString(); + reader = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "utf-8")); + if (reader != null) { + StringBuilder sb = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + sb.append(line).append("\n"); } - } finally { - if (ins != null) { - try { - ins.close(); - } catch (IOException e) { - e.printStackTrace(); - } + return sb.toString(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); } - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } + } + } + return null; + } + + /** + * 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(); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } + return null; - } + + /* + // it will return the number of characters actually skipped + reader.skip(Long.MAX_VALUE); + int maxLineNum = reader.getLineNumber(); + maxLineNum++; // 最大行号 + */ + } }