|
|
@@ -4,9 +4,10 @@ import java.io.BufferedReader;
|
|
4
|
4
|
import java.io.File;
|
|
5
|
5
|
import java.io.FileInputStream;
|
|
6
|
6
|
import java.io.FileOutputStream;
|
|
|
7
|
+import java.io.FileReader;
|
|
7
|
8
|
import java.io.IOException;
|
|
8
|
|
-import java.io.InputStream;
|
|
9
|
9
|
import java.io.InputStreamReader;
|
|
|
10
|
+import java.io.LineNumberReader;
|
|
10
|
11
|
import java.text.SimpleDateFormat;
|
|
11
|
12
|
import java.util.Date;
|
|
12
|
13
|
|
|
|
@@ -133,48 +134,89 @@ public class XxlJobFileAppender extends AppenderSkeleton {
|
|
133
|
134
|
String logFileName = trigger_log_id.concat(".log");
|
|
134
|
135
|
File logFile = new File(filePathDateDir, logFileName);
|
|
135
|
136
|
if (!logFile.exists()) {
|
|
136
|
|
- try {
|
|
137
|
|
- logFile.createNewFile();
|
|
138
|
|
- } catch (IOException e) {
|
|
139
|
|
- e.printStackTrace();
|
|
140
|
|
- return null;
|
|
141
|
|
- }
|
|
|
137
|
+ return null;
|
|
142
|
138
|
}
|
|
143
|
139
|
|
|
|
140
|
+ String logData = readLines(logFile);
|
|
|
141
|
+ return logData;
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ /**
|
|
|
145
|
+ * read log data
|
|
|
146
|
+ * @param logFile
|
|
|
147
|
+ * @return
|
|
|
148
|
+ */
|
|
|
149
|
+ public static String readLines(File logFile){
|
|
|
150
|
+ BufferedReader reader = null;
|
|
144
|
151
|
try {
|
|
145
|
|
- InputStream ins = null;
|
|
146
|
|
- BufferedReader reader = null;
|
|
147
|
|
- try {
|
|
148
|
|
- ins = new FileInputStream(logFile);
|
|
149
|
|
- reader = new BufferedReader(new InputStreamReader(ins, "utf-8"));
|
|
150
|
|
- if (reader != null) {
|
|
151
|
|
- String content = null;
|
|
152
|
|
- StringBuilder sb = new StringBuilder();
|
|
153
|
|
- while ((content = reader.readLine()) != null) {
|
|
154
|
|
- sb.append(content).append("\n");
|
|
155
|
|
- }
|
|
156
|
|
- return sb.toString();
|
|
|
152
|
+ reader = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "utf-8"));
|
|
|
153
|
+ if (reader != null) {
|
|
|
154
|
+ StringBuilder sb = new StringBuilder();
|
|
|
155
|
+ String line = null;
|
|
|
156
|
+ while ((line = reader.readLine()) != null) {
|
|
|
157
|
+ sb.append(line).append("\n");
|
|
157
|
158
|
}
|
|
158
|
|
- } finally {
|
|
159
|
|
- if (ins != null) {
|
|
160
|
|
- try {
|
|
161
|
|
- ins.close();
|
|
162
|
|
- } catch (IOException e) {
|
|
163
|
|
- e.printStackTrace();
|
|
164
|
|
- }
|
|
|
159
|
+ return sb.toString();
|
|
|
160
|
+ }
|
|
|
161
|
+ } catch (IOException e) {
|
|
|
162
|
+ e.printStackTrace();
|
|
|
163
|
+ } finally {
|
|
|
164
|
+ if (reader != null) {
|
|
|
165
|
+ try {
|
|
|
166
|
+ reader.close();
|
|
|
167
|
+ } catch (IOException e) {
|
|
|
168
|
+ e.printStackTrace();
|
|
165
|
169
|
}
|
|
166
|
|
- if (reader != null) {
|
|
167
|
|
- try {
|
|
168
|
|
- reader.close();
|
|
169
|
|
- } catch (IOException e) {
|
|
170
|
|
- e.printStackTrace();
|
|
171
|
|
- }
|
|
|
170
|
+ }
|
|
|
171
|
+ }
|
|
|
172
|
+ return null;
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ /**
|
|
|
176
|
+ * read data from line num
|
|
|
177
|
+ * @param sourceFile
|
|
|
178
|
+ * @param fromLineNum
|
|
|
179
|
+ * @return
|
|
|
180
|
+ * @throws Exception
|
|
|
181
|
+ */
|
|
|
182
|
+ public static String readLinesFrom(File logFile, int fromLineNum) {
|
|
|
183
|
+ LineNumberReader reader = null;
|
|
|
184
|
+ try {
|
|
|
185
|
+ reader = new LineNumberReader(new FileReader(logFile));
|
|
|
186
|
+
|
|
|
187
|
+ // sBuffer
|
|
|
188
|
+ StringBuffer sBuffer = new StringBuffer();
|
|
|
189
|
+ String line = null;
|
|
|
190
|
+ int maxLineNum = 0;
|
|
|
191
|
+ while ((line = reader.readLine())!=null) {
|
|
|
192
|
+ maxLineNum++;
|
|
|
193
|
+ if (reader.getLineNumber() >= fromLineNum) {
|
|
|
194
|
+ sBuffer.append(line).append("\n");
|
|
172
|
195
|
}
|
|
173
|
|
- }
|
|
174
|
|
- } catch (Exception e) {
|
|
|
196
|
+ }
|
|
|
197
|
+
|
|
|
198
|
+ System.out.println("maxLineNum : " + maxLineNum);
|
|
|
199
|
+ return sBuffer.toString();
|
|
|
200
|
+ } catch (IOException e) {
|
|
175
|
201
|
e.printStackTrace();
|
|
|
202
|
+ } finally {
|
|
|
203
|
+ if (reader != null) {
|
|
|
204
|
+ try {
|
|
|
205
|
+ reader.close();
|
|
|
206
|
+ } catch (IOException e) {
|
|
|
207
|
+ e.printStackTrace();
|
|
|
208
|
+ }
|
|
|
209
|
+ }
|
|
176
|
210
|
}
|
|
|
211
|
+
|
|
177
|
212
|
return null;
|
|
178
|
|
- }
|
|
|
213
|
+
|
|
|
214
|
+ /*
|
|
|
215
|
+ // it will return the number of characters actually skipped
|
|
|
216
|
+ reader.skip(Long.MAX_VALUE);
|
|
|
217
|
+ int maxLineNum = reader.getLineNumber();
|
|
|
218
|
+ maxLineNum++; // 最大行号
|
|
|
219
|
+ */
|
|
|
220
|
+ }
|
|
179
|
221
|
|
|
180
|
222
|
}
|