close
The Wayback Machine - https://web.archive.org/web/20210605232059/https://github.com/pinojs/pino/issues/897
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

Access to child object in logMethod #897

Open
kevinsimper opened this issue Aug 18, 2020 · 6 comments
Open

Access to child object in logMethod #897

kevinsimper opened this issue Aug 18, 2020 · 6 comments

Comments

@kevinsimper
Copy link

@kevinsimper kevinsimper commented Aug 18, 2020

I was looking to access the child object, I want to make so that it prepends the message object.

Here I log what inputArgs contains inside hooks.logMethod

after having called logger.child({"featureName":"MY FEATURE"}

{ inputArgs: [ 'special' ] }
{"level":30,"time":1597765635449,"featureName":"MY FEATURE","message":"special"}

I am trying to make a function that will prepend that name to message, so it becomes

{"level":30,"time":1597765635449,"featureName":"MY FEATURE","message":"MY FEATURE: special"}
@jsumners
Copy link
Member

@jsumners jsumners commented Aug 18, 2020

The hook has no way of obtaining that context:

pino/lib/tools.js

Lines 30 to 35 in 95856d1

function genLog (level, hook) {
if (!hook) return LOG
return function hookWrappedLog (...args) {
hook.call(this, args, LOG)
}

@mcollina
Copy link
Member

@mcollina mcollina commented Aug 18, 2020

Is not this?

@jsumners
Copy link
Member

@jsumners jsumners commented Aug 18, 2020

Is not this?

Maybe? I think bindings are rendered to a string at child creation.

@danielmahon
Copy link

@danielmahon danielmahon commented Feb 1, 2021

Is there anywhere to obtain the "parent" binding context? I have tried pretty much every hook/method and can't find a way to access the context before the new bindings are written. It would be nice to have some way to designate that child loggers are children of other loggers, for example to append the log name:

const log = logger.child({ name: 'app' });
const log2 = log.child({ name: `service` });
// via hook or formatter set log2's name to "app:service"

This would also allow easy filtering of logs in logMethod to mirror debug filtering:

function logMethod(this: any, inputArgs: any, method: pino.LogFn) {
  // Check child against debug variable
  const { name } = this.bindings();
  if (!debug.enabled(name)) return;
  return method.apply(this, inputArgs);
}

I initially assumed this could be done via formatters.bindings but it doesn't have access to the logger instance and (this might be a bug) it is only run during pino instance creation and NOT during subsequent child calls... which is how it is stated in the documentation

@danielmahon
Copy link

@danielmahon danielmahon commented Feb 1, 2021

Best way I've found so far is to wrap the logger.child method:

const defaultChildMethod = logger.child;
logger.child = function child(newBindings) {
  const { name } = this.bindings();
  const newName = newBindings.name;
  if (name && newName) {
    newBindings.name = `${name}:${newName}`;
  }
  return defaultChildMethod.apply(this, [newBindings]);
};
@mcollina
Copy link
Member

@mcollina mcollina commented Feb 2, 2021

I think we should extend formatters.bindings so that it forward this to the current logger.

bindings = formatter(bindings)

Could you send a PR?

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

Successfully merging a pull request may close this issue.

None yet
4 participants