Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Created March 2, 2020 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinicius73/ebf2b4a1bf7ada29ebdbccdf0a786840 to your computer and use it in GitHub Desktop.
Save vinicius73/ebf2b4a1bf7ada29ebdbccdf0a786840 to your computer and use it in GitHub Desktop.
Pino.js
const pino = require('pino')
const dest = pino.destination()
const logger = pino({
name: process.env.APPLICATION_NAME || 'my-app',
level: process.env.LOG_LEVEL || 'info',
useLevelLabels: true,
base: {
}
}, dest)
setInterval(function () {
logger.flush()
}, 7000).unref()
const handler = pino.final(logger, (err, finalLogger, evt) => {
finalLogger.info(`${evt} caught`)
dest.flushSync()
if (err) {
finalLogger.error(err, 'error caused exit')
}
process.exit(err ? 1 : 0)
})
process.on('beforeExit', () => handler(null, 'beforeExit'))
process.on('exit', () => handler(null, 'exit'))
process.on('uncaughtException', (err) => handler(err, 'uncaughtException'))
process.on('SIGINT', () => handler(null, 'SIGINT'))
process.on('SIGQUIT', () => handler(null, 'SIGQUIT'))
process.on('SIGTERM', () => handler(null, 'SIGTERM'))
module.exports = { logger }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment