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>Usage:
32   * 
33   * <p><code>ILoggingEvent[] eventArray = Corpus.makeStandardCorpus();</code>
34   * 
35   * <p>if you wish to dump the events into a file, say "/corpus.log" :
36   * 
37   * <p> <code>Corpus.dump(eventArray, "/corpus.log");
38   * 
39   * <p>For the model behind the corpus, refer to {@link CorpusModel}.
40   * 
41   * @author Ceki G&uuml;lc&uuml;
42   *
43   */
44  public class Corpus {
45  
46      static public final int STANDARD_CORPUS_SIZE = 50 * 1000;
47      private static final int STANDARD_SEED = 34780;
48  
49      static public List<String> getStandatdCorpusWordList() throws IOException {
50          ClassLoader classLoader = Corpus.class.getClassLoader();
51          URL originOfSpeciesURL = classLoader.getResource("corpus/origin_of_species.txt");
52          return TextFileUtil.toWords(originOfSpeciesURL);
53      }
54  
55      /**
56       * Make a standard corpus. The standard corpus has
57       * {@link #STANDARD_CORPUS_SIZE} elements.
58       * 
59       * @return event array representing the standard corpus
60       * @throws IOException
61       */
62      static public ILoggingEvent[] makeStandardCorpus() throws IOException {
63          List<String> worldList = getStandatdCorpusWordList();
64          CorpusModel corpusMaker = new CorpusModel(STANDARD_SEED, worldList);
65          return make(corpusMaker, STANDARD_CORPUS_SIZE, true);
66      }
67  
68      static public ILoggingEvent[] make(CorpusModel corpusModel, int n, boolean withCallerData) {
69          LoggerContextVO lcVO = corpusModel.getRandomlyNamedLoggerContextVO();
70          PubLoggingEventVO[] plevoArray = new PubLoggingEventVO[n];
71          for (int i = 0; i < n; i++) {
72              PubLoggingEventVO e = new PubLoggingEventVO();
73              plevoArray[i] = e;
74              e.loggerContextVO = lcVO;
75              e.timeStamp = corpusModel.getRandomTimeStamp();
76  
77              LogStatement logStatement = corpusModel.getRandomLogStatementFromPool();
78              e.loggerName = logStatement.loggerName;
79              e.level = logStatement.level;
80              e.message = logStatement.mat.message;
81              e.argumentArray = corpusModel.getRandomArgumentArray(logStatement.mat.numberOfArguments);
82  
83              if (withCallerData) {
84                  e.callerDataArray = corpusModel.getRandomCallerData(ClassicConstants.DEFAULT_MAX_CALLEDER_DATA_DEPTH, e.loggerName);
85              }
86              e.throwableProxy = logStatement.throwableProxy;
87              e.threadName = corpusModel.getRandomThreadNameFromPool();
88          }
89          return plevoArray;
90      }
91  
92      /**
93       * Dump the events passed as argument into the file named targetFile.
94       * 
95       * @param eventArray
96       * @param targetFile
97       * @throws IOException
98       */
99      public static void dump(ILoggingEvent[] eventArray, String targetFile) throws IOException {
100         FileWriter fw = new FileWriter(targetFile);
101         for (ILoggingEvent e : eventArray) {
102             fw.write(e.toString());
103             fw.append(CoreConstants.LINE_SEPARATOR);
104             if (e.getThrowableProxy() != null) {
105                 IThrowableProxy tp = e.getThrowableProxy();
106                 fw.write(ThrowableProxyUtil.asString(tp));
107             }
108         }
109         fw.flush();
110         fw.close();
111     }
112 
113 }