recording logs with logback



  <!-- scoped as usual -->
  <!-- no transitive dependency for logback -->


LogRecorder and LogRecorderReflector


Lets say you have a hidden @POST method for testing.

public class TestsResource {

    // tests HisService by calling doSome().
    // you can see the log file.
    public Response hisService() {
        logger.debug("testing his service...");;
        return Response.noConent().build();

You have to connect to the server and tail the log file even if you execute the resource like this.

$ curl -X POST

Let’s make another method which returns some buffered logs.

public class TestsResource {

    public Response hisServiceLog() throws ReflectiveOperationException {
        // start recording.
        // logger, pattern, charset, limit
        recorder = LogRecorderReflector.start(
            getLogger(Logger.ROOT_LOGGER_NAME), "%message%n",
            StandardCharsets.UTF_8, 8192);
        try {
        } catch (final Exception e) {
            logger.error("error while executing fileBack()", e);
        // finish recording and get the records.
        final String records = LogRecorderReflector.finish(recorder);
        return Response.ok(records).build();        

Now we can see those buffered logs in a local console or a browser window.

$ curl
I'm doin' some baby.

One comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s