本文整理汇总了Java中org.slf4j.MDC.clear方法的典型用法代码示例。如果您正苦于以下问题:Java MDC.clear方法的具体用法?Java MDC.clear怎么用?Java MDC.clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.slf4j.MDC
的用法示例。
在下文中一共展示了MDC.clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doFilter
import org.slf4j.MDC; //导入方法依赖的package包/类
/**
* Sets the slf4j <code>MDC</code> and delegates the request to the chain.
*
* @param request servlet request.
* @param response servlet response.
* @param chain filter chain.
*
* @throws IOException thrown if an IO error occurrs.
* @throws ServletException thrown if a servet error occurrs.
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
MDC.clear();
String hostname = HostnameFilter.get();
if (hostname != null) {
MDC.put("hostname", HostnameFilter.get());
}
Principal principal = ((HttpServletRequest) request).getUserPrincipal();
String user = (principal != null) ? principal.getName() : null;
if (user != null) {
MDC.put("user", user);
}
MDC.put("method", ((HttpServletRequest) request).getMethod());
MDC.put("path", ((HttpServletRequest) request).getPathInfo());
chain.doFilter(request, response);
} finally {
MDC.clear();
}
}
示例2: initialize
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public final void initialize(Bootstrap<SamlEngineConfiguration> bootstrap) {
// Enable variable substitution with environment variables
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(false)
)
);
MDC.clear();
bootstrap.addBundle(new ServiceStatusBundle());
bootstrap.addBundle(new MonitoringBundle());
bootstrap.addBundle(new LoggingBundle());
bootstrap.addBundle(new IdaJsonProcessingExceptionMapperBundle());
final InfinispanBundle infinispanBundle = new InfinispanBundle();
bootstrap.addBundle(infinispanBundle);
guiceBundle = defaultBuilder(SamlEngineConfiguration.class)
.modules(new SamlEngineModule(), new CryptoModule(), bindInfinispan(infinispanBundle.getInfinispanCacheManagerProvider()))
.build();
bootstrap.addBundle(guiceBundle);
}
示例3: call
import org.slf4j.MDC; //导入方法依赖的package包/类
public T call() throws Exception {
MDC.setContextMap(contextMap);
try {
return target.call();
}
finally {
MDC.clear();
}
}
示例4: doFilter
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
HttpServletRequest req = (HttpServletRequest) request;
String requestId = req.getHeader(REQUEST_ID_HEADER);
requestId = (StringUtils.isBlank(requestId)) ? generateRequestId() : requestId;
String keycloakToken = req.getHeader(AUTHORIZATION_HEADER);
String identityId = getIdentityId(keycloakToken);
MDC.put(REQUEST_ID_MDC_KEY, requestId);
MDC.put(IDENTITY_ID_MDC_KEY, identityId);
chain.doFilter(request, response);
} finally {
MDC.clear();
}
}
示例5: process
import org.slf4j.MDC; //导入方法依赖的package包/类
public void process(Exchange exchange) throws Exception {
String key = (String)exchange.getIn().getHeader(WebsocketConstants.CONNECTION_KEY);
MDC.clear();
MDC.put("WebsocketConstants.CONNECTION_KEY",key);
logger.info("Headers: {}",exchange.getIn().getHeaders());
}
示例6: doFilter
import org.slf4j.MDC; //导入方法依赖的package包/类
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// 目前只生成线程编号.
Trace trace = new Trace();
SystemContext.setTrace(trace.getThreadTrace());
MDC.put("Trace", SystemContext.getTrace());
System.out.println(" filer is running ");
filterChain.doFilter(request, response);
MDC.clear();
SystemContext.clean();
}
示例7: doFilter
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object principal = authentication.getPrincipal();
if(principal instanceof User) { // ugly
User user = (User) principal;
MDC.put("userId", user.getId().toString());
}
}
chain.doFilter(request, response);
} finally {
MDC.clear();
}
}
示例8: wrap
import org.slf4j.MDC; //导入方法依赖的package包/类
/**
* Simple wrapper which copies over the context (MDC and correlation) to the executing thread and
* logs uncaught exceptions
*/
public static Runnable wrap(Runnable in, Supplier<String> correlationIdProvider, Consumer<String> correlationIdSetter) {
final Optional<Map<String, String>> context = Optional.ofNullable(MDC.getCopyOfContextMap());
final Optional<String> korrelasjonsId = Optional.ofNullable(correlationIdProvider.get());
return () -> {
Optional<Map<String, String>> contextBackup = Optional.ofNullable(MDC.getCopyOfContextMap());
final Optional<String> backupKorrelasjonsId = Optional.ofNullable(correlationIdProvider.get());
context.ifPresent(MDC::setContextMap);
korrelasjonsId.ifPresent(correlationIdSetter);
try {
in.run();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw e;
} finally {
MDC.clear();
contextBackup.ifPresent(MDC::setContextMap);
backupKorrelasjonsId.ifPresent(correlationIdSetter);
}
};
}
示例9: afterTestMethod
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public void afterTestMethod(TestContext testContext) {
Method method = testContext.getTestMethod();
if (DataSetAnnotationUtils.isRun(method)) {
try {
DataProcessor dataProcessor = testContext.
getApplicationContext().getBean(DataSetAnnotationUtils.getImplName(method));
boolean success = dataProcessor.compareResult(method);
if (!success) {
logger.info("Data test result : failure");
Assert.fail();
}
} catch (EasyTestException e) {
logger.error(e.getMessage(), e);
Assert.fail(e.getMessage());
}
}
logger.info("Data test result : success");
MDC.clear();
}
示例10: run
import org.slf4j.MDC; //导入方法依赖的package包/类
public void run() {
MDC.setContextMap(contextMap);
try {
target.run();
}
finally {
MDC.clear();
}
}
示例11: decorate
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public Runnable decorate(Runnable runnable) {
Map<String, String> contextMap = MDC.getCopyOfContextMap();
return () -> {
try {
MDC.setContextMap(contextMap);
runnable.run();
} finally {
MDC.clear();
}
};
}
示例12: not_match_when_a_mdc_keys_is_different
import org.slf4j.MDC; //导入方法依赖的package包/类
@Test
public void not_match_when_a_mdc_keys_is_different() {
MDC.put("aKey", "differentValue");
MDC.put("anotherKey", "anotherValue");
LoggingEvent logEvent = aLoggingEventWith(INFO, "message");
ExpectedLoggingMessage expectedLoggingMessage = aLog()
.withMdc("aKey", equalTo("unmatchedValue"))
.withMdc("anotherKey", equalTo("anotherValue"));
boolean matches = expectedLoggingMessage.matches(logEvent);
MDC.clear();
assertThat(matches).isFalse();
}
示例13: run
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public void run() {
RequestContextAccessor.set(requestContext);
if (mdcContents != null) {
MDC.setContextMap(mdcContents);
}
runnable.run();
MDC.clear();
RequestContextAccessor.remove();
}
示例14: execute
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
MDC.put("trigger", context.getTrigger().getKey().toString());
job.execute(context);
MDC.clear();
}
示例15: doFilter
import org.slf4j.MDC; //导入方法依赖的package包/类
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
final FilterChain filterChain) throws IOException, ServletException {
try {
final HttpServletRequest request = (HttpServletRequest) servletRequest;
addContextAttribute("remoteAddress", request.getRemoteAddr());
addContextAttribute("remoteUser", request.getRemoteUser());
addContextAttribute("serverName", request.getServerName());
addContextAttribute("serverPort", String.valueOf(request.getServerPort()));
addContextAttribute("locale", request.getLocale().getDisplayName());
addContextAttribute("contentType", request.getContentType());
addContextAttribute("contextPath", request.getContextPath());
addContextAttribute("localAddress", request.getLocalAddr());
addContextAttribute("localPort", String.valueOf(request.getLocalPort()));
addContextAttribute("remotePort", String.valueOf(request.getRemotePort()));
addContextAttribute("pathInfo", request.getPathInfo());
addContextAttribute("protocol", request.getProtocol());
addContextAttribute("authType", request.getAuthType());
addContextAttribute("method", request.getMethod());
addContextAttribute("queryString", request.getQueryString());
addContextAttribute("requestUri", request.getRequestURI());
addContextAttribute("scheme", request.getScheme());
addContextAttribute("timezone", TimeZone.getDefault().getDisplayName());
final Map<String, String[]> params = request.getParameterMap();
params.keySet().forEach(k -> {
final String[] values = params.get(k);
addContextAttribute(k, Arrays.toString(values));
});
Collections.list(request.getAttributeNames())
.forEach(a -> addContextAttribute(a, request.getAttribute(a)));
final String cookieValue = this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request);
if (StringUtils.isNotBlank(cookieValue)) {
final Principal p = this.ticketRegistrySupport.getAuthenticatedPrincipalFrom(cookieValue);
if (p != null) {
addContextAttribute("principal", p.getId());
}
}
filterChain.doFilter(servletRequest, servletResponse);
} finally {
MDC.clear();
}
}