Reporter that prints reports using SLF4J ?

541 views
Skip to first unread message

homeration

unread,
Sep 18, 2012, 12:01:21 PM9/18/12
to metric...@googlegroups.com
Hello to Metrics community,

I'm currently using the ConsoleReporter that log directly to System.out.
Now, I would like to control the ConsoleReporter output with SLF4J.

Basically, A new Slf4jReporter that would work like the ConsoleReporter but prints reports using SLF4J Api.
In particular, that will allow me to print reports to a specified log file.

Do you plan to add this kind of Reporter ?

Thx, 
homeration

Clint Checketts

unread,
Sep 18, 2012, 12:22:41 PM9/18/12
to metric...@googlegroups.com
I'm not a metrics contributor. It is very simple to create you own reporters, but harder for the metrics core guys to maintain 1,000 tiny reporters.

I'd recommend copying the ConsoleReporter and writing your own for SLF4J. It is very simple.

Then you could show your new reporter and ask if they want to add it. ;)

-Clint

homeration

unread,
Sep 19, 2012, 3:50:17 AM9/19/12
to metric...@googlegroups.com
I Created a SLF4J Reporter based on ConsoleReporter :

/**
 * 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();
       logger.info(baos.toString());
       baos.reset();
    }

}
Reply all
Reply to author
Forward
0 new messages