Mapped Diagnostic Context (MDC) with logger SLF4J in jboss / wildfly 10 is cleared

1

I set up my web application to use an MDC like this:

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

Inside the start class for the REST service 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");
    }

And the result of the application in the registry was:

[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 is the MDC I initialized erased? Do I need to declare it somewhere else?

    
asked by Carlos Laspina 02.03.2018 в 01:44
source

1 answer

-1

Set the MDC value to the actual implementation of the RS method - set the value at the beginning and remove the value at the end of the method.

You can also write an Interceptor so that you can easily handle all RS calls:

@Interceptors({ WsRestApiInterceptor.class })
public class SomeRsImpl ...

..
public class WsRestApiInterceptor {
    @AroundInvoke
    public Object aroundInvoke(InvocationContext invocationContext) throws Exception {
        MDC.put("key", "value");
        invocationContext.proceed()
        MDC.remove("key");
...
    
answered by 16.03.2018 в 02:10