diagnosing system with jax-rs


@Path("/system/diagnoses")
public class DiagnosesResource {

    private static final Logger logger
            = getLogger(DiagnosesResource.class.getName());

    private Response response() {
        handler.flush();
        return Response.ok(baos.toByteArray(), MediaType.TEXT_PLAIN).build();
    }

    @PostConstruct
    private void onPostConstruct() {
        baos = new ByteArrayOutputStream();
        formatter = new SimpleFormatter();
        handler = new StreamHandler(baos, formatter);
        logger.addHandler(handler);
    }

    @PreDestroy
    private void onPreDestroy() {
        logger.removeHandler(handler);
        handler = null;
        formatter = null;
        baos = null;
    }

    private ByteArrayOutputStream baos;

    private Formatter formatter;

    private Handler handler;
}

Now we can add some methods for diagnosing.

@POST
@Path("/some")
@Produces({MediaType.TEXT_PLAIN})
public Response some() {
    // diagnose here with some logs
    return response();
}
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s