Skip to content

Instantly share code, notes, and snippets.

@jondlm
Last active August 29, 2015 14:03
Show Gist options
  • Save jondlm/dfc1bc234f410a0828b3 to your computer and use it in GitHub Desktop.
Save jondlm/dfc1bc234f410a0828b3 to your computer and use it in GitHub Desktop.
A flexible console logger for Node.js complete with log levels and color
// Logging levels:
//
// INFO
// WARN
// ERROR
// FATAL
//
// Usage:
// var log = require('./logger.js');
//
// log('some text');
// log('info', 'info fo yo friends');
// log('warn', 'word of warning to your mother');
// log('error', 'some error text as an ERROR, wont kill your program');
// log(myObject);
// log(myObject, 'hey everyone, come checkout my object as INFO!');
var util = require('util');
var moment = require('moment');
var clc = require('cli-color');
// Colors
var colors = {
INFO: clc.blue,
WARN: clc.yellow,
ERROR: clc.red,
FATAL: clc.bgRed.white.bold
};
//
// Export
// -------------------------------------
// `thing` could be a string, object, or an array
// `optionalMessage` is needed if `thing` is not a string
module.exports = function(level, thing, optionalMessage) {
var now = moment().format(); // 2014-07-12T12:14:53-07:00
// one argument, a string
// log('important message')
if (arguments.length === 1 && isString(level)) {
thing = level;
level = 'INFO';
}
// one argument, an object or array
// log(myObject)
if (arguments.length === 1 && isObjectOrArray(level)) {
thing = level;
level = 'INFO';
}
// two arguments, 1 is object or array and 2 is a string
// log({one:1}, 'check out my object')
if (isObjectOrArray(level) && isString(thing)) {
optionalMessage = thing;
thing = level;
level = 'INFO';
}
// if thing is an object or an array, util.inspect it
if (isObjectOrArray(thing)) {
thing = util.inspect(thing, { colors: true, depth: 5 });
thing = (optionalMessage || '') + '\n' + thing;
}
level = level.toUpperCase();
if (level === 'FATAL') {
console.error(
colors['INFO'](now) + ": " +
colors[level](level) + ": " +
thing
);
return process.exit(1); // die in a fire!
}
console.log(
colors['INFO'](now) + ": " +
colors[level](prepad(level, 5)) + ": " +
thing
);
};
//
// Helpers
// -------------------------------------
// Pre-pads the `string` with spaces based on `length`
function prepad(string, length) {
if (string.length < length) {
for (i=0;i<length-string.length;i++) {
string = ' ' + string;
}
return string;
} else {
return string;
}
}
function isObjectOrArray(x) {
var type = Object.prototype.toString.call(x);
return type === '[object Object]' || type === '[object Array]';
}
function isString(x) {
return typeof x === 'string';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment