Log4j2 – Filter Messages using MarkerFilter

Learn to filter the logging messages or events using the Log4j2 MarkerFilter. We will learn to create MarkerFilter instances, configure them in log4j2.xml and the parent-child relationship among various filters.

1. MarkerFilter

The MarkerFilter compares the marker value associated with the to-be-logged message and configured marker value in the appender configuration. If both values match then the appender is allowed to write the message in the output target.

1.1. Creating MarkerFilter

To create a MarkerFilter, use the MarkerManager.getMarker() method. As the markers are constants, we can create a marker for the whole application and reuse it everywhere.

public static final Marker TRANSACTION = MarkerManager.getMarker("TRANSACTION");
public static final Marker PAYMENT = MarkerManager.getMarker("PAYMENT");
public static final Marker USER_MGMT = MarkerManager.getMarker("USER_MGMT");

If we want to add the parent-child relationship among the markers then we can use the Marker.addParents() method. In the following example, we are categorizing all the PAYMENT events under TRANSACTION.

Note that a match occurs when the Marker name matches either the Log Event’s Marker or one of its parents. So for this example, all PAYMENTs will be treated as TRANSACTIONs.

static {
  PAYMENT.addParents(TRANSACTION);
}

1.2. Configuring Markers

Now we can create appenders in the log4j2.xml and configure the markers in appenders as appropriate for the application requirements.

With the following configuration, we are writing all the transaction and payment logs in the transactions.log file. And we are logging all user management logs in the user-management.log.

<Appenders>
	...
	<RollingFile name="transactionLogs" fileName="${LOG_DIR}/transactions.log" 
  	filePattern="${LOG_DIR}/transactions.%d{dd-MMM}.log.gz" ignoreExceptions="false">

    <MarkerFilter marker="TRANSACTION" onMatch="ACCEPT" onMismatch="DENY"/>

    ...
  </RollingFile>

  <RollingFile name="userMgmtLogs" fileName="${LOG_DIR}/user-management.log"
    filePattern="${LOG_DIR}/user-management.%d{dd-MMM}.log.gz" ignoreExceptions="false">

    <MarkerFilter marker="USER_MGMT" onMatch="ACCEPT" onMismatch="DENY"/>

    ...
  </RollingFile>
</Appenders>

The filter can be configured for matching or mismatching events or both.

  • Both methods onMatch and onMismatch accept the values ACCEPT, DENY or NEUTRAL.
  • For onMatch, the default value is NEUTRAL.
  • For onMismatch, the default value is DENY.

1.3. Set Marker to Log Message

To set the marker to a log message, pass it as the first argument in the log methods such as info(), warn() or debug().

logger.info(TRANSACTION, "New transaction message!");

2. Demo

Let us test the markers using a simple program. We have created 3 markers as specified: TRANSACTION, PAYMENT and USER_MGMT.

We are logging an INFO message with each marker. After the program executes, the messages must go to appropriate log files.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

public class MarkerFilterDemo {

  private static final Logger logger = LogManager.getLogger(MarkerFilterDemo.class);

  public static final Marker TRANSACTION = MarkerManager.getMarker("TRANSACTION");
  public static final Marker PAYMENT = MarkerManager.getMarker("PAYMENT");
  public static final Marker USER_MGMT = MarkerManager.getMarker("USER_MGMT");

  static {
    PAYMENT.addParents(TRANSACTION);
  }

  public static void main(String[] args) {
    logger.info(TRANSACTION, "New transaction message!");
    logger.info(PAYMENT, "New payment message!");
    logger.info(USER_MGMT, "New user message!");
  }
}

Check out the log files.

2022-09-27 14:53:37 INFO  New user message!
2022-09-27 14:53:55 INFO  New transaction message!
2022-09-27 14:53:55 INFO  New payment message!

3. Conclusion

In this short log4j2 tutorial, we learned to create, configure and use the Marker Filter with an example. We learned how to match and write the filtered messages in log files.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.