001package 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
017import java.io.*;
018
019import net.sf.logdistiller.LogDistillation;
020import net.sf.logdistiller.LogDistiller;
021import net.sf.logdistiller.LogEvent;
022import net.sf.logdistiller.Plugin;
023import net.sf.logdistiller.ReportFormat;
024import 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 */
042public 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    }
076
077    protected PrintStream getSamplingLogStream()
078        throws FileNotFoundException
079    {
080        if ( samplingLogStream == null )
081        {
082            samplingLogStream = new PrintStream( new FileOutputStream( samplingLogFile ) );
083        }
084        return samplingLogStream;
085    }
086
087    public void addLogEventToFreq( LogEvent logEvent, Freq freq )
088        throws IOException
089    {
090        super.addLogEventToFreq( logEvent, freq );
091
092        if ( ( ( samplingMaxCount < 0 ) || ( ( samplingMaxCount > 0 ) && ( freq.count <= samplingMaxCount ) ) )
093            && ( ( samplingMaxSize < 0 ) || ( ( samplingMaxSize > 0 ) && ( freq.bytes <= samplingMaxSize ) ) ) )
094        {
095            getSamplingLogStream().println( logEvent.getRawLog() );
096        }
097    }
098
099    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}