Class MdcJsonProvider

All Implemented Interfaces:
ContextAware, FieldNamesAware<LogstashFieldNames>, JsonProvider<ILoggingEvent>

public class MdcJsonProvider extends AbstractFieldJsonProvider<ILoggingEvent> implements FieldNamesAware<LogstashFieldNames>
Includes MDC properties in the JSON output according to includeMdcKeyNames and excludeMdcKeyNames.

There are three valid combinations of includeMdcKeyNames and excludeMdcKeyNames:

  1. When includeMdcKeyNames and excludeMdcKeyNames are both empty, then all entries will be included.
  2. When includeMdcKeyNames is not empty and excludeMdcKeyNames is empty, then only those entries with key names in includeMdcKeyNames will be included.
  3. When includeMdcKeyNames is empty and excludeMdcKeyNames is not empty, then all entries except those with key names in excludeMdcKeyNames will be included.

It is a configuration error for both includeMdcKeyNames and excludeMdcKeyNames to be not empty.

By default, for each entry in the MDC, the MDC key is output as the field name. This can be changed by specifying an explicit field name to use for an MDC key via addMdcKeyFieldName(String)

If the fieldName is set, then the properties will be written to that field as a subobject. Otherwise, the properties are written inline.

  • Constructor Details

    • MdcJsonProvider

      public MdcJsonProvider()
  • Method Details

    • start

      public void start()
      Description copied from interface: JsonProvider
      Start the provider after all configuration properties are set.
      Specified by:
      start in interface JsonProvider<ILoggingEvent>
      Overrides:
      start in class AbstractJsonProvider<ILoggingEvent>
    • writeTo

      public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException
      Description copied from interface: JsonProvider
      Writes information about the event, to the given generator.

      When called, the generator is assumed to be within a JSON object context (i.e. this provider should write fields and their values to the generator). Upon return, the generator should be within the same JSON object context.

      Specified by:
      writeTo in interface JsonProvider<ILoggingEvent>
      Parameters:
      generator - the JsonGenerator to produce JSON content
      event - the event to convert into JSON
      Throws:
      IOException - if an I/O error occurs
    • setFieldNames

      public void setFieldNames(LogstashFieldNames fieldNames)
      Specified by:
      setFieldNames in interface FieldNamesAware<LogstashFieldNames>
    • getIncludeMdcKeyNames

      public List<String> getIncludeMdcKeyNames()
    • addIncludeMdcKeyName

      public void addIncludeMdcKeyName(String includedMdcKeyName)
    • setIncludeMdcKeyNames

      public void setIncludeMdcKeyNames(List<String> includeMdcKeyNames)
    • getExcludeMdcKeyNames

      public List<String> getExcludeMdcKeyNames()
    • addExcludeMdcKeyName

      public void addExcludeMdcKeyName(String excludedMdcKeyName)
    • setExcludeMdcKeyNames

      public void setExcludeMdcKeyNames(List<String> excludeMdcKeyNames)
    • getMdcKeyFieldNames

      public Map<String,String> getMdcKeyFieldNames()
    • addMdcKeyFieldName

      public void addMdcKeyFieldName(String mdcKeyFieldName)
      Adds the given mdcKeyFieldName entry in the form mdcKeyName=fieldName to use an alternative field name for an MDC key.
      Parameters:
      mdcKeyFieldName - a string in the form mdcKeyName=fieldName that identifies what field name to use for a specific MDC key.