2

I configured my web app to use an MDC this way:

    MDC.put("cliente", "TEST");
    org.slf4j.MDC.put("cliente2", "TEST2");

Into the javax.ws.rs.core.Application startup class

import org.jboss.logging.MDC;

@ApplicationPath("/api")
public class ApplicationResource extends Application {

    public ApplicationResource(){
        MDC.put("cliente", "TEST");
        org.slf4j.MDC.put("cliente2", "TEST2");
    }

And the result in the log is:

[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=[])

Why my MDC it's cleared? Is necessary declare in another place?

Carlos Laspina
  • 2,013
  • 4
  • 27
  • 44

2 Answers2

2

MDC or Mapped Diagnostic Context is based on a thread local. Those other messages are written from other threads so the MDC values are not set for those messages.

James R. Perkins
  • 16,800
  • 44
  • 60
  • And how can I make a global MDC? Is it possible? – Carlos Laspina Mar 04 '18 at 14:11
  • if you are using ThreadPool like Taskexecutor, you can able to do it. hope this link help you. https://stackoverflow.com/questions/55899284/mdc-logger-with-completablefuture/55899645#55899645 – Mayur May 11 '19 at 17:26
0

Modify your formatter adding %X{cliente}