Custom Logs in Moqui
Use Case:
I am looking to log specific processes into a separate log file within Moqui.
Upon exploring, I came across an approach in log4j2 called fish tagging.
My Approach:
To achieve this, I am using ThreadContext.put("discriminatorId","discriminator-101")
in my code to set a discriminator for the thread.
Code from test3.groovy -
final Logger logger = LoggerFactory.getLogger(test3.class)
ThreadContext.put("discriminatorId","discriminator-101")
logger.info("TEST Logging")
In my log4j2 configuration file, I have created a custom logger:
<Logger name="org.moqui.example.test3" level="info" additivity="false">
<AppenderRef ref="RoutingAppender"/>
</Logger>
Alongside, a routing appender has been configured:
<Routing name="RoutingAppender">
<Routes pattern="$${ctx:discriminatorId}">
<Route if="$${ctx:discriminatorId} != ''">
<RollingFile name="DmLogger" fileName="runtime/datamanager/${ctx:discriminatorId}.log" filePattern="runtime/datamanager/log-%d{yyyy-MM-dd}-%i-${ctx:discriminatorId}-%i.log">
<PatternLayout>
<pattern><div class="%p">%d |%-5level| %msg%n%rEx</div></pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Route>
</Routes>
</Routing>`
I want to avoid these logs appearing in STDOUT or any other files, hence routing to only one appender.
The above given approach addressed my issue.
Question to the Community:
Is the approach I’ve taken the correct way to address this issue? Are there any potential issues or improvements that the community suggests?