本文整理汇总了Java中org.slf4j.spi.MDCAdapter类的典型用法代码示例。如果您正苦于以下问题:Java MDCAdapter类的具体用法?Java MDCAdapter怎么用?Java MDCAdapter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MDCAdapter类属于org.slf4j.spi包,在下文中一共展示了MDCAdapter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)));
}
示例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;
}
示例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();
}
}
示例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();
}
示例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();
}
示例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)));
}
示例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")));
}
示例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;
}
示例9: getInstance
import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public static MDCAdapter getInstance() {
return mtcMDCAdapter;
}
示例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();
}
示例11: bwCompatibleGetMDCAdapterFromBinder
import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
private static MDCAdapter bwCompatibleGetMDCAdapterFromBinder() throws NoClassDefFoundError {
return StaticMDCBinder.SINGLETON.getMDCA();
}
示例12: getMDCAdapter
import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public static MDCAdapter getMDCAdapter() {
return mdcAdapter;
}
示例13: getMDCA
import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public MDCAdapter getMDCA() {
return new BasicMDCAdapter();
}
示例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();
}
示例15: getMDCA
import org.slf4j.spi.MDCAdapter; //导入依赖的package包/类
public MDCAdapter getMDCA() {
return new ThreadLocalMDCAdapter();
}