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