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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child logger support #46

Open
jeffijoe opened this issue Feb 19, 2018 · 1 comment
Open

Child logger support #46

jeffijoe opened this issue Feb 19, 2018 · 1 comment

Comments

@jeffijoe
Copy link
Contributor

Loving Bristol, and have even written a target + formatter myself. 馃槃

It would be awesome to have support for child loggers! Imagine creating a child logger for each HTTP request, then set some globals for it (such as the calling user, request info, etc). It would share the transports, formatters and globals of it's parent.

Example API:

const logger = new Bristol({ ... })

app.use(authenticate())
app.use((ctx, next) => {
  const child = logger.createChildLogger({ /*additional config for child */ })

  child.setGlobal('user_id', () => ctx.state.user.id)
  ctx.logger = child
  return next()
})

app.use(get('/todos', (ctx) => {
  ctx.logger.info('Getting todos')
}))
@jeffijoe
Copy link
Contributor Author

jeffijoe commented Feb 22, 2018

As a temporary trick to accomplish this:

/**
 * Creates a child logger from the specified parent.
 *
 * @param parentLogger
 */
export function createChildLogger(parentLogger) {
  const child = new Bristol()
  child.setSeverities(Object.keys(parentLogger._severities))
  child._targets = [...parentLogger._targets]
  child._transforms = [...parentLogger._transforms]
  child._globals = { ...parentLogger._globals }
  const getGlobals = child._getGlobals
  child._getGlobals = function() {
    return {
      ...parentLogger._getGlobals(),
      ...getGlobals.call(this)
    }
  }
  return child
}

@TomFrost TomFrost mentioned this issue Aug 26, 2018
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