Java 类org.slf4j.spi.MDCAdapter 实例源码

项目:eaudit4j    文件:Slf4jProcessorTest.java   
/**
 * Test that the MDC contains the audit stream field, and the correct audit stream and value
 */
@Test
public void testDefaultAuditStreamInMdc()
        throws InvocationTargetException, IllegalAccessException {

    Slf4jProperties slf4jProps = MapBasedSlf4jPropsBuilder.buildDefault();
    CommonProperties commonProps = MapBasedCommonPropsBuilder.buildDefault();
    Processor processor = new Slf4jProcessor();
    processor.init(commonProps);

    String auditStreamName = "Test audit stream name";

    MDCAdapter adapter = (MDCAdapter) method_addAuditStreamNameToMdc.invoke(processor, auditStreamName, slf4jProps);

    String fieldName = slf4jProps.getAuditStreamFieldName();

    String error = "The MDC is missing the audit stream name field key";
    assertThat(error, adapter.get(fieldName), is(not(nullValue())));
    error = "The MDC accepts any field name";
    assertThat(error, adapter.get(fieldName + "_invalid"), is(nullValue()));
    error = "The MDC does not return the correct value";
    assertThat(error, adapter.get(fieldName), is(equalTo(auditStreamName)));
}
项目:konker-platform    文件:KonkerLoggingEvent.java   
public Map<String, String> getMDCPropertyMap() {
    if(this.mdcPropertyMap == null) {
        MDCAdapter mdc = MDC.getMDCAdapter();
        if(mdc instanceof LogbackMDCAdapter) {
            this.mdcPropertyMap = ((LogbackMDCAdapter)mdc).getPropertyMap();
        } else {
            this.mdcPropertyMap = mdc.getCopyOfContextMap();
        }
    }

    if(this.mdcPropertyMap == null) {
        this.mdcPropertyMap = CACHED_NULL_MAP;
    }

    return this.mdcPropertyMap;
}
项目:HttpSessionReplacer    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of
 * {@link StaticMDCBinder}.
 * @return
 */
public MDCAdapter getMDCA() {
  try {
    return (MDCAdapter) Class.forName(getMDCAdapterClassStr()).newInstance();
  } catch (Exception e) { // NOSONAR we can't log here as we are actually instantiating log here
    System.err.println("Unable to instantiate mdc adapter " + getMDCAdapterClassStr()); // NOSONAR
    e.printStackTrace(); // NOSONAR
    return new NOPMDCAdapter();
  }
}
项目:eaudit4j    文件:Slf4jProcessor.java   
/**
 * Add the audit stream name to the MDC
 *
 * @param auditStreamName The audit stream name to add to the MDC
 * @param pProperties     The processor configuration
 * @return The {@code MDCAdapter} that is used in the modified MDC
 */
private MDCAdapter addAuditStreamNameToMdc(final String auditStreamName, final Slf4jProperties pProperties) {

    MDC.put(pProperties.getAuditStreamFieldName(), auditStreamName);

    // return the MDC Adapter
    // (the MDC is global, so the MDC Adapter is not really used in this class, but it is very helpful in unit
    // testing to get access to the underlying map without making costly copies)
    return MDC.getMDCAdapter();
}
项目:eaudit4j    文件:Slf4jProcessor.java   
/**
 * Add the serialized event JSON to the MDC
 *
 * @param serializedEvent The serialized event to add to the MDC
 * @param pProperties      The processor configuration
 * @return The {@code MDCAdapter} that is used in the modified MDC
 */
private MDCAdapter addSerializedEventToMdc(final String serializedEvent, final Slf4jProperties pProperties) {

    MDC.put(pProperties.getSerializedEventFieldName(), serializedEvent);

    // return the MDC Adapter
    // (the MDC is global, so the MDC Adapter is not really used in this class, but it is very helpful in unit
    // testing to get access to the underlying map without making costly copies)
    return MDC.getMDCAdapter();
}
项目:eaudit4j    文件:Slf4jProcessorTest.java   
/**
 * Test that the MDC contains the serialized event field, and the correct serialized event and value
 */
@Test
public void testSerializedEventInMdc()
        throws InvocationTargetException, IllegalAccessException {

    Slf4jProperties slf4jProps = MapBasedSlf4jPropsBuilder.buildDefault();
    CommonProperties commonProps = MapBasedCommonPropsBuilder.buildDefault();
    Processor processor = new Slf4jProcessor();
    processor.init(commonProps);

    // we could really use an arbitrary string instead of going through the pain of creating an event
    // and serializing it here. However, maybe someone will find this example useful at some point...
    Event event = new EventBuilder(commonProps)
            .setSubject("SubjectId-1234".toCharArray())
            .build();
    String eventJson = String.valueOf(event.toJson(commonProps.getEncoding()));

    MDCAdapter adapter = (MDCAdapter) method_addSerializedEventToMdc.invoke(processor, eventJson, slf4jProps);

    String fieldName = slf4jProps.getSerializedEventFieldName();

    String error = "The MDC is missing the serialized event field key";
    assertThat(error, adapter.get(fieldName), is(not(nullValue())));
    error = "The MDC accepts any field name";
    assertThat(error, adapter.get(fieldName + "_invalid"), is(nullValue()));
    error = "The MDC does not return the correct value";
    assertThat(error, adapter.get(fieldName), is(equalTo(eventJson)));
}
项目:eaudit4j    文件:Slf4jProcessorTest.java   
/**
 * Test that the MDC contains the correct fields from the event, as configured, with the correct names and content
 * <p>
 * - number of fields
 * - field name
 * - field value
 */
@Test
public void testEventFieldsInMdc()
        throws InvocationTargetException, IllegalAccessException {

    Map<String, String> props = new HashMap<>();
    props.put(MapBasedSlf4jPropsBuilder.KEY_MDC_FIELDS,"subject,actor:myActor,invalidField:neverExists");

    Slf4jProperties slf4jProps = MapBasedSlf4jPropsBuilder.build(props);
    CommonProperties commonProps = MapBasedCommonPropsBuilder.build(props);
    Processor processor = new Slf4jProcessor();
    processor.init(commonProps);

    Event event = new EventBuilder(commonProps)
            .setSubject("SubjectId-1234".toCharArray())
            .setObject("ObjectId-3456".toCharArray())
            .setActor("ActorId-5678".toCharArray())
            .setResult("Some result".toCharArray())
            .build();

    MDCAdapter adapter = (MDCAdapter) method_addEventFieldsToMdc.invoke(processor, event, slf4jProps);

    String error = "The MDC does not have the correct number of fields";
    assertThat(error, adapter.getCopyOfContextMap().size(), is(equalTo(2)));

    // Test the subject - we use the default name ("subject") - see configuration above for the MdcFields
    error = "The MDC is missing the correct subject key";
    assertThat(error, adapter.get("subject"), is(not(nullValue())));
    error = "The MDC does not return the correct value for the subject";
    assertThat(error, adapter.get("subject"), is(equalTo("SubjectId-1234")));

    // Test the subject - we use the default name ("myActor") - see configuration above for the MdcFields
    error = "The MDC is missing the correct actor key";
    assertThat(error, adapter.get("myActor"), is(not(nullValue())));
    error = "The MDC does not return the correct value for the actor";
    assertThat(error, adapter.get("myActor"), is(equalTo("ActorId-5678")));
}
项目:bartleby    文件:LoggingEvent.java   
public Map<String, String> getMDCPropertyMap() {
  // populate mdcPropertyMap if null
  if (mdcPropertyMap == null) {
    MDCAdapter mdc = MDC.getMDCAdapter();
    if (mdc instanceof LogbackMDCAdapter)
      mdcPropertyMap = ((LogbackMDCAdapter) mdc).getPropertyMap();
    else
      mdcPropertyMap = mdc.getCopyOfContextMap();
  }
  // mdcPropertyMap still null, use CACHED_NULL_MAP
  if (mdcPropertyMap == null)
    mdcPropertyMap = CACHED_NULL_MAP;

  return mdcPropertyMap;
}
项目:logback-mdc-ttl    文件:TtlMDCAdapter.java   
public static MDCAdapter getInstance() {
    return mtcMDCAdapter;
}
项目:eaudit4j    文件:Slf4jProcessor.java   
/**
 * Add the fields to the MDC as configured
 *
 * @param event       The event to take the fields from
 * @param pProperties The processor configuration
 * @return The {@code MDCAdapter} that is used in the modified MDC
 * @throws AuditException When there is an invalid MDC field list configuration
 */
// Need to validate sizes of the mdcField split to decide whether we use a custom name, or the field name
// We could do that with a null check, but that seems to be awkward.
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
private MDCAdapter addEventFieldsToMdc(final Event event, final Slf4jProperties pProperties)
        throws AuditException {

    // split the configured event name list, and see what we need to add to the MDC
    if (pProperties.getMdcFields() != null && !pProperties.getMdcFields().isEmpty()) {

        // split the list of fields that we need to add to the MDC
        final String[] mdcFieldNames = pProperties.getMdcFields().split(pProperties.getMdcFieldSeparator());

        // Go to the list of configured fields, and check if there is an alias configured.
        // If so, use that alias - otherwise, use the field name directly.
        // Only add the field if it exists in the event (not all events necessarily contain all fields)
        for (final String mdcFieldName : mdcFieldNames) {

            final String[] mdcField = mdcFieldName.split(pProperties.getMdcFieldNameSeparator());
            final Field field; // the actual field
            final String fieldName; // the name we use for the field in the MDC

            if (mdcField.length == 1) {

                // we do not have a dedicated MDC name configured, hence use the field name.
                // first, we check if the field has been set:
                if (event.containsField(mdcField[0])) {

                    // the field exists for this event, which means we can pull it
                    field = event.getField(mdcField[0]);
                    fieldName = field.getName();
                } else {

                    // the field does not exist in this event, hence proceed to the next field
                    continue;
                }
            } else if (mdcField.length == 2) {

                // we do have a dedicated MDC name configured
                // first, we check if the field has been set:
                if (event.containsField(mdcField[0])) {

                    // the field exists for this event, which means we can pull it
                    field = event.getField(mdcField[0]);
                    fieldName = mdcField[1];
                } else {

                    // the field does not exist in this event, hence proceed to the next field
                    continue;
                }
            } else {
                // We have less than 1 and more than 2 field name components.
                // This should never happen.
                final String error = "The Event field name / MDC mapping is invalid. ";
                LOG.warn(error);
                throw new AuditException(AuditErrorConditions.CONFIGURATION, error);
            }

            // write the field to the MDC
            MDC.put(fieldName, String.valueOf(field.getCharValue(pProperties.getStringEncoding())));
        }
    }

    // return the MDC Adapter
    // (the MDC is global, so the MDC Adapter is not really used in this class, but it is very helpful in unit
    // testing to get access to the underlying map without making costly copies)
    return MDC.getMDCAdapter();
}
项目:konker-platform    文件:KonkerMDCLogger.java   
private static MDCAdapter bwCompatibleGetMDCAdapterFromBinder() throws NoClassDefFoundError {
    return StaticMDCBinder.SINGLETON.getMDCA();
}
项目:konker-platform    文件:KonkerMDCLogger.java   
public static MDCAdapter getMDCAdapter() {
    return mdcAdapter;
}
项目:ymate-platform-v2    文件:StaticMDCBinder.java   
public MDCAdapter getMDCA() {
    return new BasicMDCAdapter();
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link BasicMDCAdapter}.
 */
public MDCAdapter getMDCA() {
    // note that this method is invoked only from within the static initializer of
    // the org.slf4j.MDC class.
    return new BasicMDCAdapter();
}
项目:dolphin-platform    文件:StaticMDCBinder.java   
public MDCAdapter getMDCA() {
    return new ThreadLocalMDCAdapter();
}
项目:EASyProducer    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link BasicMDCAdapter}.
 */
public MDCAdapter getMDCA() {
  // note that this method is invoked only from within the static initializer of 
  // the org.slf4j.MDC class.
  return new BasicMDCAdapter();
}
项目:logging    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of {@link org.slf4j.impl.StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
    return new Log4jMDCAdapter();
}
项目:logging-interceptor    文件:StaticMDCBinder.java   
public static MDCAdapter mdc() {
    return SINGLETON.getMDCA();
}
项目:logging-interceptor    文件:StaticMDCBinder.java   
public MDCAdapter getMDCA() {
    return adapter;
}
项目:bundles    文件:StaticMDCBinder.java   
public MDCAdapter getMDCA() {
    return StaticLoggerBinder.getSingleton().factory.getMDCA();
}
项目:bundles    文件:SLF4JHandler.java   
public MDCAdapter getMDCA() {
    // TODO support mdcs
    throw new UnsupportedOperationException();
}
项目:bundles    文件:EnRouteSlf4jProvider.java   
public MDCAdapter getMDCAdapter() {
    // TODO Auto-generated method stub
    return null;
}
项目:slf4j-android    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of {@link NOPMDCAdapter}.
 */
public MDCAdapter getMDCA() {
    return new NOPMDCAdapter();
}
项目:consulo    文件:StaticMDCBinder.java   
public MDCAdapter getMDCA() {
   return new BasicMDCAdapter();
}
项目:slf4j-toys    文件:StaticMDCBinder.java   
public MDCAdapter getMDCA() {
    return testMDCAdapter;
}
项目:logging-log4j2    文件:SLF4JServiceProvider.java   
@Override
public MDCAdapter getMDCAdapter() {
    return mdcAdapter;
}
项目:delern    文件:StaticMDCBinder.java   
/**
 * A stub for slf4j.
 *
 * @return NOPMDCAdapter.
 */
public MDCAdapter getMDCA() {
    return new NOPMDCAdapter();
}
项目:xltsearch    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of
 * {@link StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
   return new NOPMDCAdapter();
}
项目:Sledgehammer    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
   return new NOPMDCAdapter();
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link NOPMDCAdapter}.
 * 
 * @return instance of NOPMDCAdapter
 */
public MDCAdapter getMDCA() {
    return new NOPMDCAdapter();
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link NOPMDCAdapter}.
 */
public MDCAdapter getMDCA() {
  return new NOPMDCAdapter();
}
项目:bartleby    文件:MDC.java   
/**
 * Returns the MDCAdapter instance currently in use.
 * 
 * @return the MDcAdapter instance currently in use.
 * @since 1.4.2
 */
public static MDCAdapter getMDCAdapter() {
    return mdcAdapter;
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
    throw new UnsupportedOperationException("This code should never make it into the jar");
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
    return new Log4jMDCAdapter();
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
    return new NOPMDCAdapter();
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
    return new NOPMDCAdapter();
}
项目:bartleby    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of 
 * {@link StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
   return new LogbackMDCAdapter();
}
项目:show-java    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of
 * {@link NOPMDCAdapter}.
 */
public MDCAdapter getMDCA() {
    return new NOPMDCAdapter();
}
项目:EASyProducer    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of
 * {@link StaticMDCBinder}.
 * 
 * @return the adapter
 */
public MDCAdapter getMDCA() {
    return new NOPMDCAdapter();
}
项目:slf4j-timber    文件:StaticMDCBinder.java   
/**
 * Currently this method always returns an instance of
 * {@link NOPMDCAdapter}.
 */
public MDCAdapter getMDCA() {
  return new NOPMDCAdapter();
}