Git Product home page Git Product logo

modern-errors-winston's Introduction

modern-errors logo

Node TypeScript Codecov Mastodon Medium

modern-errors plugin for Winston.

This adds BaseError.fullFormat() and BaseError.shortFormat() which return a Winston format.

Hire me

Please reach out if you're looking for a Node.js API or CLI engineer (11 years of experience). Most recently I have been Netlify Build's and Netlify Plugins' technical lead for 2.5 years. I am available for full-time remote positions.

Features

Improved error logging with Winston:

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

Example

Adding the plugin to modern-errors.

import ModernError from 'modern-errors'

import modernErrorsWinston from 'modern-errors-winston'

export const BaseError = ModernError.subclass('BaseError', {
  plugins: [modernErrorsWinston],
})
export const InputError = BaseError.subclass('InputError')
// ...

Using the full format with Winston.

import { createLogger, transports, format } from 'winston'

const logger = createLogger({
  format: format.combine(BaseError.fullFormat(), format.json()),
  transports: [new transports.Http(httpOptions)],
})

const error = new InputError('Could not read file.', { props: { filePath } })
logger.error(error)
// Sent via HTTP:
// {
//   level: 'error',
//   name: 'InputError',
//   message: 'Could not read file.',
//   stack: `InputError: Could not read file.
//     at ...`,
//   filePath: '/...',
// }

Using the short format with Winston.

import { createLogger, transports, format } from 'winston'

const logger = createLogger({
  format: format.combine(BaseError.shortFormat(), format.cli()),
  transports: [new transports.Console()],
})

const error = new InputError('Could not read file.', { props: { filePath } })
logger.error(error)
// Printed on the console:
// error: InputError: Could not read file.
//     at ...

Install

npm install modern-errors-winston

This package requires installing Winston separately.

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.

API

modernErrorsWinston

Type: Plugin

Plugin object to pass to the plugins option of ErrorClass.subclass().

BaseError.fullFormat()

Return value: Format

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).

BaseError.shortFormat()

Return value: Format

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).

Options

Type: object

stack

Type: boolean
Default: true

Whether to log the stack trace.

level

Type: string

Override the log level.

Configuration

Options can apply to (in priority order):

export const BaseError = ModernError.subclass('BaseError', {
  plugins: [modernErrorsWinston],
  winston: options,
})
export const InputError = BaseError.subclass('InputError', { winston: options })
throw new InputError('...', { winston: options })
BaseError.fullFormat(options)

Related projects

Support

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.

Contributing

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!

modern-errors-winston's People

Contributors

dependabot[bot] avatar ehmicky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.