MDC filter with subsystem logging module
mantonec Jan 14, 2013 8:08 AMHi,
regarding logging module, is it possible to add a filter on MDC parameters ?
In logging class i have something like this:
Logger logger = Logger.getLogger( LoggerMessageListener.class ); MDC.put( Constants.MESSAGE_TYPE, msg.getStringProperty( Constants.MESSAGE_TYPE )); logger.log( level, msg.getText() );
Where Constants.MESSAGE_TYPE = "messageType" and msg.getStringProperty( Constants.MESSAGE_TYPE ) is filled at runtime with a text that identify my business activity (i.e. Operation1, Operation2 ...)
With log4j i was able to split all logs belongs Operation1 into Operation1.log, all logs belongs Operation2 into Operation2.log and so on.
Follows log4j configuration file:
<appender name="Operation1Appender" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="<logs>/Operation1.log" /> ... <filter class="com.mycompany.common.log.MDCMatchFilter"> <param name="KeyToMatch" value="messageType" /> <param name="ValueToMatch" value="Operation1" /> <param name="ExactMatch" value="true" /> </filter> </appender>
With Jboss AS7 instead i have, in my standalone.xml, something like this:
<periodic-rotating-file-handler name="Operation1" autoflush="true"> <level name="DEBUG"/> <filter> <match pattern="Operation1"/> <not> <match pattern="Operation10|Operation11"/> </not> </filter> <formatter> ... </formatter> <file relative-to="jboss.server.log.dir" path="Operation1.log"/> ... </periodic-rotating-file-handler>
This method of filtering is not feasible because filtering is not made on a field (messageType) but on the content of the entire log row and can cause unexpected behavior (for example with Operation1 and Operation11)
Moreover, introducing a new Operation (let me say Operation12) i'm constrained to change the configuration of other handlers (in this case Operation1)
So, what i want is to be able to define a filter on a specific MDC field, this grants that the filter is made only on the content of this unique field.
Thanks a lot,
Michele.