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

wrapp-log

v1.0.1

Published

A NodeJS logging library for Wrapp that outputs logs in accordance with Wrapp Engineering Proposal 007

Downloads

48

Readme

Wrapp-log

NodeJS JSON producing logging library. See output details below for examples.

Requirements

NodeJS v7+

Installation

npm install wrapp-log

or

yarn add wrapp-log

Usage

API

  • log.debug(msg, [fields]) - Outputs the message with with level: debug
  • log.info(msg, [fields]) - Outputs the message with with level: info
  • log.warning(msg, [fields]) - Outputs the message with with level: warning
  • log.error(msg, [fields]) - Outputs the message with with level: error
  • log.panic(msg, [fields]) - Outputs the message with with level: panic then exits the process with status code 1
  • log.withFields(fields) - Generate a new logger object that will always include the fields data in each log output.

The msg argument is expected to be string but can be something else as well.

The fields argument is required to be undefined or an object. Each value should be a primitive / JSON parsable object.

and error is passed in fields as: { error: new Error() } and will be serialized. See "Output details" below for more info.

Input

const log = require('wrapp-log')
 
log.info('Informal message')

try {
  throw new Error('Cannot handle the overload...')
} catch (e) {
  log.error('Something seriously wrong', { error: e })
}

console.log('') // Line break
const reqLog = log.withFields({ request_id: 'abc123' })

reqLog.info('New message')
reqLog.info('Merge fields', { extra: 'is ok' })
reqLog.warning('Overwriting previous set fields', { request_id: 'is allowed and possible' })

console.log('') // Line break

const specLog = reqLog.withFields({ specs: 1337 })

specLog.info('Inception!', { wo: 'ot' })
specLog.info('It\'s possible to override preset fields', { request_id: '321cba' })
specLog.warning('It\'s possible for any field...', { level: 'messed up' })

console.log('') // Killing the process by panic
log.panic('Too much to handle')

Output

{"level":"info","timestamp":"2017-01-09T01:10:01.799Z","msg":"Informal message"}
{"level":"error","timestamp":"2017-01-09T01:10:01.799Z","msg":"Something seriously wrong","error":{"name":"Error","message":"Cannot handle the overload...","stack":[{"fileName":"/Users/wrapp/test-logging.js","lineNumber":6,"functionName":null,"typeName":"Object","methodName":null,"columnNumber":9,"native":false},{"fileName":"module.js","lineNumber":571,"functionName":"Module._compile","typeName":"Module","methodName":"_compile","columnNumber":32,"native":false},{"fileName":"module.js","lineNumber":580,"functionName":"Module._extensions..js","typeName":"Object","methodName":".js","columnNumber":10,"native":false},{"fileName":"module.js","lineNumber":488,"functionName":"Module.load","typeName":"Module","methodName":"load","columnNumber":32,"native":false},{"fileName":"module.js","lineNumber":447,"functionName":"tryModuleLoad","typeName":null,"methodName":null,"columnNumber":12,"native":false},{"fileName":"module.js","lineNumber":439,"functionName":"Module._load","typeName":"Function","methodName":"_load","columnNumber":3,"native":false},{"fileName":"module.js","lineNumber":605,"functionName":"Module.runMain","typeName":"Module","methodName":"runMain","columnNumber":10,"native":false},{"fileName":"bootstrap_node.js","lineNumber":427,"functionName":"run","typeName":null,"methodName":null,"columnNumber":7,"native":false},{"fileName":"bootstrap_node.js","lineNumber":151,"functionName":"startup","typeName":null,"methodName":null,"columnNumber":9,"native":false},{"fileName":"bootstrap_node.js","lineNumber":542,"functionName":null,"typeName":null,"methodName":null,"columnNumber":3,"native":false}]}}

{"level":"info","timestamp":"2017-01-09T01:10:01.799Z","msg":"New message","request_id":"abc123"}
{"level":"info","timestamp":"2017-01-09T01:10:01.799Z","msg":"Merge fields","request_id":"abc123","extra":"is ok"}
{"level":"warning","timestamp":"2017-01-09T01:10:01.799Z","msg":"Overwriting previous set fields","request_id":"is allowed and possible"}

{"level":"info","timestamp":"2017-01-09T01:10:01.799Z","msg":"Inception!","request_id":"abc123","specs":1337,"wo":"ot"}
{"level":"info","timestamp":"2017-01-09T01:10:01.799Z","msg":"It's possible to override preset fields","request_id":"321cba","specs":1337}
{"level":"messed up","timestamp":"2017-01-09T01:10:01.799Z","msg":"It's possible for any field...","request_id":"abc123","specs":1337}

{"level":"panic","timestamp":"2017-01-09T01:10:01.799Z","msg":"Wow, this is just too much!"}

[Exited with code 1]

Output details

All output is performed to standard out. Nothing goes to standard error. Each log message is on one and the same line and a line break \n is added after each message.

The log output will look like the example below, but on one line only. Any additional properties will be added.

For the service attribute to be automatically added to each log line the environment variable SERVICE_NAME must be set. If not set, the attribute will not be serialized.

Errors are serialized to some extent (message, name and any attached properties), and the error stack trace uses npm module stack-trace

The multiline format below is to show the order of data being outputted. Each actual log message will be serialized on one and the same line.

{
  "service":"<service name>",
  "level": "<debug|info|warning|error|panic>",
  "timestamp": "<timestamp>",
  "msg": "<log text>",
  ["additional_properties": "some text| 123 | {\"deep\":\"stuff\"}"]
}

Circular references

The JSON serialisation are performed by circular-json. This makes data attributes with circular references possible to be logged.

Development

Tests are run with jest: jest or yarn test/npm run test (added coverage measurements)