class LogstashEventFormat implements LoggingEventFormat {

    public String toString(PaxLoggingEvent event) {
        JsonObjectBuilder object = Json.createObjectBuilder();
        try {
            object.add(MESSAGE, event.getMessage());
            object.add(SOURCE, event.getLoggerName());
            object.add(TIMESTAMP, TIMESTAMP_FORMAT.format(new Date(event.getTimeStamp())));

            JsonObjectBuilder fields = Json.createObjectBuilder();
            for (Object property : event.getProperties().entrySet()) {
                Map.Entry<String, Object> entry = (Map.Entry<String, Object>) property;
                fields.add(entry.getKey(), entry.getValue().toString());
            }

            object.add(FIELDS, fields);

            JsonArrayBuilder tags = Json.createArrayBuilder();
            tags.add(event.getLevel().toString());
            object.add(TAGS, tags);
        } catch (JsonException e) {
            // let's return a minimal, String-based message representation instead
            return "{ \"" + MESSAGE + "\" : " + event.getMessage() + "}";
        }
        return object.build().toString();
    }

}

class LogstashEventFormatTest {

    private final LoggingEventFormat format;

    @Test
    public void testBasicLogstashFormat() throws JsonException {

        PaxLoggingEvent event = MockEvents.createInfoEvent();

        JsonObject object = Json.createReader(new StringReader(format.toString(event))).readObject();
        assertEquals(MockEvents.LOG_MESSAGE, object.getString(LogstashEventFormat.MESSAGE));
        assertEquals(MockEvents.LOGGER_NAME, object.getString(LogstashEventFormat.SOURCE));
        assertEquals("INFO", object.getJsonArray(LogstashEventFormat.TAGS).getString(0));
        assertEquals(MockEvents.LOG_PROPERTY_VALUE,
                     object.getJsonObject(LogstashEventFormat.FIELDS).getString(MockEvents.LOG_PROPERTY_ID));
        assertNotNull(object.get(LogstashEventFormat.TIMESTAMP));

        System.out.println(object);
    }

}