• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java MDCAdapter类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.slf4j.spi.MDCAdapter的典型用法代码示例。如果您正苦于以下问题:Java MDCAdapter类的具体用法?Java MDCAdapter怎么用?Java MDCAdapter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



MDCAdapter类属于org.slf4j.spi包,在下文中一共展示了MDCAdapter类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: testDefaultAuditStreamInMdc

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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)));
}
 
开发者ID:mbeiter,项目名称:eaudit4j,代码行数:26,代码来源:Slf4jProcessorTest.java


示例2: getMDCPropertyMap

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
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;
}
 
开发者ID:KonkerLabs,项目名称:konker-platform,代码行数:17,代码来源:KonkerLoggingEvent.java


示例3: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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();
  }
}
 
开发者ID:AmadeusITGroup,项目名称:HttpSessionReplacer,代码行数:15,代码来源:StaticMDCBinder.java


示例4: addAuditStreamNameToMdc

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:mbeiter,项目名称:eaudit4j,代码行数:17,代码来源:Slf4jProcessor.java


示例5: addSerializedEventToMdc

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:mbeiter,项目名称:eaudit4j,代码行数:17,代码来源:Slf4jProcessor.java


示例6: testSerializedEventInMdc

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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)));
}
 
开发者ID:mbeiter,项目名称:eaudit4j,代码行数:31,代码来源:Slf4jProcessorTest.java


示例7: testEventFieldsInMdc

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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")));
}
 
开发者ID:mbeiter,项目名称:eaudit4j,代码行数:44,代码来源:Slf4jProcessorTest.java


示例8: getMDCPropertyMap

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
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;
}
 
开发者ID:cscfa,项目名称:bartleby,代码行数:16,代码来源:LoggingEvent.java


示例9: getInstance

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public static MDCAdapter getInstance() {
    return mtcMDCAdapter;
}
 
开发者ID:ofpay,项目名称:logback-mdc-ttl,代码行数:4,代码来源:TtlMDCAdapter.java


示例10: addEventFieldsToMdc

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:mbeiter,项目名称:eaudit4j,代码行数:76,代码来源:Slf4jProcessor.java


示例11: bwCompatibleGetMDCAdapterFromBinder

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
private static MDCAdapter bwCompatibleGetMDCAdapterFromBinder() throws NoClassDefFoundError {
    return StaticMDCBinder.SINGLETON.getMDCA();
}
 
开发者ID:KonkerLabs,项目名称:konker-platform,代码行数:4,代码来源:KonkerMDCLogger.java


示例12: getMDCAdapter

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public static MDCAdapter getMDCAdapter() {
    return mdcAdapter;
}
 
开发者ID:KonkerLabs,项目名称:konker-platform,代码行数:4,代码来源:KonkerMDCLogger.java


示例13: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public MDCAdapter getMDCA() {
    return new BasicMDCAdapter();
}
 
开发者ID:suninformation,项目名称:ymate-platform-v2,代码行数:4,代码来源:StaticMDCBinder.java


示例14: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:cscfa,项目名称:bartleby,代码行数:10,代码来源:StaticMDCBinder.java


示例15: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public MDCAdapter getMDCA() {
    return new ThreadLocalMDCAdapter();
}
 
开发者ID:canoo,项目名称:dolphin-platform,代码行数:4,代码来源:StaticMDCBinder.java


示例16: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:SSEHUB,项目名称:EASyProducer,代码行数:10,代码来源:StaticMDCBinder.java


示例17: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
/**
 * Currently this method always returns an instance of {@link org.slf4j.impl.StaticMDCBinder}.
 */
public MDCAdapter getMDCA() {
	return new Log4jMDCAdapter();
}
 
开发者ID:foundation-runtime,项目名称:logging,代码行数:7,代码来源:StaticMDCBinder.java


示例18: mdc

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public static MDCAdapter mdc() {
    return SINGLETON.getMDCA();
}
 
开发者ID:t1,项目名称:logging-interceptor,代码行数:4,代码来源:StaticMDCBinder.java


示例19: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public MDCAdapter getMDCA() {
    return adapter;
}
 
开发者ID:t1,项目名称:logging-interceptor,代码行数:4,代码来源:StaticMDCBinder.java


示例20: getMDCA

import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public MDCAdapter getMDCA() {
	return StaticLoggerBinder.getSingleton().factory.getMDCA();
}
 
开发者ID:osgi,项目名称:bundles,代码行数:4,代码来源:StaticMDCBinder.java



注:本文中的org.slf4j.spi.MDCAdapter类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java HeightMode类代码示例发布时间:2022-05-21
下一篇:
Java SerializationException类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap