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

notera

v0.2.2

Published

Logging interface with support for transports

Downloads

11

Readme

Notera

Logging interface with support for transports

This package provides a common interface to enable logging to various destinations using transports. Since logging is so different in different applications and contexts, we want to have a generic logging interface, and let you control the destination format using transports.

If you expect a "batteries included" solution for your logging needs, this might not be for you.

Build Status Coverage
Status

Table of contents

  1. Usage
    1. Logging interface
    2. Options
    3. Building a transport
    4. Configuring a transport
  2. Events
  3. Transport implementations

Usage

Logging interface

Since there are many logging scenarios, and each scenario requires different data to be logged, Notera's log functions can handle input arguments in any order. The transports however, expects data to arrive in a specific format to be able to handle each entry. Because of this, Notera will classify the input arguments.

The classification will:

  • Take the first found string argument, and use it as the message (msg)
  • Take the first found Error instance argument, and use it as the error (err)
  • Take any other data, whether it's a string, Error, or object, and place it inside the meta key. If there is more than one argument falling into this category, it will become an array with the values.

Example usages:

const options = { ... }
const logger = new Notera(options)

logger.log('info', 'Hello')
// > Transports receives: { level: 'info', msg: 'Hello' }

logger.emerg(new Error(), 'Hello')
// > Transports receives: { level: 'emerg', msg: 'Hello', err: <Error> }

logger.alert('Hello', true, false)
// > Transports receives: { level: 'alert', msg: 'Hello', meta: [true, false] }

logger.crit({ some: 'data' }, true, new Error(), 'Hello')
// > Transports receives:
// {
//   level: 'crit',
//   msg: 'Hello',
//   meta: [
//     { some: 'data' },
//     true
//   ],
//   err: <Error>
// }

logger.err('Hello', 'world')
// > Transports receives: { level: 'err', msg: 'Hello', meta: 'world' }

logger.warning('Hello')
logger.notice('Hello')
logger.info('Hello')
logger.debug('Hello')

Options

interface Options {
  // Global context for this logger
  ctx?: string;
  // Logging levels to use, defaults to Linux kernel levels*:
  levels?: { [key: string]: number };
}

* Linux kernel levels:

{
  emerg: 0,
  alert: 1,
  crit: 2,
  err: 3,
  warning: 4,
  notice: 5,
  info: 6,
  debug: 7
}

Building a transport

A transport is simply a function that handles logging entries from a predictable format. A transport can either be synchronous or asynchronous. The difference is that you return a promise when you want to handle each log entry asynchronously. When doing this, Notera will catch rejected promises and emit an error event. Read more about events here.

Synchronous transport example

const logger = new Notera()

function consoleTransport ({ ctx, level, msg, err, meta }) {
  console.log(`${Date.now()} ${level}: ${msg}`, err, meta)
}

logger.addTransport(consoleTransport)

Asynchronous transport example

const fs = require('fs')
const { promisify } = require('util')
const appendFile = promisify(fs.appendFile)

const logger = new Notera()

function fileTransport ({ ctx, level, msg, err, meta }) {
  const line = `${Date.now()} ${level}: ${msg}\n`
  return appendFile('application.log', line)
}

logger.addTransport(fileTransport)

// We should listen to errors if the transport fails
logger.on('error', ({ err, entry, transport }) => {
  // Log to console if the file transport fails
  console.log(`Error in transport '${transport.name}': ${err.message}`)
  console.log(entry) // Contains the failed log entry
})

Configure a transport

Transports can be configured to act in different ways.

interface TransportOptions {
  // Name of the transport, to be able to reference it later on
  name?: string;
  // Levels that this transport should handle, or all levels if not specified
  levels?: string[];
}

Events

Notera will emit events in certain situations. It's up to you to listen and act on them. See the asynchronous transport example to see how to listen to events.

  • error: Emitted when an async transport has failed (rejected promise).

Transport implementations

Installation

  • npm install notera
  • yarn add notera