我将我的网络应用配置为以这种方式使用MDC:
MDC.put("cliente", "TEST");
org.slf4j.MDC.put("cliente2", "TEST2");
进入 javax.ws.rs.core.Application 启动类
import org.jboss.logging.MDC;
@ApplicationPath("/api")
public class ApplicationResource extends Application {
public ApplicationResource(){
MDC.put("cliente", "TEST");
org.slf4j.MDC.put("cliente2", "TEST2");
}
日志中的结果是:
[TEST] [TEST2] 21:18:25,048 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002225: Deploying javax.ws.rs.core.Application: class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,115 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002205: Adding provider class com.mypackage.providers.ObjectMapperContextResolver from Application class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,116 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002200: Adding class resource com.mypackage.PronetResource from Application class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,116 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002200: Adding class resource io.swagger.jaxrs.listing.ApiListingResource from Application class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,116 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002205: Adding provider class io.swagger.jaxrs.listing.SwaggerSerializers from Application class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,116 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002200: Adding class resource com.mypackage.ResourceOne from Application class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,116 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002200: Adding class resource com.mypackage.ResourceTwo from Application class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,116 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 29) RESTEASY002205: Adding provider class com.mypackage.mappers.ExceptionMapper from Application class com.mypackage.ApplicationResource$Proxy$_$$_WeldClientProxy
[TEST] [TEST2] 21:18:25,227 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 29) WFLYUT0021: Registered web context: /api/test
[] [] 21:18:25,292 INFO [org.jboss.as.server] (management-handler-thread - 2) WFLYSRV0010: Deployed "api-test-web" (runtime-name : "api-test-web.war")
[2018-03-01 09:18:25,311] Artifact api-test-web:war exploded: Artifact is deployed successfully
[2018-03-01 09:18:25,311] Artifact api-test-web:war exploded: Deploy took 13,992 milliseconds
[] [] 21:18:31,004 INFO [com.mypackage.TestApiImpl] (default task-1) Consulting... --> Request: QueryRequest(tipo_documento=CI, documento=5789124)
[] [] 21:18:31,173 INFO [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (default task-1) HHH000397: Using ASTQueryTranslatorFactory
[] [] 21:18:31,605 INFO [com.mypackage.TestApiImpl] (default task-1) End consulting --> Response: QueryResponse(datos=[])
[] [] 21:28:44,315 INFO [com.mypackage.TestApiImpl] (default task-2) Consulting... --> Request: QueryRequest(tipo_documento=CI, documento=4199210)
[] [] 21:28:44,332 INFO [com.mypackage.TestApiImpl] (default task-2) End consulting --> Response: QueryResponse(data=[])
为什么我的MDC被清除了?有必要在另一个地方申报吗?
答案 0 :(得分:1)
MDC或映射诊断上下文基于本地线程。那些其他消息是从其他线程写入的,因此不为这些消息设置MDC值。
答案 1 :(得分:0)
添加%X {cliente}来修改格式化程序