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

@evokegroup/logger

v3.0.0

Published

Console logging based on severity.

Downloads

4

Readme

@evokegroup/logger

Console logging based on severity.

Class: Logger

constructor(severity, { output, retain, onLog })

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | severity | Logger.Severity, number, string | Logger.Severity.Disabled | The maximum severity to output | | output | boolean | true | Output the messages to the console | | retain | Logger.Severity, number, string | Logger.Severity.Disabled | Message severity level to retain for later retrieval via getLogs() | | onLog | function | | A function to call whenever a message is logged regardless of severity |

Example

const Logger = require('@evokegroup/logger');
function doSomethingWithLogs(log) {
  // All logged messages are passed to this function as they are logged.
  // example: log = {
  //   severity: 2,
  //   logged: true,
  //   date: '2021-02-07T16:43:32.233Z',
  //   messages: [
  //     'Doing something'
  //   ]
  // }
}
const logger = new Logger(Logger.Severity.Info, { output: true, retain: Logger.Severity.Error, onLog: doSomethingWithLogs});
logger.info('Doing something'); // logged to console
logger.debug('Data', { firstName: 'John', lastName: 'Doe' }); // not logged to console
logger.error('An error occurred', new Error('Bad data')); // logged to console
// do other stuff
doSomthingWithTheLog(logger.getLogs()); // Only errors are retained and returned

Properties

| Property | Type | Access | Description | | -------- | ---- | ------ | ----------- | | severity | Logger.Severity | get | The Logger.Severity level |

Methods

getLogs(severity)

Get the logged messages

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | severity | Logger.Severity | The retain severity | The minimum severity level. If not given falls back on the retain severity level passed when the Logger was instantiated |

Example

const Logger = require('@evokegroup/logger');
const logger = new Logger(Logger.Severity.Error, { retain: Logger.Severity.Debug });
logger.info('Doing something'); // not logged to console
logger.debug('Data', { firstName: 'John', lastName: 'Doe' }); // not logged to console
logger.error('An error occurred', new Error('Bad data')); // logged

const allRetainedLogs = logger.getLogs();
/*
  [{
    severity: 2, logged: false, date: '2021-02-07T16:43:32.233Z', messages: ['Doing something']
  }, {
    severity: 3, logged: false, date: '2021-02-07T16:43:32.233Z', messages: ['Data', { firstName: 'John', lastName: 'Doe' }]
  }, {
    severity: 0, logged: true, date: '2021-02-07T16:43:32.233Z', messages: ['An error occurred', Error: Bad data)]
  }]
*/

const noDebug = logger.getLogs(Logger.Severity.Info);
/*
  noDebug = [{
    severity: 2, logged: false, date: '2021-02-07T16:43:32.233Z', messages: ['Doing something']
  }, {
    severity: 0, logged: true, date: '2021-02-07T16:43:32.233Z', messages: ['An error occurred', Error: Bad data)]
  }]
*/

debug(message[, ...message])

Log messages with a Logger.Severity.Debug severity level and using console.debug.

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | messages | * | | The messages to log |

Example

const Logger = require('@evokegroup/logger');

const logger_1 = new Logger(Logger.Severity.Debug);
logger_1.debug('Details', { firstName: 'John', lastName: 'Doe' });
// Expected output:
// Details
// { firstName: 'John', lastName: 'Doe' }

const logger_2 = new Logger(Logger.Severity.Error);
logger_2.debug('Details', { firstName: 'John', lastName: 'Doe' }); // This message will not be logged since the logger's maximum severity level is less than Logger.Severity.Debug

error(message[, ...message])

Log messages with a Logger.Severity.Error severity level and using console.error.

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | messages | * | | The messages to log |

info(message[, ...message])

Log messages with a Logger.Severity.Info severity level and using console.info.

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | messages | * | | The messages to log |

log(severity, message[, ...message])

Log messages

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | severity | Logger.Severity, number, string | Logger.Severity.Disabled | The message severity level | | messages | * | | The messages to log |

Example

const Logger = require('@evokegroup/logger');

const logger = new Logger(Logger.Severity.Error);
logger.log(Logger.Severity.Error, 'Error');
// Expected output:
// Error

warn(message[, ...message])

Log messages with a Logger.Severity.Warning severity level and using console.warn.

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | messages | * | | The messages to log |

Enumeration: Logger.Severity

| Property | Value | | -------- | ----- | | Disabled | -1 | | Error | 0 | | Warning | 1 | | Info | 2 | | Debug | 3 |