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

logarama

v3.2.0

Published

Logging for node and the browser, logging levels, hierarchical child loggers, custom formatting, etc

Downloads

7

Readme

logarama

Build Status

Yet another simple logging library for the browser with minimum levels and hierarchical loggers.

  • All log methods map to same-named console methods.
  • An instantiate-able Logger class - allowing for multiple separate logger instances which can be individually controlled.
  • Create child loggers which inherit parent loggers' options and yet can be customized.
  • Change logging level (and children's logging levels) at runtime.
  • Override-able message formatting and output targets.
  • Supports UMD for easy integration within your project.
  • Small (<1KB).

Installation

$ npm install logarama

## How to use

var Logger = require('logarama');

var logger = new Logger();

logger.debug('this', 'is', 'an', 'array', [1,2,3]);

/*
[DEBUG]: this
[DEBUG]: is
[DEBUG]: an
[DEBUG]: array
[DEBUG]: [ 1, 2, 3 ]
*/

By default the minimum logging level is debug. You can override this:

var logger = new Logger({
  minLevel: 'error'
});

logger.trace(1);
logger.debug(2);
logger.info(3);
logger.warn(4);
logger.error(5);

/*
app[ERROR]: 5
*/

Change logging level at runtime:

var logger = new Logger({
  minLevel: 'error'
});

console.log(logger.minLevel()) /* error */

logger.debug(2);

logger.setMinLevel('debug');

console.log(logger.minLevel()) /* debug */

logger.debug(3);

/*
app[DEBUG]: 3
*/

Tags

Add a tag (prefix) to your messages:

var logger = new Logger('app');

logger.trace(1);
logger.debug(2);
logger.info(3);
logger.warn(4);
logger.error(5);

/*
app[TRACE]: 1
   (anonymous function) ...
app[DEBUG]: 2
app[INFO]: 3
app[WARN]: 4
app[ERROR]: 5
*/

The .throw() method is provided as a convient way of throwing an Error with the tag as a prefix:

var logger = new Logger('app');

// same as: throw new Error('(app) an error occurred')
logger.throw('an error occurred');

Formatting

You can override the built-in argument formatter with your own:

var logger = new Logger({
  format: function(arg) {
    return '{' + arg + '}';
  }
});

logger.debug(2, null, undefined, false, 'str', 23.2, [1,2,3]);

/*
app[DEBUG]: {2} {null} {undefined} {false} {str} {23.2} {1,2,3}
*/

Output targets

The default output target is the console. You can override this with your own:

var logMessagesToSend = [];

var logger = new Logger('Routing', {
  output: function(level, tag, msg) {
    logMessagesToSend.push([level, tag, msg])
  }
});

logger.debug(2, 3);
logger.warn('test')

console.log(logMessagesToSend);
/*
[
  ['debug', 'Routing', ['2', '3']],
  ['warn', 'Routing', 'test'],
]
*/

Child loggers

Child loggers inherit their parent's properties.

var logger = new Logger('parent', {
  minLevel: 'info',
});

var child = logger.create();
child.debug(2);
child.info(2);

/*
parent[INFO]: 2
*/

However, child tags are prefixed by their parents' tags:

var logger = new Logger('parent', {
  minLevel: 'info',
});

var child = logger.create('child');

child.info(2);

/*
parent/child[INFO]: 2
*/

Parent level changes get propagated down to children:

var logger = new Logger('parent', {
  minLevel: 'warn',
});

var child = logger.create('child', {
  minLevel: 'error',
});

child.info(2);
logger.setMinLevel('info');
child.info(3);

/*
parent/child[INFO]: 3
*/

Browser noConflict

If you're not using a module loader in browser environments then window.Logger gets set. You can use noConflict() to restore the original value of this property:

window.Logger = 2;

// load logarama.js
// window.Logger now equals the Logger class

// restore the original
var Logger = window.Logger.noConflict();

// window.Logger now equals 2

Building

To build the code and run the tests:

$ npm install
$ npm run build

Contributing

Contributions are welcome! Please see CONTRIBUTING.md.

License

MIT - see LICENSE.md