Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Liferay clears ThreadContext's MDC parameter map
It looks like a logger from Liferay's LogFactory clears ThreadContext's parameter map after first log write. Apache's log4j2 logger does not clear the parameter map. This causes problems if/when I use MDC logging and expect my MDC parameters stay in TheadContext's parameter map for many log writes, until I decide to remove them.
My log4j2 pattern:
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t][%c{1}:%L] NDC = [%x] MDC all = [%X] MDC email = [%X{email}] %m%n
And my test code:
Logger log4jLogger = LogManager.getLogger(LogTestPortlet.class);
Log liferayLogger = LogFactoryUtil.getLog(LogTestPortlet.class);
ThreadContext.put("email", "dummy@test.my");
log4jLogger.info("log4jlogger 1");
log4jLogger.info("log4jlogger 2");
liferayLogger.info("liferayLogger 1");
liferayLogger.info("liferayLogger 2");
log4jLogger.info("log4jlogger 3");
ThreadContext.remove("email");
And Liferay log:
2024-02-21 13:51:25.116 INFO MDC all = [{email=dummy@test.my}] MDC email = [dummy@test.my] log4jlogger 1
2024-02-21 13:51:25.117 INFO MDC all = [{email=dummy@test.my}] MDC email = [dummy@test.my] log4jlogger 2
2024-02-21 13:51:25.118 INFO MDC all = [{email=dummy@test.my}] MDC email = [dummy@test.my] liferayLogger 1
2024-02-21 13:51:25.119 INFO MDC all = [{}] MDC email = [] liferayLogger 2
2024-02-21 13:51:25.120 INFO MDC all = [{}] MDC email = [] log4jlogger 3
This example simulates a scenario where Liferay writes to log during my processing (for example if a call a Liferay function in my code).
I beleive this is caused by LPS-155004 Cleaning the ThreadContext for
log4j after usage to avoid mixing up with other classes.
https://github.com/liferay/liferay-portal/commit/4a6fe4797f2d86b0fa9a65ad16562083ac26c0fe
IMO it would be good if _cleanThreadContext() would remove only those parameters from ThreadContext that were added in _populateThreadContext(). As said, currently also possible non-Liferay parameters are removed from ThreadContext.
My test environment: Docker version of Liferay 7.4.3.106-ga106.
Bug Report Created: https://liferay.atlassian.net/browse/LPD-18800
Powered by Liferay™