/**
* A simple reporters based on ConsoleReporter which prints out application metrics to SLF4J periodically.
*/
public class Slf4jReporter extends ConsoleReporter {
/** SLF4J Logger */
private static final Logger logger = LoggerFactory.getLogger(Slf4jReporter.class.getName());
/** OutputStream linked to ConsoleReporter */
private final ByteArrayOutputStream baos;
/**
* Enables the reporter and causes it to print to
* STDOUT with the specified period.
*
* @param period the period between successive outputs
* @param unit the time unit of {@code period}
*/
public static void enable(long period, TimeUnit unit) {
final Slf4jReporter reporter = new Slf4jReporter(new ByteArrayOutputStream());
reporter.start(period, unit);
}
/**
* Creates a new {@link Slf4jReporter} for the default metrics registry, with unrestricted
* output.
*
* @param baos the {@link ByteArrayOutputStream} to which output will be written
*/
public Slf4jReporter(ByteArrayOutputStream baos) {
super(new PrintStream(baos));
this.baos = baos;
}
@Override
public void run() {
super.run();
baos.reset();
}
}