Minimal Node.JS logger for 12-factor apps

A twelve-factor app never concerns itself with routing or storage of its output stream. It should not attempt to write to or manage logfiles. Instead, each running process writes its event stream, unbuffered, to stdout. During local development, the developer will view this stream in the foreground of their terminal to observe the app’s behavior.

Done. This is all you need (dependencies: lodash and color):

// Usage:
// var logger = require('./logger');
// logger.debug('Debug message');
// logger.info('Info message');
// logger.warn('Warn message');
// logger.error('Error message');
// logger.addLevels({
//   silly: 'white'
// });
// logger.silly('Silly message');
//
// Further reading:
// * http://12factor.net/logs

var colors = require('colors'),
    _ = require('lodash');

var makeLogFunction = function (color, level) {
  return function (msg) {
    console.log(('[' + level + ']')[color] + ' ' + new Date().toString().grey + ' ' + msg + ' ');
  };
};

var logger = {
  addLevels: function (levels) {
    logger = _.defaults(logger, _.mapValues(levels, makeLogFunction));
  }
};

logger.addLevels({
  debug: 'grey',
  info: 'blue',
  warn: 'orange',
  error: 'red'
});

module.exports = logger;
 
5
Kudos
 
5
Kudos

Now read this

Simple animated bar charts using D3 and data-attributes

D3 is incredible powerful, but sometimes all you need is a bar chart. This snippet allows you to define one in a very declarative way. <!DOCTYPE html> <meta charset="utf-8"> <style> .chart { height: 70px; width: 300px;... Continue →