Created
March 2, 2020 01:50
-
-
Save vinicius73/ebf2b4a1bf7ada29ebdbccdf0a786840 to your computer and use it in GitHub Desktop.
Pino.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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