Giter Club home page Giter Club logo

loggerr's Introduction

Loggerr

NPM Version NPM Downloads Build Status js-happiness-style Sauce Test Status

A very simple logger with levels, thats it, no frills.

Install

$ npm install --save loggerr

Usage

var Loggerr = require('loggerr');

var log = new Loggerr();
log.error(new Error('My error message'));
/*
output: 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'
});
/*
output: Thu Apr 16 2015 22:05:27 GMT-0500 (CDT) [info] - {"msg":"Something happened","foo":"info about what happened"}
*/

Log Levels

Loggerr supports log levels. Each log level can be directed to a different output stream or disabled entirely. The supported 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:

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

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

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:

var Loggerr = require('loggerr'),
	chalk = require('chalk');

var logger = new Loggerr({
	formatter: function(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 two built in formatters:

  • default: Outputs date, level and json
  • cli: Outputs just the message and json data, colorized and formatted

To use the cli formatter just require it and pass the formatter options:

var logger = 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:

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

new Loggerr({
	streams: Loggerr.levels.map(function() {
		return logfile;
	})
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.