Skip to content

Instantly share code, notes, and snippets.

@keepitsimple
Created August 20, 2021 08:12
Show Gist options
  • Save keepitsimple/e7dec47d8f52b7bb6c5a12aeee6c5c9c to your computer and use it in GitHub Desktop.
Save keepitsimple/e7dec47d8f52b7bb6c5a12aeee6c5c9c to your computer and use it in GitHub Desktop.
Express.js pino logger configuration; healthcheck
import express from 'express'
import pino from 'pino'
import pinoHttp from 'pino-http'
import { SERVICE_UNAVAILABLE } from 'http-status'
// import { db } from './db'
const logger = pino({ level: process.env.LOG_LEVEL || 'info' })
const PORT = process.env.PORT || 3000
const app = express()
if (!module.parent) {
app.use(pinoHttp({
logger,
autoLogging: {
ignorePaths: ['/healthcheck']
}
}))
}
app.get('/healthcheck', (_, res) => {
const healthcheck = {
uptime: process.uptime(),
message: 'OK',
timestamp: Date.now()
}
try {
res.send(healthcheck)
} catch (e) {
healthcheck.message = e
res.status(SERVICE_UNAVAILABLE).send()
}
})
app.get('/', (req, res) => {
// Now we can use logger or req.log as we like
logger.info(req.url)
req.log.debug('something else')
res.send('Service is running...')
})
if (!module.parent) {
app.listen(PORT)
console.log('Express started on port 3000')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment