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

@brainbits/node-logger

v2.4.4

Published

Logger for node projects

Downloads

1,092

Readme

Logger

Installation

yarn add @brainbits/node-logger @brainbits/node-logger-formatter-monolog

or

npm install -S @brainbits/node-logger @brainbits/node-logger-formatter-monolog

Configuration

Adding a formatter

You have to add a formatter to your package.json. E. g. the monolog formatter:

"nodeLogger": {
    "formatter": "@brainbits/node-node-logger-node-logger-formatter-monolog"
}

Parameters

You can add more parameters to your "nodeLogger": {} section.

channel Channel of the logger (string)

Default: Name of your module

maxLevel Maximum level (string)

Default: info

timerLevel Timer level (string)

Default: debug

levels Levels (sorted array)

Default:

    'emergency',
    'alert',
    'critical',
    'error',
    'warning',
    'notice',
    'info', // max level default
    'debug',
]

outputs Outputs (object)

Default:

{
    emergency: 'stderr',
    warning: 'stdout',
}

plugins Plugins [Optional]

Example

"nodeLogger": {
    "plugins": [
        "@brainbits/node-node-logger-plugin-<name>"
    ]
}

formatter Module name of the formatter (see "Adding a formatter")

Environment variables

You can set your own ENV_VARS in your package.json with env(<env>, <fallback[optional]>)

Example

"nodeLogger": {
    "maxLevel": "env(LOGGER_LEVEL, info)"
}

This will take the value of LOGGER_LEVEL or "info" as fallback. The fallback is optional. If there is no suitable value the default is set.

Usage

Create a Logger instance

import { Logger } from '@brainbits/node-logger';

const logger = new Logger();

Arguments

import { Logger } from '@brainbits/node-logger';

const config = {
    maxLevel: 'error',
    level: [
        'error',
        'info',
        'debug',
    ],
    formatter: (event) => {
        console.log(event)
    }
};

const logger = new Logger(config);

First argument is a string to define your context.

Second argument is the entire configration object. You can override the configuration in your package.json here.

Default logger

import Logger from '@brainbits/node-logger';

const logger = new Logger();

const meta = {
    foo: 'bar';
};

logger.info('This is my message', meta);

Timer feature

There is a timer function logger.start(<message>)

import { logger } from '@brainbits/node-logger';

const meta = {
    foo: 'bar';
};

logger.start('timer');

//... somewhere else in your code ...

function loadAsyncShit() {
    return fetch('https://api/resource?query=blah')
        .then(result => {
            logger.stop('timer', meta);
            return result;
        };
};
// Could output: [2010-01-31 23:59:59] module.DEBUG: timer {"foo":"bar","timeMs":75} []

Anatomy

logger.<level>(<message>, <meta>)

Levels <level>

Pick one of these levels (default):

  • 0: emergency
  • 1: alert
  • 2: critical
  • 3: error
  • 4: warning
  • 5: notice
  • 6: info
  • 7: debug (default: no ouput [maxLevel])

Message <message>

Message can be a string, array or object (and Error object)

Meta <meta>

Must be an object. Used for additional context data.

logger.info('This is my message', { foo: 'bar' });

Caution: If message is an object and meta has the same property, meta will override this property.