Skip to content

feat: add META symbol to exports#84

Merged
lukekarrys merged 1 commit intomainfrom
lk/add-meta
Apr 16, 2024
Merged

feat: add META symbol to exports#84
lukekarrys merged 1 commit intomainfrom
lk/add-meta

Conversation

@lukekarrys
Copy link
Copy Markdown
Contributor

@lukekarrys lukekarrys commented Apr 16, 2024

Two things to note:

  • The key is the string 'META'
  • The value is Symbol('proc-log.meta')

So it could be used like the following. All of the following is up to producers/consumers to implement with shared conventions. All proc-log is doing to allowing them to share a unique Symbol.

const { output, META } = require('../')

// An example of how consumers would see if a META object
// was the last argument from an event
const getMeta = (args) => {
  let meta = {}
  const last = args.at(-1)
  if (last && typeof last === 'object' && Object.hasOwn(last, META)) {
    meta = args.pop()
  }
  return [meta, ...args]
}

process.on('output', (level, ...rawArgs) => {
  const [{ force = false }, ...args] = getMeta(rawArgs)
  console.log(level, { force, args })
})

// in this implementation the value does not matter, just the key
output.standard('arg1', 'arg2', { [META]: null, force: true })
output.standard('arg1', 'arg2')
output.standard('arg1', null)
output.standard(null)
output.standard()

// Will log the following:
// standard { force: true, args: [ 'arg1', 'arg2' ] }
// standard { force: false, args: [ 'arg1', 'arg2' ] }
// standard { force: false, args: [ 'arg1', null ] }
// standard { force: false, args: [ null ] }
// standard { force: false, args: [] }

Closes: #81

@lukekarrys lukekarrys requested a review from a team as a code owner April 16, 2024 19:58
Copy link
Copy Markdown
Contributor

@wraithgar wraithgar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works if we have one proc-log in the tree.

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

Successfully merging this pull request may close these issues.

Include metadata that can be emitted along with events

2 participants