View Javadoc
1   package net.sf.logdistiller.plugins;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.io.*;
18  
19  import net.sf.logdistiller.LogDistillation;
20  import net.sf.logdistiller.LogDistiller;
21  import net.sf.logdistiller.LogEvent;
22  import net.sf.logdistiller.Plugin;
23  import net.sf.logdistiller.ReportFormat;
24  import net.sf.logdistiller.util.FormatUtil;
25  
26  /**
27   * <b>Log sampling</b> plugin: works like FreqPlugin, but saves some log events to a log file.
28   * <p>
29   * Parameters (added to FreqPlugin's parameters):
30   * </p>
31   * <ul>
32   * <li><code>sampling.filename</code> (default: <code>[id]-sampling.log</code>): name of the file to save the sample of
33   * log events for each attribute's value.</li>
34   * <li><code>sampling.maxCount</code> (default: 10, -1 = no limit): number of log events to save for each attribute's
35   * value.</li>
36   * <li><code>sampling.maxSize</code> (default: -1 = no limit): maximum size (in kb) of the sample file</li>
37   * </ul>
38   *
39   * @see FreqPlugin
40   * @since 0.7
41   */
42  public class SamplingPlugin
43      extends FreqPlugin
44  {
45      private static final long serialVersionUID = -2960554427849032861L;
46  
47      public final static String ID = "sampling";
48  
49      public final static Plugin TYPE = new Type();
50  
51      private final int samplingMaxCount;
52  
53      private final long samplingMaxSize;
54  
55      private final String samplingFilename;
56  
57      private File samplingLogFile;
58  
59      private transient PrintStream samplingLogStream;
60  
61      public SamplingPlugin( LogDistiller.Plugin definition )
62      {
63          super( definition, false );
64  
65          samplingMaxCount = Integer.parseInt( definition.getParam( "sampling.maxCount", "10" ) );
66          samplingMaxSize = Long.parseLong( definition.getParam( "sampling.maxSize", "-1" ) ) * 1024;
67          samplingFilename = definition.getParam( "sampling.filename", definition.getGroup().getId() + "-sampling.log" );
68      }
69  
70      public void begin( File destinationDirectory )
71          throws FileNotFoundException
72      {
73          super.begin( destinationDirectory );
74          samplingLogFile = new File( destinationDirectory, samplingFilename );
75      }
76  
77      protected PrintStream getSamplingLogStream()
78          throws FileNotFoundException
79      {
80          if ( samplingLogStream == null )
81          {
82              samplingLogStream = new PrintStream( new FileOutputStream( samplingLogFile ) );
83          }
84          return samplingLogStream;
85      }
86  
87      public void addLogEventToFreq( LogEvent logEvent, Freq freq )
88          throws IOException
89      {
90          super.addLogEventToFreq( logEvent, freq );
91  
92          if ( ( ( samplingMaxCount < 0 ) || ( ( samplingMaxCount > 0 ) && ( freq.count <= samplingMaxCount ) ) )
93              && ( ( samplingMaxSize < 0 ) || ( ( samplingMaxSize > 0 ) && ( freq.bytes <= samplingMaxSize ) ) ) )
94          {
95              getSamplingLogStream().println( logEvent.getRawLog() );
96          }
97      }
98  
99      public void end()
100         throws IOException
101     {
102         if ( samplingLogStream != null )
103         {
104             samplingLogStream.close();
105         }
106         super.end();
107     }
108 
109     protected void appendLinkToFreqReport( ReportFormat.PluginReport report )
110     {
111         report.addLink( samplingLogFile.getName(), "sampling log file (max count = "
112             + ( ( samplingMaxCount < 0 ) ? "none" : String.valueOf( samplingMaxCount ) ) + ", max size = "
113             + ( ( samplingMaxSize < 0 ) ? "none" : FormatUtil.formatSize( samplingMaxSize ) ) + ")" );
114     }
115 
116     private static class Type
117         extends Plugin
118     {
119         public Type()
120         {
121             super( ID );
122         }
123 
124         public LogDistillation.Plugin newInstance( LogDistiller.Plugin conf )
125         {
126             return new SamplingPlugin( conf );
127         }
128     }
129 }