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

loggerr

v3.3.0

Published

A simple logger to console or file

Downloads

1,227

Readme

Loggerr

NPM Version NPM Downloads CI Test js-standard-style

A very simple logger.

Features:

  • Synchronous output (great for cli's, browser and tools)
  • Levels (built-in and customizable)
  • Formatting (built-in and customizable)
  • Loggerr is dependency free (formatters are not)
  • Always captures stack trace on error logs
  • Tiny filesize
  • The cli formatter 🚀

cli formatter example

Install

$ npm install --save loggerr

Usage

const log = require('loggerr')
log.error(new Error('My error message'))
// Thu Apr 16 2015 22:05:27 GMT-0500 (CDT) [error] - {"msg":"Error: My error message\n<STACK TRACE>"}

log.info('Something happened', {
  foo: 'info about what happened'
})
// Thu Apr 16 2015 22:05:27 GMT-0500 (CDT) [info] - {"msg":"Something happened","foo":"info about what happened"}

Log Levels

Each log level can be directed to a different output stream or disabled entirely. The default levels are as follows:

  • emergency
  • alert
  • critical
  • error
  • warning (default)
  • notice
  • info
  • debug

Constants are available for setting and referencing the levels and their streams. These constants are the all uppercase version of the level. Here is an example of setting the log level:

const logger = new Loggerr({
  level: Loggerr.DEBUG
})

logger.debug('Foo')
// Thu Apr 16 2015 22:05:27 GMT-0500 (CDT) [debug] - {"msg":"Foo"}

Customize Levels

You can fully customize the levels for your purposes. For example, here we implement pino compatible levels:

const log = new Loggerr({
  level: [ 'trace', 'debug', 'info', 'warn', 'error', 'fatal' ]
})

log.trace('Example trace log')

See the example of custom levels for cli output.

Log Formatting

Loggerr supports formatting via formatter functions. The default formatter outputs a timestamp, the log level and the messages formatted as json. But you can provide a custom formatter function with the formatter options. Formatter functions take three parameters: date, level, data. Say we want to output the log message with a color based on the level:

const Loggerr = require('loggerr')
const chalk = require('chalk')

const logger = new Loggerr({
  formatter: (date, level, data) => {
    var color
    switch (Loggerr.levels.indexOf(level)) {
      case Loggerr.EMERGENCY:
      case Loggerr.ALERT:
      case Loggerr.CRITICAL:
      case Loggerr.ERROR:
        color = chalk.red
        break
      case Loggerr.WARNING:
      case Loggerr.NOTICE:
        color = chalk.yellow
        break
      case Loggerr.INFO:
      case Loggerr.DEBUG:
        color = chalk.white
        break
    }
    return color(data.msg)
  }
})

There are a few built-in in formatters:

  • default: Outputs date, level and json
  • cli: Outputs the message and json data, colorized and formatted
  • bunyan: Compatible format to bunyan
  • browser: Relies on console.log, so just returns the data

For these built-in formatters can specify the string name of the formatter for built-in formatters:

const log = new Loggerr({
  formatter: 'cli'
})

To use the cli formatter you can require it and pass the formatter options:

const log = new Loggerr({
  formatter: require('loggerr/formatters/cli')
})

Output Streams

You can output each level to it's own stream. The method is simple, just pass an array of streams corresponding to each level as the streams option. The simplest way is to just map over Loggerr.levels, this is how we set the defaults:

new Loggerr({
  streams: Loggerr.levels.map(function (level, i) {
    return i > Loggerr.WARNING ? process.stdin : process.stderr
  })
})

The most useful reason to specify an output stream to to redirect logs to files. Here is an example of how to do that:

const logfile = fs.createWriteStream('./logs/stdout.log', {
  flags: 'a',
  encoding: 'utf8'
})

new Loggerr({
  streams: Loggerr.levels.map(() => logfile)
})