Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Does someone know how to add a custom to logger MDC (Try adding a custom value to be put on logger MDC without success)
Does someone know how to add a custom to logger MDC [message #1841080] Mon, 03 May 2021 10:36 Go to next message
Seydou Zakou is currently offline Seydou ZakouFriend
Messages: 43
Registered: May 2020
Member
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 Go to previous messageGo to next message
Beat Schwarzentrub is currently offline Beat SchwarzentrubFriend
Messages: 201
Registered: November 2010
Senior Member
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
Re: Does someone know how to add a custom to logger MDC [message #1841090 is a reply to message #1841084] Mon, 03 May 2021 15:48 Go to previous messageGo to next message
Seydou Zakou is currently offline Seydou ZakouFriend
Messages: 43
Registered: May 2020
Member
Hi Beat,

Thank you for your quick response. I just try what you suggested and it work as expected.
Re: Does someone know how to add a custom to logger MDC [message #1842660 is a reply to message #1841090] Sun, 27 June 2021 18:53 Go to previous message
kajen  grover is currently offline kajen groverFriend
Messages: 1
Registered: June 2021
Junior Member
Push your {user_id} into MDC e.g. MDC. put("user_id", "Pawel");
Include the MDC entry in your log statements.

Previous Topic:REST with authentication
Next Topic:Created Sessions Scout 11
Goto Forum:
  


Current Time: Sat Feb 24 07:03:25 GMT 2024

Powered by FUDForum. Page generated in 0.02618 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top