npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@makerx/node-winston

v1.2.0

Published

A set of winston formats, console transport and logger creation functions

Downloads

647

Readme

Node Winston

A set of winston formats, console transport and logger creation functions.

Simplifies using winston logging and provides coloured YAML log output for local development.

Creating a Logger

The createLogger function combines omitFormat, omitNilFormat and optionally prettyConsoleFormat together to configure the Console transport for the returned logger.

| Option | Description | | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | consoleFormat | Either pretty (useful for local development) or json (default) | | consoleOptions | The ConsoleTransportOptions passed into the Console transport, useful for setting silent, e.g. to switch off output during test runs, per-transport level etc. | | loggerOptions | The LoggerOptions passed into the Logger, useful for the level, defaultMeta and other customisations. | | loggerOptions | The LoggerOptions passed into the Logger, useful for the level, defaultMeta and other customisations. | | omitPaths | Paths of fields you wish to omit form logging. For example, during local development you may wish to hide values from defaultMeta, e.g. user context which would be omitted in every log entry and irrelevent for local dev. | | transports | Extra Transports you wish to add to the logger. |

At MakerX we generally use config files to control logging output across local development and deployed environments:

logger.ts

import { isLocalDev } from '@makerxstudio/node-common'
import { createLogger } from '@makerxstudio/node-winston'
import config from 'config'

const logger = createLogger({
  consoleFormat: isLocalDev ? 'pretty' : 'json',
  consoleOptions: config.get('logging.consoleOptions'),
  loggerOptions: config.get('logging.loggerOptions'),
  omitPaths: config.get('logging.omitPaths'),
})

export default logger

This would translate into different runtime configurations:

// local development logger would be created something like...
const logger = createLogger({
  consoleFormat: 'pretty',
  loggerOptions: {
    defaultMeta: {
      service: 'my-application-name',
    },
    level: 'verbose',
  },
  omitPaths: ['service'], // defaultMeta.service is set in the default (all environments) config, localdev config strips this from output
})

// deployed environment logger would be created something like...
const logger = createLogger({
  consoleFormat: 'json',
  loggerOptions: {
    defaultMeta: {
      service: 'my-application-name',
    },
    level: 'info',
  },
})

// integration tests could silence noisy console output by setting process.env.SILENT_CONSOLE to 'true'
const logger = createLogger({
  consoleOptions: {
    silent: true,
  },
})

Transports

The createLogger method creates (only) a Console transport.

If you wish to add other transports, pass them in via the transports option, e.g.

const logger = createLogger({
  transports: [
    new DailyRotateFile({
      level: 'info',
      filename: 'application-%DATE%.log',
      datePattern: 'YYYY-MM-DD-HH',
      zippedArchive: true,
      maxSize: '20m',
      maxFiles: '14d',
    }),
  ],
})

Formats

createLogger applies some default behaviour, chaining omitNilFormat and omitFormat in front of the final json or coloured YAML format.

  • omitNilFormat removes null or undefined values from output
  • omitFormat removes values by path using lodash omit (see docs for path specification)
  • prettyConsoleFormat applies the colorize and timestamp formats before formatting logs as coloured YAML

If you wish to add additional formats, pass them in via the consoleFormats option.

Error serialization

The Error class's message and stack properties are not enumerable; the output of JSON.stringify(new Error('message')) is '{}'.

Winston has some special handling, so that when an error is the first or second argument, message and stack props are logged:

logger.log(new Error('cause')) // {message: 'cause', stack: ...}
logger.log('message', new Error('cause')) // {message: 'message cause', stack: ...}

However, when errors are nested in structured log data, message and stack props are lost:

catch (error) {
  logger.log('message', { info, error }) // {message: 'message', error: {}}
}

Winston logform uses safe-stable-stringify which supports a replacer, similar to JSON.stringify.

In createLogger we use serializableErrorReplacer via the JSON format options to ensure that the message and stack properties of errors are serialized to error logs:

format.json({ replacer: serializableErrorReplacer })