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

winston-json-formatter

v0.10.0

Published

A Winston JSON Formatter

Downloads

1,703

Readme

npm (tag) Known Vulnerabilities dependencies Status devDependencies Status Jenkins CI

Winston JSON Formatter

A json and console formatter for winston@3.

Quickstart:

const { createLogger, transports } = require('winston');
const { configuredFormatter } = require('winston-json-formatter');

// Create Logger and configure options.

const logger = createLogger({ 
    level: 'silly',
    transports: [
        new (transports.Console)(),
    ],
});
const options = { 
    service: 'test-service', 
    logger: 'Winston-JSON-Formatter',
    version: '1.0.0', 
    typeFormat: 'json'
};

// set winston logger format and print logs.
logger.format = configuredFormatter(options);

// Availale log levels
logger.error('message');
logger.warn('message');
logger.info('message');
logger.verbose('message');
logger.debug('message');
logger.silly('message');

// Error Objects can be logged directly
err = new Error('Heroic BSoD')
logger.error(err)

Configuration

ENV

  • NODE_ENV=dev || NODE_ENV=development => options.typeFormat=console
  • Otherwise, options.typeFormat=json (default)

| Option | Default | Type | | -------------------- | ----------------------------- | ----------------------- | | options.typeFormat | Default: 'json' | Enum: 'console', 'json' | | options.hostname | Default: os.hostname() | String | | options.logger | Default: 'application-logger' | String | | options.node_env | Default: ENV NODE_ENV | String | | options.service | | String | | options.version | | String |

Test

yarn install
yarn test
yarn test:coverage

Output

Json logging


> const err = new Error('error message');
> const err.code = 'SOME_CODE';
> // The colon and space here are recommended due to this bug in winston (https://github.com/winstonjs/winston/issues/1634)
> logger.info('log message: ', err);
{
  "service": "test-service",
  "logger": "Winston-JSON-Formatter",
  "hostname": "host",
  "level": "info",
  "msg": "log message: error message",
  "meta": {
    "service": {
      "version": "1.0.0",
      "node_env": ""
    },
    "logger": {
      "time": "2018-11-28T02:52:06.700Z"
    },
    "event": {
      "code": "SOME_CODE"
    }
  },
  "err": {
    // There should be a member `"name": "Error"` here if it were not for winston bug (https://github.com/winstonjs/winston/issues/1635)
    "stack": "THE_ERROR_STACK"
  }
}

> const err = new Error('error message');
> const err.code = 'SOME_CODE';
> logger.info(err, {foo: 'bar', baz: 'qux'});
{
  "service": "test-service",
  "logger": "Winston-JSON-Formatter",
  "hostname": "host",
  "level": "info",
  "msg": "message: error message",
  "meta": {
    "service": {
      "version": "1.0.0",
      "node_env": ""
    },
    "logger": {
      "time": "2018-11-28T02:52:06.700Z"
    },
    "event": {
      "foo": "bar",
      "baz": "qux",
      "code": "SOME_CODE"
    }
  },
  "err": {
    "name": "Error"
    "stack": "THE_ERROR_STACK"
  }
}

Console logging

console log style

# Generally, the format is:

# timestamp level message
# stack_if_one_exists
# {
#   other: 'members-if-they-exist'
# }

# > logger.info("log message") ------------------------------------------------------------------ 
2019-04-12T02:10:11.091Z info log message  
# ----


# > logger.info(new Error("error message")) ------------------------------------------------------------------ 
2019-04-12T02:10:11.094Z info error message 
Error: error message
    at Object.<anonymous> (/Users/houli/docs/gitrepos/amida-tech/winston-json-formatter/experiment.js:38:13)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3 
# ----


# > logger.info("message", new Error("error message")) ------------------------------------------------------------------ 
2019-04-12T02:10:11.095Z info log messageerror message 
Error: error message
    at Object.<anonymous> (/Users/houli/docs/gitrepos/amida-tech/winston-json-formatter/experiment.js:41:28)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3 
# ----


# > logger.info({a: "a", b: "b"}) ------------------------------------------------------------------ 
2019-04-12T02:10:11.095Z info 
{
  "a": "a",
  "b": "b"
}  
# ----


# > logger.info(new Error("error message"), { a: "a", b: "b" } ) ------------------------------------------------------------------ 
2019-04-12T02:10:11.096Z info error message 
Error: error message
    at Object.<anonymous> (/Users/houli/docs/gitrepos/amida-tech/winston-json-formatter/experiment.js:47:13)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3 
{
  "a": "a",
  "b": "b"
}
# ----


# Logging an object that is not an Error that does have a member named stack will confuse the logger. This is unavoidable.
# > logger.info({ name: "some name", stack: "some stack" }) ------------------------------------------------------------------ 
2019-04-12T02:10:11.096Z info 
{
  "name": "some name",
  "stack": "some stack"
} 
some stack 
# ----


# Logging an object that is not an Error that does have a member named stack will confuse the logger. This is unavoidable.
# > logger.info("message", { name: "some name", stack: "some stack" }) ------------------------------------------------------------------ 
2019-04-12T02:10:11.096Z info message 
some stack 
# ----


# > logger.info("message", { a: "a", b: "b" }) ------------------------------------------------------------------ 
2019-04-12T02:10:11.096Z info message  
{
  "a": "a",
  "b": "b"
}
# ----


# > const err = new Error("error message"); err.code = "SOME_CODE"; logger.info("message", new Error("error message")) ------------------------------------------------------------------ 
2019-04-12T02:10:11.097Z info messageError A 
Error: Error A
    at Object.<anonymous> (/Users/houli/docs/gitrepos/amida-tech/winston-json-formatter/experiment.js:58:14)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3 
{
  "code": "SOME_CODE"
}
# ----


# > const err = new Error("error message"); err.code = "SOME_CODE"; logger.info(new Error("error message")) ------------------------------------------------------------------ 
2019-04-12T02:10:11.097Z info Error A 
Error: Error A
    at Object.<anonymous> (/Users/houli/docs/gitrepos/amida-tech/winston-json-formatter/experiment.js:58:14)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3 
{
  "code": "SOME_CODE"
}
# ----


# > const err = new Error("error message"); err.code = "SOME_CODE"; logger.info(new Error("error message"), { a: "a", b: "b" } ) ------------------------------------------------------------------ 
2019-04-12T02:10:11.097Z info Error A 
Error: Error A
    at Object.<anonymous> (/Users/houli/docs/gitrepos/amida-tech/winston-json-formatter/experiment.js:58:14)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3 
{
  "a": "a",
  "b": "b",
  "code": "SOME_CODE"
}

Miscellaneous Goodies

This repo also provides a function to check if a particular log level is greater than the currently configured log level.

Example useage:

// In some service...

// config.js
module.exports = {
    logLevel: 'info',
}

// FancyCustomError.js
import { initLogLevelGte } from 'winston-json-formatter'
const config = require('../config/index.js')
const logLevelGte = initLogLevelGte(config.logLevel)

class FancyCustomError extends Error {
  constructor(rootErrorThatCouldHaveSecretStuffInIt, message, foo, bar) {
    super()

    this.message = message

    if (logLevelGte('debug')) {
        this.rootErrorThatCouldHaveSecretStuffInIt = rootErrorThatCouldHaveSecretStuffInIt
    }
  }
}

// someFile.js
logger.info(new FancyCustomError(err, 'Error creating user'))

// When the configured log level is 'info', you would see logged only 'Error creating user', but when the configured log level is 'debug', you would see all the guts and glory.