Does someone know how to add a custom to logger MDC [message #1841080] |
Mon, 03 May 2021 10:36  |
Eclipse User |
|
|
|
Here the step i have followed
1. Create a classe in app.server
@ApplicationScoped
public class MainGroupContextValueProvider implements IDiagnosticContextValueProvider {
public static final String KEY = "group.name";
@Override
public String key() {
return KEY;
}
@Override
public String value() {
final ISession session = ISession.CURRENT.get();
return session != null ? ((ServerSession)session).getUserMainGroup() : "DefaultUserGroup";
}
}
getUserMainGroup is loaded from database in `execLoadSession` and setted into setSharedContextVariable to be accessible on Client side
2. Create a custom RunContext
@Replace
public class CustomServerRunContext extends ServerRunContext {
@Override
protected <RESULT> void interceptCallableChain(final CallableChain<RESULT> callableChain) {
callableChain.add(new DiagnosticContextValueProcessor(BEANS.get(MainGroupContextValueProvider.class)));
super.interceptCallableChain(callableChain);
}
}
3. Add the custom value on config.properties.
<variable scope="context" name="mdcPattern" value="principal=%X{subject.principal.name}, group=%X{group.name}, scoutSession=%X{scout.session.id}, jobName=%X{scout.job.name}, cid=%X{scout.correlation.id}" />
When starting the application everything is correct, I have the default value of group "DefaultUserGroup" in the log. But I would expect to have another value when a user session is logged into the app. What did I forget? Is this the best way to go?
Thank you in advance.
|
|
|
Re: Does someone know how to add a custom to logger MDC [message #1841084 is a reply to message #1841080] |
Mon, 03 May 2021 12:38   |
Eclipse User |
|
|
|
Hi Seydou
The fact that you see "group=DefaultUserGroup" in your log output indicates that the MainGroupContextValueProvider is active and is correctly registered in your CustomServerRunContext. That ISession.CURRENT.get() is always null can only mean that the code is not executed in the context of a session.
I suspect that in your case, the order of the two lines in CustomServerRunContext#interceptCallableChain needs to be changed:
@Replace
public class CustomServerRunContext extends ServerRunContext {
@Override
protected <RESULT> void interceptCallableChain(final CallableChain<RESULT> callableChain) {
super.interceptCallableChain(callableChain); // <-- call this first!
callableChain.add(new DiagnosticContextValueProcessor(BEANS.get(MainGroupContextValueProvider.class)));
}
}
The session itself is also handled by one of the processors in the chain (see the default implementation). If you add your own processor first, the particular processor that spans the session context is not active yet. By adding the default Scout processors first, you should be able to access the session as expected.
Regards,
Beat
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06040 seconds