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

@revmob/logger

v1.1.0

Published

[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

Downloads

5

Readme

Logger

Standard - JavaScript Style Guide

Build Status

A Techmob InnerSource library for logging. It encapsulates the pino library, adding the following features:

There is no support for custom transports, as this should not be a responsibility of the application.

Principles

1. Structured logging

The log format output is JSON.

2. Context-first

Instead of:

logger.info('Some message', context)

Use as:

logger.info(context, 'Some message')

3. Distributed logging ready

Example: generating a unique ID for a request — if it's not provided through the X-Request-ID header — and attaching it to the log context:

For each incomming request, the generated request-id is guaranteed to be unique by the uuid implementation. An alternative to uuid is cuid.

const uuid = require('uuid')
const express = require('express')
const logger = require('@revmob/logger').default()

const app = express()

app.use([
    (req, res, next) => {
        logger.ns.run(() => {
            const requestId = req.get('X-Request-ID') || uuid.v1()
            logger.ns.set('request-id', requestId)
            next()
        }) 
    }
])

logger.log('This is awesome!') 

Outputs:

{ ... "request-id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", "msg": "This is awesome!", ...  }

The ns property holds an instance of a continuation-local-storage namespace. See the docs for more details.

Installation

npm install --save pino pino-noir @revmob/logger

Both pino and pino-noir are peer dependencies of @revmob/logger.

Usage

General usage

For most use cases, you want to have just one instance of the logger in your application:

// logger.js
const config = {
   // ...
}

const instance = require('@revmob/logger').default(config)

// or
import logger from '@revmob/logger'
const instance = logger(config)

Where config is an object containing configuration parameters accepted by pino. It can also be ommited,

Example:

const log = require('@revmob/logger').default()

log.trace('This entry will be shown')
log.debug('All entries with level above verbose will be shown')
log.info('I am shown')
log.warn('I am bigger than info')
log.error('Behold, I am the greatest!')
log.fatal('Poor child, I am the ultimate log!')

Output:

{"level":10,"time":1512596602556,"msg":"This entry will be shown","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}
{"level":20,"time":1512596602557,"msg":"All entries with level above verbose will be shown","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}
{"level":30,"time":1512596602558,"msg":"I am shown","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}
{"level":40,"time":1512596602558,"msg":"I am bigger than info","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}
{"level":50,"time":1512596602558,"msg":"Behold, I am the greatest!","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}
{"level":60,"time":1512596602558,"msg":"Poor child, I am the ultimate log!","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}

If we specify a level in the config, all logs bellow that threshold will not be displayed:

const log = require('@revmob/logger').default({
    level: 'warn'
})

Output:

{"level":40,"time":1512596602558,"msg":"I am bigger than info","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}
{"level":50,"time":1512596602558,"msg":"Behold, I am the greatest!","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}
{"level":60,"time":1512596602558,"msg":"Poor child, I am the ultimate log!","name":"main","pid":18886,"hostname":"henrique-All-Series","v":1}

Development time

JSON logs are not exactly friendly to human eyes. To aid this, use pino CLI in development to get a better output:

const log = require('@revmob/logger').default()

log.trace('This entry will be shown')
log.debug('All entries with level above verbose will be shown')
log.info('I am shown')
log.warn('I am bigger than info')
log.error('Behold, I am the greatest!')
log.fatal('Poor child, I am the ultimate log!')

log.info({ foo: 'bar' }, 'Hey, look! I have context')
log.info(new Error('I\'m an error'), 'Hey, look! I have an error context')

Running:

node app.js | npx pino pretty

Output:

Pretty Print

NOTICE: for npm@<5.2.0, npx is not available. Change the command after the pipe to $(npm bin)/pino pretty.

Forking

We can fork the logger to change some of its properties when needed:

const log = require('@revmob/logger').default()

log.trace('This entry will be shown')
log.debug('All entries with level above verbose will be shown')
log.info('I am shown')
log.warn('I am bigger than info')
log.error('Behold, I am the greatest!')
log.fatal('Poor child, I am the ultimate log!')

log.info({ foo: 'bar' }, 'Hey, look! I have context')
log.info(new Error('I\'m an error'), 'Hey, look! I have an error context')

const child = log.child({
  name: 'child',
  level: 'error'
})

child.trace('This entry will be shown')
child.debug('All entries with level above verbose will be shown')
child.info('I am shown')
child.warn('I am bigger than info')
console.log('\n\n-----------------------------------------------\n\n')
child.error('Behold, I am the greatest!')
child.fatal('Poor child, I am the ultimate log!')

Output:

Forking

Configuration

The config object is forwarded to pino.

The default config is:

({
  level,
  extreme,
  base: {
    name: 'main',
    pid: process.pid,
    hostname: hostname()
  },
  serializers: Object.assign(
    {
      req: req => ({
        id: req.id,
        method: req.method,
        url: req.url,
        body: req.body,
        headers: req.headers,
        remoteAddress: req.connection && req.connection.remoteAddress,
        remotePort: req.connection && req.connection.remotePort
      }),
      res: pino.stdSerializers.res,
      err: pino.stdSerializers.err
    },
    noir([
      'err.options.auth.*',
      'options.auth.*',
      'auth.*',
      'secret',
      'password',
      'pass'
    ], '****')
  )
})

We are free to patch this configuration.

Contributing to Logger

Contributions are always welcome, no matter how large or small. See Contributing.