Skip to content

Default express logging middleware to match BYU Application Development logging standards

License

Notifications You must be signed in to change notification settings

byu-oit/express-logger

Repository files navigation

Express Logger

GitHub Release codecov

Default express logging middleware to match CES Application Development logging standards.

Install

npm i @byu-oit/express-logger

Usage

import { LoggerMiddleware } from '@byu-oit/express-logger'
const app = Express()

app.use(LoggerMiddleware())

// ... add routes to express app
CommonJS Equivalent

const { LoggerMiddleware } = require('@byu-oit/express-logger')

const app = Express()

app.use(LoggerMiddleware())

Any requests to the express server will then write logs that look like:

{
  "level":"info",
  "time":1617128842026,
  "req":{
    "id":"Root=1-abcde",
    "method":"GET",
    "url":"/persons/123456789",
    "remoteAddress":"::1"
  },"res":{
    "statusCode":200
  },
  "responseTime":168,
  "message":"request completed"
}

Embedded Logger

This middleware will attach the Pino logger to the request object, so if needed you can access the logger like:

app.use(LoggerMiddleware())

app.get('/foo', (req, res) => {
  req.log.debug('Inside the /foo route')
  res.send('hello world')
})

Options

Any pinoHttp options can be overridden, but for compliance with our logging standards, we recommend sticking to the defaults provided in this package.

Example of overwriting a default

app.use(LoggerMiddleware({
  level: 'trace' 
}))

Note: If you provide your own logger please use the @byu-oit/logger to ensure you follow the logging standards.

import DefaultLogger from '@byu-oit/logger'

const myLogger = DefaultLogger()

app.use(LoggerMiddleware({
  logger: myLogger
}))

About

Default express logging middleware to match BYU Application Development logging standards

Resources

License

Stars

Watchers

Forks

Packages