0.0.6 • Published 9 years ago
falalog v0.0.6
Falalog
Logs stuff in a nice, consistent way!
Installation
npm install falalog
Usage
let falalog = require('falalog');
[{
name: 'date', // name of the field you'll pass in the log object
coerce: v => v.toString(), // function that will coerce the value before logging. optional.
default: new Date(2000, 0, 1), // default value. optional. defaults to a blank string.
prefix: '[', // printed before the field value in the log line. optional. defaults to [
suffix: ']' // printed after field value. defaults to ]
}, {
name: 'name',
default: 'unknown' // note, when a default is non-null, the prefix and suffix will still be printed if a field is not passed
}, {
name: 'message',
default: null // choose null for the default to avoid printing prefix/suffix in the event of an unpassed field
}].map(falalog.defineField);
[{
name: 'debug', // here we define what our "log levels" will be. each log object passed must have a level field the name of one of these
numeric: 0, // numeric value associated with log level. logs will not print unless log level exceeds env variable MIN_ERROR_LEVEL
log: console.log, // logging function to use
objectFilter: o => o, // function to filter log object before logging
stringFilter: s => s, // function to filter compiled log string before logging
styles: ['gray','dim'], // chalk styles to use on compiled string before logging
},{
name: 'warning',
numeric: 2,
log: console.log,
styles: ['yellow']
},{
name: 'error',
numeric: 3,
log: console.error,
styles: ['red']
}].map(falalog.defineLevel);
Falalog.log({
level: 'debug',
date: new Date(2015,0,1),
name: 'The Dude',
message: 'Hello, dude!',
appendStyles: ['bold'] // append the bold style to output
});
// prints "F[debug][Thu Jan 01 2015 00:00:00 GMT-0800 (PST)][The Dude][Hello, Dude!]" to console.log
// note "F[{level}]" will be prepended to all logs, where {level} is the log level
Falalog.log({
level: 'debug',
date: new Date(2015,0,1),
message: 'Hello, dude!',
onlyStyles: ['bold','yellow'] // replace default debug styles with these
});
// prints "F[debug][Thu Jan 01 2015 00:00:00 GMT-0800 (PST)][unknown][Hello, dude!]" to console.log
Falalog.log({
level: 'debug',
date: new Date(2015,0,1),
stringFilterBefore: s => s.toUpperCase() // occurs after level string filter
});
// prints "F[DEBUG][THU Jan 01 2015 00:00:00 GMT-0800 (PST)][unknown]" to console.error
/*
Other params for .log():
stringFilterAfter: s => s.toUpperCase() // occurs before level string filter
stringFilterOnly: s => s.toUpperCase() // bypass level filter with this string filter
*/
Testing
Test cases are incomplete. But:
cd falalog
mocha --harmony
``