/** * Return the value associated with an MDC entry designated by the Key * property. If that value is null, then return the value assigned to the * DefaultValue property. */ public String getDiscriminatingValue(ILoggingEvent event) { // http://jira.qos.ch/browse/LBCLASSIC-213 Map<String, String> mdcMap = event.getMDCPropertyMap(); if (mdcMap == null) { return defaultValue; } String mdcValue = mdcMap.get(key); if (mdcValue == null) { return defaultValue; } else { return mdcValue; } }
/** * A synonym for {@link #getMDCPropertyMap}. * @deprecated Use {@link #getMDCPropertyMap()}. */ @Override @Deprecated public Map<String, String> getMdc() { return event.getMDCPropertyMap(); }
@Override public String convert(ILoggingEvent event) { Map<String, String> mdcPropertyMap = event.getMDCPropertyMap(); if (mdcPropertyMap == null) { return defaultValue; } if (key == null) { return outputMDCForAllKeys(mdcPropertyMap); } else { String value = mdcPropertyMap.get(key); if (value != null) { return value; } else { return defaultValue; } } }
Map<String, String> mergePropertyMaps(ILoggingEvent event) { Map<String, String> mergedMap = new HashMap<String, String>(); // we add the context properties first, then the event properties, since // we consider that event-specific properties should have priority over // context-wide properties. Map<String, String> loggerContextMap = event.getLoggerContextVO().getPropertyMap(); Map<String, String> mdcMap = event.getMDCPropertyMap(); if (loggerContextMap != null) { mergedMap.putAll(loggerContextMap); } if (mdcMap != null) { mergedMap.putAll(mdcMap); } return mergedMap; }
public static short computeReferenceMask(ILoggingEvent event) { short mask = 0; int mdcPropSize = 0; if (event.getMDCPropertyMap() != null) { mdcPropSize = event.getMDCPropertyMap().keySet().size(); } int contextPropSize = 0; if (event.getLoggerContextVO().getPropertyMap() != null) { contextPropSize = event.getLoggerContextVO().getPropertyMap().size(); } if (mdcPropSize > 0 || contextPropSize > 0) { mask = PROPERTIES_EXIST; } if (event.getThrowableProxy() != null) { mask |= EXCEPTION_EXISTS; } return mask; } }
Map<String, String> propertyMap = event.getMDCPropertyMap();
values[i++] = loggingEvent.getMDCPropertyMap();
@Override protected void append(ILoggingEvent eventObject) { final RequestContext ctx = RequestContext.mapCurrent(Function.identity(), () -> null); if (ctx != null) { final State state = state(ctx); final RequestLog log = ctx.log(); final Set<RequestLogAvailability> availabilities = log.availabilities(); // Note: This equality check is extremely fast. // See RequestLogAvailabilitySet for more information. if (!availabilities.equals(state.availabilities)) { state.availabilities = availabilities; export(state, ctx, log); } final Map<String, String> originalMdcMap = eventObject.getMDCPropertyMap(); final Map<String, String> mdcMap; // Create a copy of 'state' to avoid the race between: // - the delegate appenders who iterate over the MDC map and // - this class who update 'state'. if (!originalMdcMap.isEmpty()) { mdcMap = new UnionMap<>(state.clone(), originalMdcMap); } else { mdcMap = state.clone(); } eventObject = new LoggingEventWrapper(eventObject, mdcMap); } aai.appendLoopOnAppenders(eventObject); }
@Test public void testWithoutContext() { final List<ILoggingEvent> events = prepare(); final ILoggingEvent e = log(events); assertThat(e.getMDCPropertyMap()).isEmpty(); }
@Override protected Map<String, Object> toJsonMap(ILoggingEvent event) { final MapBuilder mapBuilder = new MapBuilder(timestampFormatter, customFieldNames, additionalFields, includes.size()) .addTimestamp("timestamp", isIncluded(EventAttribute.TIMESTAMP), event.getTimeStamp()) .add("level", isIncluded(EventAttribute.LEVEL), () -> String.valueOf(event.getLevel())) .add("thread", isIncluded(EventAttribute.THREAD_NAME), event::getThreadName) .add("logger", isIncluded(EventAttribute.LOGGER_NAME), event::getLoggerName) .add("message", isIncluded(EventAttribute.MESSAGE), event::getFormattedMessage) .add("context", isIncluded(EventAttribute.CONTEXT_NAME), () -> event.getLoggerContextVO().getName()) .add("version", jsonProtocolVersion != null, jsonProtocolVersion) .add("exception", isIncluded(EventAttribute.EXCEPTION) && event.getThrowableProxy() != null, () -> throwableProxyConverter.convert(event)); final boolean includeMdc = isIncluded(EventAttribute.MDC); if (flattenMdc) { filterMdc(event.getMDCPropertyMap()).forEach((k,v) -> mapBuilder.add(k, includeMdc, v)); } else { mapBuilder.addMap("mdc", includeMdc, () -> filterMdc(event.getMDCPropertyMap())); } final boolean includeCallerData = isIncluded(EventAttribute.CALLER_DATA); final StackTraceElement[] callerData = event.getCallerData(); if (includeCallerData && callerData.length >= 1) { final StackTraceElement stackTraceElement = callerData[0]; mapBuilder.add("caller_class_name", includeCallerData, stackTraceElement.getClassName()); mapBuilder.add("caller_method_name", includeCallerData, stackTraceElement.getMethodName()); mapBuilder.add("caller_file_name", includeCallerData, stackTraceElement.getFileName()); mapBuilder.addNumber("caller_line_number", includeCallerData, stackTraceElement.getLineNumber()); } return mapBuilder.build(); }
@Test public void testMdcPropertyPreservation() throws Exception { final List<ILoggingEvent> events = prepare(a -> a.addBuiltIn(BuiltInProperty.REQ_DIRECTION)); MDC.put("some-prop", "some-value"); final ServiceRequestContext ctx = newServiceContext("/foo", null); try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("req.direction", "INBOUND") .containsEntry("some-prop", "some-value") .hasSize(2); } finally { MDC.remove("some-prop"); } }
public static LoggingEventVO build(ILoggingEvent le) { LoggingEventVO ledo = new LoggingEventVO(); ledo.loggerName = le.getLoggerName(); ledo.loggerContextVO = le.getLoggerContextVO(); ledo.threadName = le.getThreadName(); ledo.level = (le.getLevel()); ledo.message = (le.getMessage()); ledo.argumentArray = (le.getArgumentArray()); ledo.marker = le.getMarker(); ledo.mdcPropertyMap = le.getMDCPropertyMap(); ledo.timeStamp = le.getTimeStamp(); ledo.throwableProxy = ThrowableProxyVO.build(le.getThrowableProxy()); // add caller data only if it is there already // fixes http://jira.qos.ch/browse/LBCLASSIC-145 if (le.hasCallerData()) { ledo.callerDataArray = le.getCallerData(); } return ledo; }
/** * Return the value associated with an MDC entry designated by the Key * property. If that value is null, then return the value assigned to the * DefaultValue property. */ public String getDiscriminatingValue(ILoggingEvent event) { // http://jira.qos.ch/browse/LBCLASSIC-213 Map<String, String> mdcMap = event.getMDCPropertyMap(); if (mdcMap == null) { return defaultValue; } String mdcValue = mdcMap.get(key); if (mdcValue == null) { return defaultValue; } else { return mdcValue; } }
@Override public String convert(ILoggingEvent event) { Map<String, String> mdcPropertyMap = event.getMDCPropertyMap(); if (mdcPropertyMap == null) { return defaultValue; } if (key == null) { return outputMDCForAllKeys(mdcPropertyMap); } else { String value = event.getMDCPropertyMap().get(key); if (value != null) { return value; } else { return defaultValue; } } }
Map<String, String> mergePropertyMaps(ILoggingEvent event) { Map<String, String> mergedMap = new HashMap<String, String>(); // we add the context properties first, then the event properties, since // we consider that event-specific properties should have priority over // context-wide properties. Map<String, String> loggerContextMap = event.getLoggerContextVO() .getPropertyMap(); Map<String, String> mdcMap = event.getMDCPropertyMap(); if (loggerContextMap != null) { mergedMap.putAll(loggerContextMap); } if (mdcMap != null) { mergedMap.putAll(mdcMap); } return mergedMap; }
@Test public void testClientContextWithMinimalLogs() throws Exception { final List<ILoggingEvent> events = prepare(a -> { // Export all properties. for (BuiltInProperty p : BuiltInProperty.values()) { a.addBuiltIn(p); } }); final ClientRequestContext ctx = newClientContext("/foo", "type=bar"); try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "client.com") .containsEntry("local.ip", "5.6.7.8") .containsEntry("local.port", "5678") .containsEntry("remote.host", "server.com") .containsEntry("remote.ip", "1.2.3.4") .containsEntry("remote.port", "8080") .containsEntry("req.direction", "OUTBOUND") .containsEntry("req.authority", "server.com:8080") .containsEntry("req.method", "GET") .containsEntry("req.path", "/foo") .containsEntry("req.query", "type=bar") .containsEntry("scheme", "unknown+h2") .containsEntry("tls.session_id", "0101020305080d15") .containsEntry("tls.proto", "TLSv1.2") .containsEntry("tls.cipher", "some-cipher") .hasSize(15); } }
try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "server.com") .containsEntry("local.ip", "5.6.7.8")
final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "server.com") .containsEntry("local.ip", "5.6.7.8")
final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "client.com") .containsEntry("local.ip", "5.6.7.8")
final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "server.com") .containsEntry("local.ip", "5.6.7.8")