I would like to log in multitenant (Java Spring with Akka) application. Each logline should contain the tenant id, I found that the MDC looks perfect for this purpose. I use akka.event.Logging
for actors, and SLF4J (org.slf4j.Logger
) for other parts of the code. I am quite new with MDC and AspectJ.
example:
logger.info("Tenant: {} is processed", tenantId);
I would like to run the following code before every log lines creation which contains tenantId variable name (like the example):
@Aspect
public class MDCUtils {
/**
* Set TenantId on MDC context map
*
* @param tenantId
*/
@Before(???)
public static void setTenantId(String tenantId) {
Map<String, String> contextMap = new HashMap<>();
contextMap.put("tenantId", tenantId);
MDC.setContextMap(contextMap);
}
}
What should I put inside the @Before()
to catch all of log lines creation which contains tenantId variable name. If I catched them, how can I retrieve the tenantId
value? (Most of these loglines have more than one parameter, not only tenantId
)
I would like to use tenantId
for this pattern:
<pattern>%X{tenant} %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>