001    package net.sf.logdistiller.plugins;
002    
003    /*
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    import java.io.*;
018    
019    import net.sf.logdistiller.LogDistillation;
020    import net.sf.logdistiller.LogDistiller;
021    import net.sf.logdistiller.LogEvent;
022    import net.sf.logdistiller.Plugin;
023    import net.sf.logdistiller.ReportFormat;
024    import net.sf.logdistiller.util.FormatUtil;
025    
026    /**
027     * <b>Log sampling</b> plugin: works like FreqPlugin, but saves some log events to a log file.
028     * <p>
029     * Parameters (added to FreqPlugin's parameters):
030     * </p>
031     * <ul>
032     * <li><code>sampling.filename</code> (default: <code>[id]-sampling.log</code>): name of the file to save the sample of
033     * log events for each attribute's value.</li>
034     * <li><code>sampling.maxCount</code> (default: 10, -1 = no limit): number of log events to save for each attribute's
035     * value.</li>
036     * <li><code>sampling.maxSize</code> (default: -1 = no limit): maximum size (in kb) of the sample file</li>
037     * </ul>
038     *
039     * @see FreqPlugin
040     * @since 0.7
041     */
042    public class SamplingPlugin
043        extends FreqPlugin
044    {
045        private static final long serialVersionUID = -2960554427849032861L;
046    
047        public final static String ID = "sampling";
048    
049        public final static Plugin TYPE = new Type();
050    
051        private final int samplingMaxCount;
052    
053        private final long samplingMaxSize;
054    
055        private final String samplingFilename;
056    
057        private File samplingLogFile;
058    
059        private transient PrintStream samplingLogStream;
060    
061        public SamplingPlugin( LogDistiller.Plugin definition )
062        {
063            super( definition, false );
064    
065            samplingMaxCount = Integer.parseInt( definition.getParam( "sampling.maxCount", "10" ) );
066            samplingMaxSize = Long.parseLong( definition.getParam( "sampling.maxSize", "-1" ) ) * 1024;
067            samplingFilename = definition.getParam( "sampling.filename", definition.getGroup().getId() + "-sampling.log" );
068        }
069    
070        public void begin( File destinationDirectory )
071            throws FileNotFoundException
072        {
073            super.begin( destinationDirectory );
074            samplingLogFile = new File( destinationDirectory, samplingFilename );
075            samplingLogStream = new PrintStream( new FileOutputStream( samplingLogFile ) );
076        }
077    
078        public void addLogEventToFreq( LogEvent logEvent, Freq freq )
079            throws IOException
080        {
081            super.addLogEventToFreq( logEvent, freq );
082    
083            if ( ( ( samplingMaxCount < 0 ) || ( ( samplingMaxCount > 0 ) && ( freq.count <= samplingMaxCount ) ) )
084                && ( ( samplingMaxSize < 0 ) || ( ( samplingMaxSize > 0 ) && ( freq.bytes <= samplingMaxSize ) ) ) )
085            {
086                samplingLogStream.println( logEvent.getRawLog() );
087            }
088        }
089    
090        public void end()
091            throws IOException
092        {
093            samplingLogStream.close();
094            super.end();
095        }
096    
097        protected void appendLinkToFreqReport( ReportFormat.PluginReport report )
098        {
099            report.addLink( samplingLogFile.getName(), "sampling log file (max count = "
100                + ( ( samplingMaxCount < 0 ) ? "none" : String.valueOf( samplingMaxCount ) ) + ", max size = "
101                + ( ( samplingMaxSize < 0 ) ? "none" : FormatUtil.formatSize( samplingMaxSize ) ) + ")" );
102        }
103    
104        private static class Type
105            extends Plugin
106        {
107            public Type()
108            {
109                super( ID );
110            }
111    
112            public LogDistillation.Plugin newInstance( LogDistiller.Plugin conf )
113            {
114                return new SamplingPlugin( conf );
115            }
116        }
117    }