1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package fr.ifremer.isisfish.logging;
27
28 import java.io.BufferedWriter;
29 import java.io.File;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.UnsupportedEncodingException;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.nuiton.util.StringUtil;
37
38
39
40
41
42
43 public abstract class LoggerHelper {
44
45 public static String filename;
46 public static long MEGA_STREAM_SIZE = 6 * 100000;
47 private static long totalPause;
48
49 protected static final Log log = LogFactory.getLog(LoggerHelper.class);
50
51 public static void createMock(File testDir, String filename, int i) throws IOException, InterruptedException {
52 LoggerHelper.MEGA_STREAM_SIZE = 6 * i;
53 LoggerHelper.filename = new File(testDir, filename).getAbsolutePath();
54 File file = new File(testDir, filename);
55 long t0 = System.nanoTime();
56 mockLogFile();
57 long t1;
58
59 t1 = System.nanoTime();
60 log.info("create [" + LoggerHelper.MEGA_STREAM_SIZE + "] in " + StringUtil.convertTime(t1 - t0) + " [file: " + file + ", length: " + StringUtil.convertMemory(file.length()) + "]");
61 System.gc();
62 }
63
64 static void pause(long time) throws InterruptedException {
65 Thread.sleep(time);
66 totalPause += time;
67 System.gc();
68 }
69
70
71
72
73
74
75
76 public static void mockLogFile() throws IOException, InterruptedException {
77 File f = new File(filename);
78 if (f.exists()) {
79 f.delete();
80 }
81 BufferedWriter s = new BufferedWriter(new FileWriter(f));
82 int MAX_0 = (int) (MEGA_STREAM_SIZE / 6);
83 int i = 0;
84 long t0, t00;
85 long t1;
86 totalPause = 0;
87 t00 = System.currentTimeMillis();
88 t0 = System.currentTimeMillis();
89 try {
90
91 for (i = 0; i < MAX_0; i++) {
92 LogRecord record = LogRecord.newInstance(LogLevel.FATAL, 0, i + "debug ", "ioreiovrio veuio vreupo xz xe opiu tep uxr u ", "xeoiujoiz xpo puoi rui hp", i, new String[0]);
93 s.write(record.toString() + '\n');
94 s.flush();
95 }
96
97 t1 = System.currentTimeMillis();
98 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
99
100 t0 = System.currentTimeMillis();
101
102 for (i = MAX_0; i < 2 * MAX_0; i++) {
103 LogRecord record = LogRecord.newInstance(LogLevel.WARN, 0, i + " warn", "jkljfdjkl fdkjlkj lkjdlkfjl dfkljl dflkfdj lfdjlk fjdlkj fdl", "vmfof o rieipk?prj,anklvndfkljn gmlrtsoijrmoig", i, new String[0]);
104 s.write(record.toString() + '\n');
105 }
106 t1 = System.currentTimeMillis();
107 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
108
109 t0 = System.currentTimeMillis();
110 for (i = 2 * MAX_0; i < 3 * MAX_0; i++) {
111 LogRecord record = LogRecord.newInstance(LogLevel.INFO, 0, i + " info", "grlgjlrmgr jglerj mgrjm gre pjkogremoiqp jgpo rejopigjrej,m", "grkgherjfezoprjgbpdofajhlbnb;k nfkfjgmjmsg", i, new String[0]);
112 s.write(record.toString() + '\n');
113 }
114 t1 = System.currentTimeMillis();
115 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
116
117 t0 = System.currentTimeMillis();
118 for (i = 3 * MAX_0; i < 4 * MAX_0; i++) {
119 LogRecord record = LogRecord.newInstance(LogLevel.DEBUG, 0, i + " debug", "grmgjueroijiorehjnjodj j gn mbr,nhltrhjjdo jp joi jreio j", "gezoeteroiyerphehkzb,fgbd bcmsherkgheiusbcjkdvberb ", i, new String[0]);
120 s.write(record.toString() + '\n');
121 s.flush();
122 }
123 t1 = System.currentTimeMillis();
124 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
125
126 t0 = System.currentTimeMillis();
127 for (i = 4 * MAX_0; i < 5 * MAX_0; i++) {
128 LogRecord record = LogRecord.newInstance(LogLevel.TRACE, 0, i + " trace", "ffgkdhvlsdknjk eriopgejo ipjiropj i io poi opijgr pe jupoieruj ga geigerihpi oeop erp ", "griofeioteogbbekjbnrjemk prejrlmekhgrehivbn rekhgrhi ", i, new String[0]);
129 s.write(record.toString() + '\n');
130 }
131 t1 = System.currentTimeMillis();
132 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
133
134 t0 = System.currentTimeMillis();
135 for (i = 5 * MAX_0; i < 6 * MAX_0; i++) {
136 LogRecord record = LogRecord.newInstance(LogLevel.TRACE, 0, i + " user", "grekgrhze t e re klgre ge rgre gre gfer g rjyukj yujty reez ze fz", "gekfhezh zhrze hgerhglkrej ljbvkxdfn df,ekgrrnvkmn", i, new String[0]);
137 s.write(record.toString() + '\n');
138 s.flush();
139 }
140 t1 = System.currentTimeMillis();
141 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
142 } catch (OutOfMemoryError e) {
143 t1 = System.currentTimeMillis();
144 log.info("failed after | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
145 e.printStackTrace();
146 } finally {
147 if (s != null) {
148 s.flush();
149 s.close();
150 }
151 }
152 }
153
154
155
156
157
158
159
160 public static void mockLogFile2() throws IOException, InterruptedException {
161 File f = new File(filename);
162 if (f.exists()) {
163 f.delete();
164 }
165 BufferedWriter s = new BufferedWriter(new FileWriter(f));
166 int MAX_0 = (int) (MEGA_STREAM_SIZE / 6);
167 int i = 0;
168 long t0, t00;
169 long t1;
170 totalPause = 0;
171 t00 = System.currentTimeMillis();
172 t0 = System.currentTimeMillis();
173 try {
174
175 for (i = 0; i < MAX_0; i++) {
176 LogRecord record = LogRecord.newInstance(LogLevel.FATAL, 0, i + "debug ", "ioreiovrio veuio vreupo xz xe opiu tep uxr u ", "xeoiujoiz xpo puoi rui hp", i, new String[0]);
177 s.write(record.toString() + '\n');
178 s.flush();
179 }
180
181 t1 = System.currentTimeMillis();
182 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
183
184 t0 = System.currentTimeMillis();
185
186 for (i = MAX_0; i < 2 * MAX_0; i++) {
187 LogRecord record = LogRecord.newInstance(LogLevel.WARN, 0, i + " warn", "jkljfdjkl fdkjlkj lkjdlkfjl dfkljl dflkfdj lfdjlk fjdlkj fdl", "vmfof o rieipk?prj,ùnklvndfkljn gmlrtsoîjrmoig", i, new String[0]);
188 s.write(record.toString() + '\n');
189 }
190 t1 = System.currentTimeMillis();
191 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
192
193 t0 = System.currentTimeMillis();
194 for (i = 2 * MAX_0; i < 3 * MAX_0; i++) {
195 LogRecord record = LogRecord.newInstance(LogLevel.INFO, 0, i + " info", "grlgjlrmgr jglerj mgrjm gre pjkogremoiqp jgpo rejopigjrej,m", "grkgherjfezoprjgbpdofùjhlbnb;k nfkfjgmjmsg", i, new String[0]);
196 s.write(record.toString() + '\n');
197 }
198 t1 = System.currentTimeMillis();
199 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
200
201 t0 = System.currentTimeMillis();
202 for (i = 3 * MAX_0; i < 4 * MAX_0; i++) {
203 LogRecord record = LogRecord.newInstance(LogLevel.DEBUG, 0, i + " debug", "grmgjueroijiorehjnjodj j gn mbr,nhltrhjjdo jp joi jreio j", "gezoeteroiyerphehkzb,fgbd bcmsherkgheiusbcjkdvberb ", i, new String[0]);
204 s.write(record.toString() + '\n');
205 s.flush();
206 }
207 t1 = System.currentTimeMillis();
208 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
209
210 t0 = System.currentTimeMillis();
211 for (i = 4 * MAX_0; i < 5 * MAX_0; i++) {
212 LogRecord record = LogRecord.newInstance(LogLevel.TRACE, 0, i + " trace", "ffgkdhvlsdknjk eriopgejo ipjiropj i io poi opijgr pe jupoieruj g'^ geigerihpi oeop erp ", "griofeioteogbbekjbnrjemk çprejrlmekhgrehivbn rekhgrhi ", i, new String[0]);
213 s.write(record.toString() + '\n');
214 }
215 t1 = System.currentTimeMillis();
216 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
217
218 t0 = System.currentTimeMillis();
219 for (i = 5 * MAX_0; i < 6 * MAX_0; i++) {
220 LogRecord record = LogRecord.newInstance(LogLevel.TRACE, 0, i + " user", "grekgrhze t e re klgre ge rgre gre gfer g rjyukj yujty reez ze fz", "gekfhezh zhrze hgerhglkrej ljbvkxdfn df,ekgrrnvkmn", i, new String[0]);
221 s.write(record.toString() + '\n');
222 s.flush();
223 }
224 t1 = System.currentTimeMillis();
225 log.debug("create next [" + MAX_0 + "] in " + StringUtil.convertTime(t1 - t0) + " | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
226 } catch (OutOfMemoryError e) {
227 t1 = System.currentTimeMillis();
228 log.info("failed after | total " + i + " in " + StringUtil.convertTime(t1 - totalPause - t00));
229 e.printStackTrace();
230 } finally {
231 if (s != null) {
232 s.flush();
233 s.close();
234 }
235 }
236 }
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 public static class LogRecord implements java.io.Serializable {
260
261
262 protected long timeStamp;
263
264
265 protected String message;
266
267
268 protected int level;
269
270
271 protected String[] throwableStrRep;
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 protected transient String text;
289
290
291
292
293
294
295
296 protected transient long offset;
297
298 private static final long serialVersionUID = 1335077740059488080L;
299
300 public static LogRecord newInstance(LogLevel level,
301 long timestamp,
302 String message,
303 String fileName,
304 String methodName,
305 int lineNumber,
306 String[] errors) throws UnsupportedEncodingException {
307 return new LogRecord(
308 level == null ? LogLevel.DEBUG.ordinal() : level.ordinal(),
309 timestamp,
310 message,
311 fileName,
312 methodName,
313 lineNumber,
314 errors
315 );
316 }
317
318 protected LogRecord(int level,
319 long timestamp,
320 String message,
321 String filename,
322 String methodName,
323 int lineNumber,
324 String[] throwableStrRep) throws UnsupportedEncodingException {
325 this.level = level;
326 this.timeStamp = timestamp;
327
328 this.message = new String((filename + ":" + lineNumber + " - " + methodName + " - " + message).getBytes("utf-8"));
329
330
331
332 this.throwableStrRep = throwableStrRep;
333 }
334
335 public String getMessage() {
336 return message;
337 }
338
339 public int getLevel() {
340 return level;
341 }
342
343 public String[] getThrowableStrRep() {
344 return throwableStrRep;
345 }
346
347 public long getTimeStamp() {
348 return timeStamp;
349 }
350
351 public int getLineNumber() {
352 return 0;
353
354 }
355
356 public String getMethodName() {
357 return "";
358
359 }
360
361 public long getOffset() {
362 return offset;
363 }
364
365 public void setOffset(long offset) {
366 this.offset = offset;
367 }
368
369
370
371
372
373
374 @Override
375 public String toString() {
376 if (text == null) {
377 text = new StringBuilder(LogLevel.getLogLevel(level).toString()).
378 append(' ').append(message).toString();
379 }
380
381
382
383
384
385
386 return text;
387 }
388
389
390
391
392
393
394
395
396 @Override
397 public boolean equals(Object o) {
398 if (this == o) {
399 return true;
400 }
401 if (o == null || getClass() != o.getClass()) {
402 return false;
403 }
404
405 LogRecord event = (LogRecord) o;
406 return timeStamp == event.timeStamp &&
407 !(message != null ? !message.equals(event.message) :
408 event.message != null);
409 }
410
411 @Override
412 public int hashCode() {
413 int result;
414 result = (int) (timeStamp ^ (timeStamp >>> 32));
415 result = 31 * result + (message != null ? message.hashCode() : 0);
416 return result;
417 }
418
419 }
420 }