1 package net.sf.logdistiller.plugins;
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
31
32
33
34
35
36
37
38
39
40
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 }