1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.corpus;
15
16 import java.io.FileWriter;
17 import java.io.IOException;
18 import java.net.URL;
19 import java.util.List;
20
21 import ch.qos.logback.classic.ClassicConstants;
22 import ch.qos.logback.classic.spi.ILoggingEvent;
23 import ch.qos.logback.classic.spi.IThrowableProxy;
24 import ch.qos.logback.classic.spi.LoggerContextVO;
25 import ch.qos.logback.classic.spi.PubLoggingEventVO;
26 import ch.qos.logback.classic.spi.ThrowableProxyUtil;
27 import ch.qos.logback.core.CoreConstants;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class Corpus {
50
51 static public final int STANDARD_CORPUS_SIZE = 50 * 1000;
52 private static final int STANDARD_SEED = 34780;
53
54 static public List<String> getStandatdCorpusWordList() throws IOException {
55 ClassLoader classLoader = Corpus.class.getClassLoader();
56 URL originOfSpeciesURL = classLoader.getResource("corpus/origin_of_species.txt");
57 return TextFileUtil.toWords(originOfSpeciesURL);
58 }
59
60
61
62
63
64
65
66
67 static public ILoggingEvent[] makeStandardCorpus() throws IOException {
68 List<String> worldList = getStandatdCorpusWordList();
69 CorpusModel corpusMaker = new CorpusModel(STANDARD_SEED, worldList);
70 return make(corpusMaker, STANDARD_CORPUS_SIZE, true);
71 }
72
73 static public ILoggingEvent[] make(CorpusModel corpusModel, int n, boolean withCallerData) {
74 LoggerContextVO lcVO = corpusModel.getRandomlyNamedLoggerContextVO();
75 PubLoggingEventVO[] plevoArray = new PubLoggingEventVO[n];
76 for (int i = 0; i < n; i++) {
77 PubLoggingEventVO e = new PubLoggingEventVO();
78 plevoArray[i] = e;
79 e.loggerContextVO = lcVO;
80 e.timeStamp = corpusModel.getRandomTimeStamp();
81
82 LogStatement logStatement = corpusModel.getRandomLogStatementFromPool();
83 e.loggerName = logStatement.loggerName;
84 e.level = logStatement.level;
85 e.message = logStatement.mat.message;
86 e.argumentArray = corpusModel.getRandomArgumentArray(logStatement.mat.numberOfArguments);
87
88 if (withCallerData) {
89 e.callerDataArray = corpusModel.getRandomCallerData(ClassicConstants.DEFAULT_MAX_CALLEDER_DATA_DEPTH,
90 e.loggerName);
91 }
92 e.throwableProxy = logStatement.throwableProxy;
93 e.threadName = corpusModel.getRandomThreadNameFromPool();
94 }
95 return plevoArray;
96 }
97
98
99
100
101
102
103
104
105 public static void dump(ILoggingEvent[] eventArray, String targetFile) throws IOException {
106 FileWriter fw = new FileWriter(targetFile);
107 for (ILoggingEvent e : eventArray) {
108 fw.write(e.toString());
109 fw.append(CoreConstants.LINE_SEPARATOR);
110 if (e.getThrowableProxy() != null) {
111 IThrowableProxy tp = e.getThrowableProxy();
112 fw.write(ThrowableProxyUtil.asString(tp));
113 }
114 }
115 fw.flush();
116 fw.close();
117 }
118
119 }