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

Makes final warn that is not supported instead of throwing #528

Merged
merged 4 commits into from Oct 5, 2018

Conversation

mcollina
Copy link
Member

@mcollina mcollina commented Oct 4, 2018

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

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

LGTM

@davidmarkclements
Copy link
Member

I've read up on the issue - not sure I agree that warning is better than throwing.

Throwing is the right thing because you shouldn't use pino.final at all with streams

I'd rather pino-pretty catches the error and then prints a warning itself

@mcollina
Copy link
Member Author

mcollina commented Oct 5, 2018

I've read up on the issue - not sure I agree that warning is better than throwing.

I think it is. The following currently won't work, and a user would have to add another if for process.env.NODE_ENV:

const pino = require('pino')

const logger = pino({
  prettyPrint: process.env.NODE_ENV === 'development'
})

const run = async () => {
  process.on('SIGTERM', pino.final(logger, (err, finalLogger) => {
    finalLogger.info('Graceful shutdown 1')
    finalLogger.info('Graceful shutdown 2')
    process.exit(0)
  }))

  await delay(30)
}

const delay = (secs) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve(), secs * 1000)
  })
}

run().catch(err => {
  logger.error({ err })
})

Throwing is the right thing because you shouldn't use pino.final at all with streams

I'd rather pino-pretty catches the error and then prints a warning itself

You cannot catch that error, as pino-pretty is not involved at all in the check. Another option is to replace the instanceof check with a typeof stream.flushSync === 'function', and let ducktyping do its magic. The stream we use for pinoPretty: true would have this function which will emit an error when called. Would this be better?

@davidmarkclements
Copy link
Member

yeah I think it caters to the two targeted cases -

  1. output a warning when using pretty print with pino.final
  2. throw when using any other stream with pino.final

@mcollina
Copy link
Member Author

mcollina commented Oct 5, 2018

PTAL

@@ -27,7 +27,7 @@ test('throws if the supplied handler is not a function', async ({ throws }) => {
test('throws if not supplied logger with pino.destination or pino.extreme instance', async ({ throws, doesNotThrow }) => {
throws(() => {
pino.final(pino(fs.createWriteStream('/dev/null')), () => {})
}, Error('only compatible with loggers with pino.destination and pino.extreme'))
}, Error('inal requires a stream that has a flushSync method, such as pino.destination and pino.extreme'))
Copy link
Member

Choose a reason for hiding this comment

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

typo: inal -> final

@davidmarkclements
Copy link
Member

other than the typo - LGTM

Copy link
Member

@davidmarkclements davidmarkclements left a comment

Choose a reason for hiding this comment

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

Lgtm

@mcollina mcollina merged commit 7010b5f into master Oct 5, 2018
@mcollina mcollina deleted the gentle-final branch October 5, 2018 15:39
@github-actions
Copy link

github-actions bot commented Feb 6, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pino.final does not work with pino-pretty
3 participants