Skip to content

Make PatternLayout's %m more versatile #1341

@Mumeii

Description

@Mumeii

Hi

Right now, in current version 6.7.0 of log4js, the %m field from PatternLayout's pattern is relying on a call to util.format(...loggingEvent.data)

Although doing so is convenient from a formatting capability point of view, I think there is a drawback :

It leave no choice at all about which part of the loggingEvent.data is passed to util.format

I can picture a use case where having a better flexibility would be great :

This use case in a way is aiming to do something much like the PatternLayouts from Slf4j / Logback, where one can associate Markers to a log message, and refer to it in the log pattern, thanks the %marker field

Doing something alike with log4js could looks like that :

logger.info( "An account got credited", {
    userName: "bob",
    amount: 100,
    currency: "EUR"
} )

In previous example :

  • the first parameter would be the message, and it would be the responsibility to the logger.info caller to format it himself (if necessary)
  • the second parameter would be a markers map

This way, at least two kind of appenders could take advantage of it :

  1. one PatternLayout, using a new %m like field, enabling to selectively log only the first parameter, and using also a %x{markers} token, that would be able to extract & format markers (the second param) from the logEvent.data array
  2. one log4js-logstash-tcp appender, that would send to the logstash entry point of an ELK stack the first parameter in a message field, along with all other metadata found in the markers object. Which in fact is what it's already doing !

So far I'm quite close from been able to tune it that way, but I have two troubles left to overcome :

  • The one reported in this issue I've open recently
  • And the trouble presented here. Whenever using %m in a PatternLayout, the whole loggingEvent.data array is passed to util.format

This last trouble is problematic in the way that it's not flexible :

  • %m log both the message AND the "markers", it's not possible to choose only one or the other
  • whenever the markers are displayed this way, there is no control on the way they are formatted : util.format turn them into a multi line JSON string. What if one would prefer to have a single line JSON string ?
  • it's not possible to blacklist certain of the markers displayed this way

Notice that it should be possible to overcome the last two troubles by using a %x{markers} field, after implementing the appropriate tokens provider function !

So in the end, the only trouble left is been able to choose precisely which part of the loggingEvent.data must be displayed.

For exemple, why not adding an optional trailing part to %m ?

Such as %m{0,2-4}, that would stand for sending to util.format only first, third, fourth and fifth parameters

I think it would make log4js much more versatile !

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions