reines / dropwizard-logging-loggly

Dropwizard logging support for Loggly.
Apache License 2.0
7 stars 6 forks source link

Is it possible to send json format ? #2

Open smallufo opened 9 years ago

smallufo commented 9 years ago

Hi , is it possible to send json format ? With json format , it is possible to append thread / logger / context ... info.

smallufo commented 9 years ago

It supports json format. FYI :

    final LogglyAppender<ILoggingEvent> appender = new LogglyAppender<>();
    final String tagName = tag.orElse(applicationName);

    JsonLayout jsonLayout = new JsonLayout();
    jsonLayout.setJsonFormatter(new JacksonJsonFormatter());
    jsonLayout.setAppendLineSeparator(true);
    jsonLayout.setContext(context);
    jsonLayout.start();
    buildLayout(context, TimeZone.getDefault());

    LayoutWrappingEncoder encoder = new LayoutWrappingEncoder();
    encoder.setLayout(jsonLayout);
    encoder.setContext(context);
    encoder.start();

    appender.setName("loggly-appender");
    appender.setContext(context);
    appender.setLayout(jsonLayout);
    appender.setEndpointUrl(String.format(ENDPOINT_URL_TEMPLATE, server, token, tagName));

    addThresholdFilter(appender, threshold);
    appender.start();

    return wrapAsync(appender);
reines commented 9 years ago

Could you submit a PR with this? I'll happily merge it.

smallufo commented 9 years ago

Sorry, I am not familiar with git. Just use/merge it if you want .

reines commented 9 years ago

I've commit this in c4d2b13c6cdd490d80bd9eb51cfd64f12f211e31. It should be build and the snapshot pushed to oss.sonatype.org shortly.

I haven't actually tested this yet so please let me know how you get on, and if all is good I'll push 0.6 to maven central.

charlieg commented 9 years ago

How about passing a string in the log message that Loggly will parse as JSON?

For example, if I log something like this:

logger.info("{\"hello\": \"world\", \"foo\": \"bar\"}");

the json record shows up in Loggly like this:

json:
thread: dw-83 - POST /api/create
level: INFO
timestamp: 1439689242730
context: default
logger: com.example.MyClass
message: {"hello": "world", "foo": "bar"}

and the Raw Message looks like this:

{"timestamp":"1439689242730","level":"INFO","thread":"dw-83 - POST /api/create","logger":"com.example.MyClass","message":"{\"hello\": \"world\", \"foo\": \"bar\"}","context":"default"}

As you can see, the JSON message I'm passing isn't being parsed by Loggly. I suspect this is due to the double-quote preceding the left-brace in the raw message.

Any suggestions?

charlieg commented 9 years ago

Changing nothing in my code except using dropwizard-logging-loggly v0.5 instead of v0.6 results in the JSON message being properly parsed by Loggly. However, this means that the other log info (e.g., thread, level, timestamp, logger) aren't formatted as JSON. This leads me to believe there's something funky with the message formatting in v0.6.

Still investigating...

charlieg commented 9 years ago

Unless I'm missing something, it looks like the only options are to use dropwizard-logging-loggly v0.5 (which means you lose JSON formatting of thread, level, timestamp, logger, etc.) or MDC (which means you lose the ability to produce hierarchical JSON messages).