Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useCustomFormat is broken #116

Open
Alexsey opened this issue Jul 21, 2022 · 0 comments
Open

useCustomFormat is broken #116

Alexsey opened this issue Jul 21, 2022 · 0 comments

Comments

@Alexsey
Copy link
Contributor

Alexsey commented Jul 21, 2022

As it has been found during the discussion in PR there is a problem with the way how Loki message is getting constructied from the winston's info object:

const line = this.useCustomFormat ? info[MESSAGE] : <some custom serialization of info>

Where useCustomFormat is true when some format has been provided to the winston-loki transport instance and false otherwise.

Notes that winston has a concept of "finalizing" and "non-finalizing" formats: if a format is finalizing - it's updating the value of info[MESSAGE]. If a format is non-finalizing - it's not updating info[MESSAGE]. info[MESSAGE] has some default value even without applying any formats.

There are two problematic scenarios:

  1. Only non-finalizing format(s) has been used on the transport level.

It means that useCustomFormat is true and info[MESSAGE] has default value. This default value doesn't include metadata like level and any changes being done by all non-finalizing formats from both logger and transport levels. And this default value will be sent to Loki.

  1. A finalizing format has been used on the logger level and no format has been used on the transport level.

It means that useCustomFormat is false but info[MESSAGE] has some prepared value. This value will be lost during the serialization of info.

Of course, the correct behavior would be to use info[MESSAGE] if a finalizing format has been applied and try to serialize the info object somehow if there were no finalizing format applied. But there is no way to understand if it has been used or not.

It seems that the only way to avoid unexpected behavior is to remove useCustomFormat, always use info[MESSAGE], and explicitly tell in the documentation that some finalizing format must be used.

In most cases, winston.format.json() is exactly what most users would expect to see - we can recommend using it unless there is an explicit need to use some other finalizing format.

After the discussion, I'm willing to make a PR with a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant