Skip to content

Writing error info to a log? #146

@stephen-last

Description

@stephen-last

I need to log error information to a morgan log, but I'm unsure how to do this, there don't seem to be any examples to look at.

import express from 'express'
import morgan from 'morgan'

const router = express.Router()

morgan.token('something', (req, res) => req.something)

const options = {
  stream: rfs('api.log', {
    interval: '2d',
    size: '10M',
    path: path.join(__dirname, '/../../../', 'logs')
  })
}

router.use((req, res, next) => {
  req.something = '123 abc'
  next()
})

router.use(morgan(`:date[iso] :something`, options))

router.use('/', (req, res) => {
  res.send('hello')
})

router.use('/', (err, req, res) => {
  // we have an err object here!
  res.send('error')
})

So the above will log 123 abc when res.send() is called.

How do I get info from the error object into the log..?

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions