Vit Bernatik
Vit Bernatik

Reputation: 3802

log4j 2 - is it possible to use multiple markers for one log message?

I would like to do something like

logger.debug(new Marker[]{SQL_UPDATE,USER},"Updating user account");

Then I can filter either each SQL_UPDATE and/or each log regarding USER database.

Is it possible to achieve it somehow?

One solution would be to use specific log message for example I would use some specific text like @SQL_UP and then I would use text regex filter like:

<RegexFilter regex=".*@SQL_UP.*" onMatch="DENY" onMismatch="NEUTRAL"/>

But I assume it would be slower and I would need to be careful to not have message text mixed with tags by mistake...

Upvotes: 1

Views: 1938

Answers (1)

alan7678
alan7678

Reputation: 2311

As far as i know log4j2 only allows a single marker to be applied per log message. However, markers can be given parent markers by calling the addParents method.

Ex. using defined markers DEALERSHIP_A, DEALERSHIP_B and PORSCHE

The following filter will log all messages about DEALERSHIP_A's PORSCHE

Java

Marker DEALERSHIP_A, DEALERSHIP_B, PORSCHE;//must use MarkerManager.getMarker

PORSCHE.addParents(DEALERSHIP_A);//adds parents

logger.log(PORSCHE,"The 911 has maintenance issues");

PORSCE.setParents(DEALERSHIP_B);//replaces parents

logger.log(PORSCHE,"A cayenne has just been sold"); `

Configuration File

<filters> <MarkerFilter marker="DEALERSHIP_A" onMatch="Neutral" onMismatch="Deny"/> <MarkerFilter marker="PORSCHE" onMatch="Accept" onMismatch="Deny" </filters>

Console Output with filter

The 911 has maintenance issues

Upvotes: 2

Related Questions