pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

nestedKey + mixin behavior change as of V7 #1441

Closed mothershipper closed 2 years ago

mothershipper commented 2 years ago

We've been using nestedKey + mixin setting to add standard keys to the root level of the logger, but put caller data under a nested key. We have a shared library with a factory func that creates a standardized logger, something like this:

const createLogger = () => {
  return pino({
     ...
     nestedKey: 'data',
     mixin: () => {
       return { "ctx": { 'caller': 'some_func' } } // ignore the "static" nature here - this is just a sample
     },
  })
}

As of the v6 release, this used to let us segment where the metadata is coming from in our logs -- if it's at the root of the log, it's coming from pino or our standard lib mixin. If it's in the 'data' field, it's from the caller.

// on v6
logger.info({ "key": 1 }, "some log")
=> { "data": { "key": 1 }, "message": "some log", "ctx": { "caller": "some_func" }}

With the v7 release, our mixin is now mixed and nested:

// on v7+
logger.info({ "key": 1 }, "some log")
=> { "data": { "key": 1, "ctx": { "caller": "some_func" }}, "message": "some log" }

Is this the desired behavior? It was done properly with regards to semver, but the release notes don't really reflect that this was a breaking change.

If this is the desired behavior, I'd be happy to contribute some docs to mixin to make it a bit more clear how they work together. But I'd also like to ask how we can get the original behavior back :)

mcollina commented 2 years ago

This was deliberate and part of https://github.com/pinojs/pino/pull/885. A doc update would be awesome!

mothershipper commented 2 years ago

Thanks @mcollina - I'll whip something up today.

Would you happen to have a suggestion for how we can get the original behavior back through pino?

For more context on the use-case: we have a few fields that we pull from async local storage and inject through the mixin function -- for example requestIDs, traceIDs, etc. We'd like to maintain those fields at the root since we have indices already set up and it gives us separation between our injected data and the caller data.

We're currently working around this by wrapping pino, but I'd be curious if there was a more native/recommended way to pull these mixin keys to the root log object and only nest the caller supplied data. I'd be happy to include the suggested approach in the docs too, in case anyone is in a similar situation :)

mcollina commented 2 years ago

Check out the formatters, there should be all you need.

windy1 commented 2 years ago

@mothershipper Were you able to find a workaround for this? Currently dealing with the same issue, except the log property in formatters accepts an object argument that starts at the nestedKey and NOT from the root of the log message, unless I'm missing something I don't see any way to modify the root of the log message using a formatter @mcollina

mothershipper commented 2 years ago

@windy1 I haven't had a chance to poke at it yet -- we fixed the behavior change in our logging layer that wrapped the pino API

windy1 commented 2 years ago

@mothershipper That's what we ended up doing as well.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.