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.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   * <p>
33   * Usage:
34   * 
35   * <p>
36   * <code>ILoggingEvent[] eventArray = Corpus.makeStandardCorpus();</code>
37   * 
38   * <p>
39   * if you wish to dump the events into a file, say "/corpus.log" :
40   * 
41   * <p>
42   * <code>Corpus.dump(eventArray, "/corpus.log");
43   * 
44   * <p>
45   * For the model behind the corpus, refer to {@link CorpusModel}.
46   * 
47   * @author Ceki G&uuml;lc&uuml;
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       * Make a standard corpus. The standard corpus has {@link #STANDARD_CORPUS_SIZE}
63       * elements.
64       * 
65       * @return event array representing the standard corpus
66       * @throws IOException
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      * Dump the events passed as argument into the file named targetFile.
101      * 
102      * @param eventArray
103      * @param targetFile
104      * @throws IOException
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 }