View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * <p>
32   * Usage:
33   * 
34   * <p>
35   * <code>ILoggingEvent[] eventArray = Corpus.makeStandardCorpus();</code>
36   * 
37   * <p>
38   * if you wish to dump the events into a file, say "/corpus.log" :
39   * 
40   * <p>
41   * <code>Corpus.dump(eventArray, "/corpus.log");
42   * 
43   * <p>
44   * For the model behind the corpus, refer to {@link CorpusModel}.
45   * 
46   * @author Ceki G&uuml;lc&uuml;
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       * Make a standard corpus. The standard corpus has {@link #STANDARD_CORPUS_SIZE}
62       * elements.
63       * 
64       * @return event array representing the standard corpus
65       * @throws IOException
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       * Dump the events passed as argument into the file named targetFile.
100      * 
101      * @param eventArray
102      * @param targetFile
103      * @throws IOException
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 }