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

mag

v0.9.1

Published

Mag is the streaming logger for NodeJS

Downloads

3,466

Readme

mag

Mag is the streaming logger for NodeJS

Installation

$ npm install mag --save

Use cases

Everyday use cases

If you have something to show in the terminal, use the mag as well as you used console.log before.

var logger = require('mag')();

logger.info('my great application is running!');
logger.debug('process pid is %s', process.pid);

//01:27:36.427 <INFO> my great application is running!
//01:27:36.429 <DEBUG> process pid is 29860

You get well formatted message with timestamp.

If you need to visually separate the messages from different sources, use the namespace of mag.

var mag = require('mag');

var logger = mag('my-app');
var libLogger = mag('my-lib');

logger.info('my great application is running');
libLogger.debug('my library is running too');

//22:36:24.245 [my-app] <INFO> my great application is running
//22:36:24.246 [my-lib] <DEBUG> my library is running too

For application developers

If you're thinking about formatting the output messages of your application, just require mag-hub before all mag requires.

mag-hub is passthrough stream. If you require mag-hub then all log objects will be written to this stream. You can read from this stream and make any transformation with your messages before writing them to stdout.

var hub = require('mag-hub');

// Formatters
var info = require('mag-process-info');
var format = require('mag-format-message');
var colored = require('mag-colored-output');

var myCustomFormatter = require('./my-custom-formatter.js');

hub.pipe(info())
  .pipe(format())
  .pipe(myCustomFormatter())
  .pipe(colored())
  .pipe(process.stdout);

More information are in mag-hub module.

For module developers

If you're developing a module and you have something to write to the log, just use the mag.

var mag = require('mag');
var logger = mag('my-module');

var env = process.env.NODE_ENV || 'development';

logger.info('module is running in %s environment', env);
...
myModule.on('heartbeat', function(){
  logger.debug('heartbeat of module');
});

myModule.on('error', function(){
  logger.critical(new Error('game is over'));
});

You must not to worry about the output message format. (In other loggers you have to think about the message format even if you develop a module - it is bad practice)

If users of your module want to format log of the module they can require mag-hub in their application and format logs as they want. See previous section for app developers for more information.

Warning: mag-hub is designed for use only at the application leyer, do not require it into your modules distributed via npm.

Motivation

TODO

  • You do not have to configure the output if you develop a module
  • You can do anything with the output if you develop an application

How does it work?

Specification of internal messages format can be found here: SPEC.md

Examples

You can find examples of using mag logger in this reposiory: mag-examples.

There are following branches:

  • simple - simplest replacement of console
  • module - module using mag as logger
  • app - example of application that uses the module above
  • app-formatted-output - mag-hub, log levels, and collored output (full example of mag power)

Ideology

A brief interpretation of The Twelve-Factor App - Logs section:

  • Log is the stream of time-ordered events.
  • App never concerns itself with routing or storage of its output stream.
  • App should not attempt to write to or manage logfiles.
  • App should write its event stream, unbuffered, to stdout.

License

MIT