Log errors with Winston
npm install winston-error-format





Log errors with Winston.
This provides with two Winston
formats:
- Full: includes all properties
- Short: includes only the error's name, message and
stack
Unlike Winston's default
error format:
- The error name is logged
- The full format logs nested errors, including
cause
and aggregate
errors
- The full format is JSON-safe
- The short format optionally logs the stack trace
- The error instance is not modified
Using the full format.
``js
import { createLogger, format, transports } from 'winston'
import { fullFormat } from 'winston-error-format'
const logger = createLogger({
format: format.combine(fullFormat(), format.json()),
transports: [new transports.Http(httpOptions)],
})
const error = new ExampleError('Could not read file.')
error.filePath = '/...'
logger.error(error)
// Sent via HTTP:
// {
// level: 'error',
// name: 'ExampleError',
// message: 'Could not read file.',
// stack: ExampleError: Could not read file.
// at ...,`
// filePath: '/...',
// }
Using the short format.
`js
import { createLogger, format, transports } from 'winston'
import { shortFormat } from 'winston-error-format'
const logger = createLogger({
format: format.combine(shortFormat(), format.cli()),
transports: [new transports.Console()],
})
const error = new ExampleError('Could not read file.')
logger.error(error)
// Printed on the console:
// error: ExampleError: Could not read file.
// at ...
`
`bash`
npm install winston-error-format
This package requires installing Winston
separately.
`bash`
npm install winston
This package works in Node.js >=18.18.0.
This is an ES module. It must be loaded using
an import or import() statement,
not require(). If TypeScript is used, it must be configured to
output ES modules,
not CommonJS.
options: Options\Format
_Return value_:
Returns a logger
format
to combine with
format.json() or
format.prettyPrint(). This
logs all error properties, making it useful with
transports like
HTTP.
Errors should be logged using
logger.*(error).
options: Options\Format
_Return value_:
Returns a logger
format
to combine with
format.simple() or
format.cli(). This logs only the
error name, message and stack, making it useful with
transports like the
console.
Errors should be logged using
logger.*(error).
_Type_: object | ((Error) => object)
The options object can be error-specific by passing a function returning it
instead.
_Type_: boolean\true
_Default_:
Whether to log the stack trace.
_Type_: string
Override the log level.
_Type_: (Error) => Error
Maps the error before logging it.
- winston: A logger for just about
everything
- modern-errors: Handle errors in
a simple, stable, consistent way
- modern-errors-winston:
Log errors with Winston
- error-custom-class: Create
one error class
- error-class-utils: Utilities
to properly create error classes
- error-serializer: Convert
errors to/from plain objects
- normalize-exception:
Normalize exceptions/errors
- is-error-instance: Check if
a value is an Error instancemerge-error-cause
- : Merge ancause
error with its set-error-class
- : Properlyset-error-message
update an error's class
- : Properlywrap-error-message
update an error's message
- :set-error-props
Properly wrap an error's message
- : Properlyset-error-stack
update an error's properties
- : Properlyerror-cause-polyfill
update an error's stack
- :error.cause
Polyfill handle-cli-error
- : 💣 Errorsafe-json-value
handler for CLI applications 💥
- : ⛑️ JSONlog-process-errors
serialization should never fail
- : Showerror-http-response
some ❤ to Node.js process errors
- :
Create HTTP error responses
For any question, _don't hesitate_ to submit an issue on GitHub.
Everyone is welcome regardless of personal background. We enforce a
Code of conduct in order to promote a positive and
inclusive environment.
This project was made with ❤️. The simplest way to give back is by starring and
sharing it online.
If the documentation is unclear or has a typo, please click on the page's Edit`
button (pencil icon) and suggest a correction.
If you would like to help us fix a bug or add a new feature, please check our
guidelines. Pull requests are welcome!