Java Code Examples for ch.qos.logback.classic.spi.LoggingEvent#setMDCPropertyMap()

The following examples show how to use ch.qos.logback.classic.spi.LoggingEvent#setMDCPropertyMap() . 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: GelfEncoderTest.java    From logback-gelf with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void numericValueAsNumber() throws IOException {
    encoder.start();

    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger logger = lc.getLogger(LOGGER_NAME);

    final LoggingEvent event = simpleLoggingEvent(logger, null);

    event.setMDCPropertyMap(ImmutableMap.of("int", "200", "float", "0.00001"));

    final String logMsg = encodeToStr(event);

    final ObjectMapper om = new ObjectMapper();
    final JsonNode jsonNode = om.readTree(logMsg);
    basicValidation(jsonNode);

    assertTrue(logMsg.contains("\"_int\":200"));
    assertTrue(logMsg.contains("\"_float\":0.00001"));
}
 
Example 2
Source File: GelfEncoderTest.java    From logback-gelf with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void numericValueAsString() throws IOException {
    encoder.setNumbersAsString(true);
    encoder.start();

    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger logger = lc.getLogger(LOGGER_NAME);

    final LoggingEvent event = simpleLoggingEvent(logger, null);

    event.setMDCPropertyMap(ImmutableMap.of("int", "200", "float", "0.00001"));

    final String logMsg = encodeToStr(event);

    final ObjectMapper om = new ObjectMapper();
    final JsonNode jsonNode = om.readTree(logMsg);
    basicValidation(jsonNode);

    assertTrue(logMsg.contains("\"_int\":\"200\""));
    assertTrue(logMsg.contains("\"_float\":\"0.00001\""));
}
 
Example 3
Source File: LogbackUtilTest.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
@Test
public void testTranslate() throws Exception {
  //Given
  LoggerContext context = new LoggerContext();
  Logger logger = context.getLogger("SomeLogger");
  LoggingEvent le = new LoggingEvent("a.b.C",logger, ch.qos.logback.classic.Level.INFO,"message",null,null);
  final Map<String, String> split = Splitter.on(",").withKeyValueSeparator("=").split("a=b,b=as");
  le.setMDCPropertyMap(split);
  le.setCallerData(new StackTraceElement[]{new StackTraceElement("a.b.C","someMethod","C.java",120)});

  //when
  final LogData logData = LogbackUtil.translate(le).build();

  //then
  assertEquals(logData.getMessage(),"message");
  assertEquals(logData.getLevel(),Level.INFO);
  assertEquals(logData.getLoggerName(),"SomeLogger");
  assertEquals(logData.getClazz(),"a.b.C");
  assertEquals(logData.getMethod(),"someMethod");
  assertEquals(logData.getLine(),"120");
  assertEquals(logData.getProperties(),split);
}
 
Example 4
Source File: GelfEncoderTest.java    From logback-gelf with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void complex() throws IOException {
    encoder.setIncludeRawMessage(true);
    encoder.setIncludeLevelName(true);
    encoder.addStaticField("foo:bar");
    encoder.setIncludeCallerData(true);
    encoder.setIncludeRootCauseData(true);
    encoder.start();

    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger logger = lc.getLogger(LOGGER_NAME);

    final LoggingEvent event = simpleLoggingEvent(logger, null);

    event.setMDCPropertyMap(ImmutableMap.of("mdc_key", "mdc_value"));

    final String logMsg = encodeToStr(event);

    final ObjectMapper om = new ObjectMapper();
    final JsonNode jsonNode = om.readTree(logMsg);
    basicValidation(jsonNode);
    assertEquals("DEBUG", jsonNode.get("_level_name").textValue());
    assertEquals("bar", jsonNode.get("_foo").textValue());
    assertEquals("mdc_value", jsonNode.get("_mdc_key").textValue());
    assertEquals("message {}", jsonNode.get("_raw_message").textValue());
    assertNull(jsonNode.get("_exception"));
}
 
Example 5
Source File: JobMessageLogAppenderTest.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
private LoggingEvent createLoggingEvent(final Level level,
                                        final String message,
                                        final Object... params) {
    final LoggingEvent loggingEvent = new LoggingEvent();
    loggingEvent.setMDCPropertyMap(singletonMap("job_id", "someJobId"));
    loggingEvent.setMessage(message);
    loggingEvent.setArgumentArray(params);
    loggingEvent.setLevel(level);
    loggingEvent.setMarker(JobMarker.JOB);
    return loggingEvent;
}
 
Example 6
Source File: GelfAppender.java    From logback-gelf-appender with Apache License 2.0 4 votes vote down vote up
@Override
protected void append(ILoggingEvent event) {

    if (event == null) {
        return;
    }

    // create a copy of the logging event to avoid passing exception stacktraces to GELF's short_message field
    LoggingEvent copy = new LoggingEvent();
    copy.setMessage(event.getMessage());
    copy.setLevel(event.getLevel());
    copy.setArgumentArray(event.getArgumentArray());
    copy.setLoggerName(event.getLoggerName());
    copy.setThreadName(event.getThreadName());
    copy.setTimeStamp(event.getTimeStamp());
    copy.setMDCPropertyMap(event.getMDCPropertyMap());

    final GelfMessageBuilder builder = new GelfMessageBuilder(this.layout.doLayout(copy), hostName).timestamp(
                event.getTimeStamp() / 1000d)
            .level(GelfMessageLevel.fromNumericLevel(toGelfNumericValue(event.getLevel())))
            .additionalField("loggerName", event.getLoggerName())
            .additionalField("threadName", event.getThreadName());

    final Marker marker = event.getMarker();

    if (marker != null) {
        builder.additionalField("marker", marker.getName());
    }

    if (includeMDC) {
        for (Map.Entry<String, String> entry : event.getMDCPropertyMap().entrySet()) {
            builder.additionalField(entry.getKey(), entry.getValue());
        }
    }

    final StackTraceElement[] callerData = event.getCallerData();

    if (includeSource && event.hasCallerData() && callerData.length > 0) {
        StackTraceElement source = callerData[0];

        builder.additionalField("sourceFileName", source.getFileName());
        builder.additionalField("sourceMethodName", source.getMethodName());
        builder.additionalField("sourceClassName", source.getClassName());
        builder.additionalField("sourceLineNumber", source.getLineNumber());
    }

    final IThrowableProxy thrown = event.getThrowableProxy();

    if (includeStackTrace && thrown != null) {
        String convertedThrowable = throwableConverter.convert(event);

        builder.additionalField("exceptionClass", thrown.getClassName());
        builder.additionalField("exceptionMessage", thrown.getMessage());
        builder.additionalField("exceptionStackTrace", convertedThrowable);

        builder.fullMessage(event.getFormattedMessage() + "\n\n" + convertedThrowable);
    } else {
        builder.fullMessage(event.getFormattedMessage());
    }

    if (includeLevelName) {
        builder.additionalField("levelName", event.getLevel().levelStr);
    }

    if (!additionalFields.isEmpty()) {
        builder.additionalFields(additionalFields);
    }

    if(!client.trySend(builder.build())) {
        addError("Failed to write log event to the GELF server using trySend");
    }
}