8000 GitHub · Where software is built
[go: up one dir, main page]

Skip to content
Feature request: Context management thru AsyncLocalStorage? #4030
Open
@everett1992

Description

@everett1992

Use case

Currently if you want to context in async workflows you have to pass child loggers to the methods you call

const tasks = batch.map(async (id) => {
  doWork(id, { logger: logger.createChild() })
})
await Promise.all(tasks)

Solution/User Experience

We could use node's AsyncLocalStorage to save the 'current' logger, and functions could fetch the logger from the store

This could be combined with explicit resource management to allow writing code like

const handler = (event, context) => {
  using logger = withContext(context)
  const tasks = event.batch.map(doWork)
 await Promise.all(tasks) 
}

async function doWork(task: Task) {
  using logger = withAdditionalKeys({ id: task.id })
  await step1(task)
  await step2(task)
}

async function step1(task: Task) {
  using logger = withAdditionalKeys({ step: "1" )
  logger.info()
}

Alternative solutions

This can be built ad-hoc but would be better to include in the library.

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestThis item refers to a feature request for an existing or new utilityloggerThis item relates to the Logger UtilityresearchingThis item is currently under consideration and we'll update once we have more info

    Type

    No type

    Projects

    Status

    Ideas

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0