Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java package Log4J2: MapFilters

I've got a simple question about MapFilters. Well, I know what maps are: simple key,value-pairs. Now log4j allows to filter for so called map messages by (Example from website):

  <MapFilter onMatch="ACCEPT" onMismatch="DENY" operator="or">
    <KeyValuePair key="eventId" value="Login"/>
    <KeyValuePair key="eventId" value="Logout"/>
  </MapFilter>

But where do I define these messages in the code. I tried sth like:

    MapMessage mm = new MapMessage();
    mm.put("eventId", "Login");
    logger.exit(mm);

But this isn't rather a convenient way to handle this and by the way: It doesn't work.

Do you know a more detailed introduction into this topic? The API doesn't really help me here.

Thanks to you all!

like image 794
John Rumpel Avatar asked Oct 22 '25 03:10

John Rumpel


1 Answers

I came across the above issue while configuring MapFilter. ThreadContextMapFilter works in the similar fashion but in that case we have to populate ThreadContext map and everything works fine. The reason behind the success with ThreadContextMapFilter is ThreadContext map is Context based and it is valid for all the log events in the same context, but MapFilter which works on the basis of MapMessage is not context based (MapMessage) rather it is event based, so with each logging event we got to pass the MapMessage as parameter for the MapFilter to work.

The relevant configuration and code:

<configuration status="WARN" name="abcd">
    <properties/>
    <MapFilter onMatch="ACCEPT" onMismatch="DENY">
        <KeyValuePair key="Mic" value="Mic123"/>
    </MapFilter>                 
        ...........
        ........... <<Put other relevant configurations here>>
</configuration>

Java code:

public static void main(String args[]) {
    MapMessage mapMessage = new MapMessage();

    mapMessage.put("Mic", "Mic123");
    log4j2Tester.logger.debug(mapMessage,new Exception("Okies"));
    log4j2Tester.logger.debug(mapMessage);
}

In the above code, log4j2Tester is the object of my standalone class and logger is the configured logger.

Hope this post will solve your problem. Happy Coding.

like image 162
user2398545 Avatar answered Oct 26 '25 00:10

user2398545



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!