Created
October 14, 2011 16:00
-
-
Save mklabs/1287500 to your computer and use it in GitHub Desktop.
log helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var globalize = require('globalize'), | |
_ = require('underscore'), | |
eyes = require('eyes'), | |
conf = require('./conf')(module), | |
output = conf.get('path'), | |
formats = conf.get('format'), | |
defaults = formats.defaults; | |
var helpers = exports; | |
// Merge here each formats with defaults | |
Object.keys(formats).forEach(function(format) { | |
if(format !== 'defaults') formats[format] = _.extend({}, defaults, formats[format]); | |
}); | |
// borrowed to cliff's inspect | |
helpers.inspect = eyes.inspector({ stream: null, | |
styles: { // Styles applied to stdout | |
all: null, // Overall style applied to everything | |
label: 'underline', // Inspection labels, like 'array' in `array: [1, 2, 3]` | |
other: 'inverted', // Objects which don't have a literal representation, such as functions | |
key: 'grey', // The keys in object literals, like 'a' in `{a: 1}` | |
special: 'grey', // null, undefined... | |
number: 'blue', // 0, 1, 2... | |
bool: 'magenta', // true false | |
regexp: 'green' // /\d+/ | |
}, | |
maxLength: 50 | |
}); | |
// ### function timestamp () | |
// Returns a timestamp string for the current time, format is following [globalize date format](https://github.com/jquery/globalize#dates) | |
helpers.timestamp = function timestamp() { | |
return globalize.format(new Date(), formats.date.format); | |
}; | |
// ### function tmpl (template, data) | |
// tiny and micro-template utility, returns a string, depending on template string and data provided. | |
// | |
// tmpl(':foo <p>:bar</p>', {foo: 'bar', bar: true}) | |
// > bar <p>true</p> | |
helpers.tmpl = function tmpl(s,d){ return s.replace(/:([a-z]+)/g, function(w,m){ return d[m] || ''; } );}; | |
// ## function pad(str, padding) | |
// | |
// Basic padding function. | |
helpers.pad = function pad(str, options) { | |
var padded = options.pad - str.length, | |
elipsis = options.elipsis, | |
suffix = options.suffix; | |
return padded >= 0 ? | |
str + new Array(padded + elipsis.length + suffix.length + 1).join(' ') : | |
str.substring(0, options.pad) + elipsis + suffix; | |
}; | |
helpers.padLeft = function pad(str, options) { | |
var padded = options.pad - str.length, | |
elipsis = options.elipsis, | |
prefix = options.prefix; | |
return padded >= 0 ? | |
new Array(padded + elipsis.length + prefix.length + 1).join(' ') + str : | |
prefix + elipsis + str.substring(str.length - options.pad, str.length); | |
}; | |
helpers.format = function format(prefix, msg) { | |
return helpers.tmpl(conf.get('pattern'), { | |
prefix: helpers.formatProp(prefix, formats.prefix), | |
msg : helpers.formatProp(msg, formats.msg) | |
}); | |
}; | |
helpers.formatProp = function formatProp(prop, options) { | |
var prefix = options.prefix, | |
suffix = options.suffix, | |
align = options.align; | |
if(align !== 'left' && align !== 'right') { | |
throw new Error('Logger: Wrong align options >> ' + align + ' for ' + prop); | |
} | |
// no prefix/suffix when prop is '' | |
prop = prop ? prefix + prop + suffix : prop; | |
if(options.pad === false) return prop; | |
return helpers[align === 'left' ? 'pad' : 'padLeft'](prop, options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment