Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.codahale.metrics;
  
  import org.slf4j.Logger;
  import org.slf4j.Marker;
  
A reporter class for logging metrics values to a SLF4J org.slf4j.Logger periodically, similar to ConsoleReporter or CsvReporter, but using the SLF4J framework instead. It also supports specifying a org.slf4j.Marker instance that can be used by custom appenders and filters for the bound logging toolkit to further process metrics reports.
 
 public class Slf4jReporter extends ScheduledReporter {
    
Returns a new Slf4jReporter.Builder for Slf4jReporter.

Parameters:
registry the registry to report
Returns:
a Slf4jReporter.Builder instance for a Slf4jReporter
 
     public static Builder forRegistry(MetricRegistry registry) {
         return new Builder(registry);
     }

    
A builder for CsvReporter instances. Defaults to logging to metrics, not using a marker, converting rates to events/second, converting durations to milliseconds, and not filtering metrics.
 
     public static class Builder {
         private final MetricRegistry registry;
         private Logger logger;
         private Marker marker;
         private TimeUnit rateUnit;
         private TimeUnit durationUnit;
         private MetricFilter filter;
 
         private Builder(MetricRegistry registry) {
             this. = registry;
             this. = LoggerFactory.getLogger("metrics");
             this. = null;
             this. = .;
             this. = .;
             this. = .;
         }

        
Log metrics to the given logger.

Parameters:
logger an SLF4J org.slf4j.Logger
Returns:
this
 
         public Builder outputTo(Logger logger) {
             this. = logger;
             return this;
         }

        
Mark all logged metrics with the given marker.

Parameters:
marker an SLF4J org.slf4j.Marker
Returns:
this
 
         public Builder markWith(Marker marker) {
             this. = marker;
             return this;
         }

        
Convert rates to the given time unit.

Parameters:
rateUnit a unit of time
Returns:
this
 
         public Builder convertRatesTo(TimeUnit rateUnit) {
             this. = rateUnit;
             return this;
         }

        
Convert durations to the given time unit.

Parameters:
durationUnit a unit of time
Returns:
this
 
         public Builder convertDurationsTo(TimeUnit durationUnit) {
             this. = durationUnit;
             return this;
         }

        
Only report metrics which match the given filter.

Parameters:
filter a MetricFilter
Returns:
this
 
        public Builder filter(MetricFilter filter) {
            this. = filter;
            return this;
        }

        
Builds a Slf4jReporter with the given properties.

Returns:
a Slf4jReporter
        public Slf4jReporter build() {
            return new Slf4jReporter();
        }
    }
    private final Logger logger;
    private final Marker marker;
    private Slf4jReporter(MetricRegistry registry,
                          Logger logger,
                          Marker marker,
                          TimeUnit rateUnit,
                          TimeUnit durationUnit,
                          MetricFilter filter) {
        super(registry"logger-reporter"filterrateUnitdurationUnit);
        this. = logger;
        this. = marker;
    }
    @Override
    public void report(SortedMap<StringGaugegauges,
                       SortedMap<StringCountercounters,
                       SortedMap<StringHistogramhistograms,
                       SortedMap<StringMetermeters,
                       SortedMap<StringTimertimers) {
        for (Entry<StringGaugeentry : gauges.entrySet()) {
            logGauge(entry.getKey(), entry.getValue());
        }
        for (Entry<StringCounterentry : counters.entrySet()) {
            logCounter(entry.getKey(), entry.getValue());
        }
        for (Entry<StringHistogramentry : histograms.entrySet()) {
            logHistogram(entry.getKey(), entry.getValue());
        }
        for (Entry<StringMeterentry : meters.entrySet()) {
            logMeter(entry.getKey(), entry.getValue());
        }
        for (Entry<StringTimerentry : timers.entrySet()) {
            logTimer(entry.getKey(), entry.getValue());
        }
    }
    private void logTimer(String nameTimer timer) {
        final Snapshot snapshot = timer.getSnapshot();
        .info(,
                    "type=TIMER, name={}, count={}, min={}, max={}, mean={}, stddev={}, median={}, " +
                            "p75={}, p95={}, p98={}, p99={}, p999={}, mean_rate={}, m1={}, m5={}, " +
                            "m15={}, rate_unit={}, duration_unit={}",
                    name,
                    timer.getCount(),
                    convertDuration(snapshot.getMin()),
                    convertDuration(snapshot.getMax()),
                    convertDuration(snapshot.getMean()),
                    convertDuration(snapshot.getStdDev()),
                    convertDuration(snapshot.getMedian()),
                    convertDuration(snapshot.get75thPercentile()),
                    convertDuration(snapshot.get95thPercentile()),
                    convertDuration(snapshot.get98thPercentile()),
                    convertDuration(snapshot.get99thPercentile()),
                    convertDuration(snapshot.get999thPercentile()),
                    convertRate(timer.getMeanRate()),
                    convertRate(timer.getOneMinuteRate()),
                    convertRate(timer.getFiveMinuteRate()),
                    convertRate(timer.getFifteenMinuteRate()),
                    getRateUnit(),
                    getDurationUnit());
    }
    private void logMeter(String nameMeter meter) {
        .info(,
                    "type=METER, name={}, count={}, mean_rate={}, m1={}, m5={}, m15={}, rate_unit={}",
                    name,
                    meter.getCount(),
                    convertRate(meter.getMeanRate()),
                    convertRate(meter.getOneMinuteRate()),
                    convertRate(meter.getFiveMinuteRate()),
                    convertRate(meter.getFifteenMinuteRate()),
                    getRateUnit());
    }
    private void logHistogram(String nameHistogram histogram) {
        final Snapshot snapshot = histogram.getSnapshot();
        .info(,
                    "type=HISTOGRAM, name={}, count={}, min={}, max={}, mean={}, stddev={}, " +
                            "median={}, p75={}, p95={}, p98={}, p99={}, p999={}",
                    name,
                    histogram.getCount(),
                    snapshot.getMin(),
                    snapshot.getMax(),
                    snapshot.getMean(),
                    snapshot.getStdDev(),
                    snapshot.getMedian(),
                    snapshot.get75thPercentile(),
                    snapshot.get95thPercentile(),
                    snapshot.get98thPercentile(),
                    snapshot.get99thPercentile(),
                    snapshot.get999thPercentile());
    }
    private void logCounter(String nameCounter counter) {
        .info("type=COUNTER, name={}, count={}"namecounter.getCount());
    }
    private void logGauge(String nameGauge gauge) {
        .info("type=GAUGE, name={}, value={}"namegauge.getValue());
    }
    @Override
    protected String getRateUnit() {
        return "events/" + super.getRateUnit();
    }
New to GrepCode? Check out our FAQ X