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

feat: add META symbol to exports #84

Merged
merged 1 commit into from
Apr 16, 2024
Merged

feat: add META symbol to exports #84

merged 1 commit into from
Apr 16, 2024

Conversation

lukekarrys
Copy link
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
Member

@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.

@lukekarrys lukekarrys merged commit 66b7da7 into main Apr 16, 2024
28 checks passed
@lukekarrys lukekarrys deleted the lk/add-meta branch April 16, 2024 20:31
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