net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder Java Examples

The following examples show how to use net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LogstashTests.java    From godaddy-logger with MIT License 5 votes vote down vote up
private Map<String, Object> getJson() throws IOException {
    OutputStreamAppender<ILoggingEvent> appender = (OutputStreamAppender<ILoggingEvent>) testLogger.getAppender("loggingEventCompositeJsonEncoderAppender");
    LoggingEventCompositeJsonEncoder encoder = (LoggingEventCompositeJsonEncoder) appender.getEncoder();

    String encodedValue = new String(encoder.encode(listAppender.list.get(0)), "UTF-8");

    return jsonFactory.createParser(encodedValue).readValueAs(new TypeReference<Map<String, Object>>() {});
}
 
Example #2
Source File: LogzioLogbackAppenderTest.java    From logzio-logback-appender with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncoder() {
    String token = "testEncoder";
    String type = "testEncoder" + random(8);
    String loggerName = "testEncoder" + random(8);
    int drainTimeout = 1;
    String message1 = "Just a log - " + random(5);

    LoggingEventCompositeJsonEncoder encoder = new LoggingEventCompositeJsonEncoder();
    encoder.getProviders().addProvider(new LogLevelJsonProvider());
    encoder.getProviders().addProvider(new MessageJsonProvider());
    encoder.getProviders().addProvider(withPattern(String.format("{ \"loglevel\": \"%s\" }", "INFO"), new LoggingEventPatternJsonProvider()));
    encoder.getProviders().addProvider(withPattern(String.format("{ \"type\": \"%s\" }", type), new LoggingEventPatternJsonProvider()));
    encoder.getProviders().addProvider(withPattern(String.format("{ \"logger\": \"%s\" }", loggerName), new LoggingEventPatternJsonProvider()));
    encoder.getProviders().addProvider(withName("timestamp", new LoggingEventFormattedTimestampJsonProvider()));

    logzioLogbackAppender.setEncoder(encoder);

    Logger testLogger = createLogger(logzioLogbackAppender, "testEncoder", type, loggerName, drainTimeout, false, false, null, false);
    encoder.start();

    testLogger.info(message1);

    sleepSeconds(2 * drainTimeout);

    mockListener.assertNumberOfReceivedMsgs(1);
    MockLogzioBulkListener.LogRequest logRequest = mockListener.assertLogReceivedByMessage(message1);
    mockListener.assertLogReceivedIs(logRequest, token, type, loggerName, Level.INFO.levelStr);
}