From 3b9b01780a783bb920c23168b7bbe06f6502acf9 Mon Sep 17 00:00:00 2001 From: Couto Date: Sun, 12 Feb 2012 01:04:24 +0000 Subject: [PATCH 1/7] Add colors to stdout, keep file stream uncolored --- examples/file.js | 4 +-- lib/log.js | 87 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/examples/file.js b/examples/file.js index 03d7d88..48afaba 100644 --- a/examples/file.js +++ b/examples/file.js @@ -5,8 +5,8 @@ var Log = require('../lib/log') , fs = require('fs') - , stream = fs.createWriteStream(__dirname + '/file.log', { flags: 'a' }) - , log = new Log('debug', stream); + // , stream = fs.createWriteStream(__dirname + '/file.log', { flags: 'a' }) + , log = new Log('debug'); log.debug('a debug message'); log.info('a info message'); diff --git a/lib/log.js b/lib/log.js index 41daac2..5dc7b9e 100644 --- a/lib/log.js +++ b/lib/log.js @@ -2,8 +2,23 @@ * Log.js * Copyright(c) 2010 TJ Holowaychuk * MIT Licensed + * + * Colorful output messages by Couto */ + /** + * Colors Cheat-sheet + * white : "\033[1;37m", black : "\033[0;30m", + * red : "\033[0;31m", light_red : "\033[1;31m", + * green : "\033[0;32m", light_green : "\033[1;32m", + * yellow : "\033[0;33m", light_yellow : "\033[1;33m", + * blue : "\033[0;34m", light_blue : "\033[1;34m", + * purple : "\033[0;35m", light_purple : "\033[1;35m", + * cyan : "\033[0;36m", light_cyan : "\033[1;36m", + * gray : "\033[1;30m", light_gray : "\033[0;37m", + * reset : "\033[0m" + */ + /** * Module dependencies. */ @@ -13,9 +28,9 @@ var EventEmitter = require('events').EventEmitter; /** * Initialize a `Loggeer` with the given log `level` defaulting * to __DEBUG__ and `stream` defaulting to _stdout_. - * - * @param {Number} level - * @param {Object} stream + * + * @param {Number} level + * @param {Object} stream * @api public */ @@ -26,9 +41,24 @@ var Log = exports = module.exports = function Log(level, stream){ if (this.stream.readable) this.read(); }; +/** + * Define Colors for levels + */ +exports.colors = { + 'EMERGENCY' : "\033[0;31m", + 'ALERT' : "\033[0;33m", + 'CRITICAL' : "\033[0;31m", + 'ERROR' : "\033[1;31m", + 'WARNING' : "\033[0;33m", + 'NOTICE' : "\033[0;36m", + 'INFO' : "\033[0;35m", + 'DEBUG' : "\033[0m", + 'reset' : "\033[0m" +} + /** * System is unusable. - * + * * @type Number */ @@ -36,8 +66,8 @@ exports.EMERGENCY = 0; /** * Action must be taken immediately. - * - * @type Number + * + * @type Number */ exports.ALERT = 1; @@ -52,7 +82,7 @@ exports.CRITICAL = 2; /** * Error condition. - * + * * @type Number */ @@ -60,7 +90,7 @@ exports.ERROR = 3; /** * Warning condition. - * + * * @type Number */ @@ -68,7 +98,7 @@ exports.WARNING = 4; /** * Normal but significant condition. - * + * * @type Number */ @@ -76,7 +106,7 @@ exports.NOTICE = 5; /** * Purely informational message. - * + * * @type Number */ @@ -84,7 +114,7 @@ exports.INFO = 6; /** * Application debug messages. - * + * * @type Number */ @@ -92,16 +122,16 @@ exports.DEBUG = 7; /** * prototype. - */ + */ Log.prototype = { - + /** * Start emitting "line" events. * * @api public */ - + read: function(){ var buf = '' , self = this @@ -119,7 +149,7 @@ Log.prototype = { date: new Date(captures[1]) , level: exports[captures[2]] , levelString: captures[2] - , msg: captures[3] + , msg: captures[3] }; self.emit('line', obj); } catch (err) { @@ -133,7 +163,7 @@ Log.prototype = { self.emit('end'); }); }, - + /** * Log output message. * @@ -148,12 +178,23 @@ Log.prototype = { var msg = args[0].replace(/%s/g, function(){ return args[i++]; }); - this.stream.write( - '[' + new Date + ']' - + ' ' + levelStr - + ' ' + msg - + '\n' - ); + if (this.stream === process.stdout) { + this.stream.write( + exports.colors[levelStr] + + '[' + new Date + ']' + + ' ' + levelStr + + ' ' + msg + + exports.colors.reset + + '\n' + ); + } else { + this.stream.write( + '[' + new Date + ']' + + ' ' + levelStr + + ' ' + msg + + '\n' + ); + } } }, @@ -228,7 +269,7 @@ Log.prototype = { * * @param {String} msg * @api public - */ + */ info: function(msg){ this.log('INFO', arguments); From f663cfed35a8ea145870a7f882617f5587617112 Mon Sep 17 00:00:00 2001 From: Couto Date: Sun, 12 Feb 2012 01:05:12 +0000 Subject: [PATCH 2/7] Fix credits --- lib/log.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/log.js b/lib/log.js index 5dc7b9e..a7f93d9 100644 --- a/lib/log.js +++ b/lib/log.js @@ -3,7 +3,7 @@ * Copyright(c) 2010 TJ Holowaychuk * MIT Licensed * - * Colorful output messages by Couto + * Colorful stdout messages by Couto */ /** From 406fea4f4180775c0bd915920e4f99163eb58b6f Mon Sep 17 00:00:00 2001 From: Couto Date: Sun, 12 Feb 2012 01:19:40 +0000 Subject: [PATCH 3/7] Colorful stdout, with ability to turn colors off/on --- examples/file.js | 10 ++++++++-- lib/log.js | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/examples/file.js b/examples/file.js index 48afaba..6ffd2ad 100644 --- a/examples/file.js +++ b/examples/file.js @@ -6,13 +6,19 @@ var Log = require('../lib/log') , fs = require('fs') // , stream = fs.createWriteStream(__dirname + '/file.log', { flags: 'a' }) - , log = new Log('debug'); + , log = new Log('debug').colorful({ + DEBUG : '\033[1;30m', + NOTICE: '\033[0;32m' + }); log.debug('a debug message'); log.info('a info message'); log.notice('a notice message'); log.warning('a warning message'); + +log.colorful(false); + log.error('a error message'); log.critical('a critical message'); log.alert('a alert message'); -log.emergency('a emergency message'); \ No newline at end of file +log.emergency('a emergency message'); diff --git a/lib/log.js b/lib/log.js index a7f93d9..27dba7d 100644 --- a/lib/log.js +++ b/lib/log.js @@ -178,7 +178,7 @@ Log.prototype = { var msg = args[0].replace(/%s/g, function(){ return args[i++]; }); - if (this.stream === process.stdout) { + if (this.stream === process.stdout && this.useColors) { this.stream.write( exports.colors[levelStr] + '[' + new Date + ']' @@ -198,6 +198,20 @@ Log.prototype = { } }, + colorful : function (colors) { + var k; + if (Object.prototype.toString.call(colors) === '[object Object]') { + for (k in colors) { + if (colors.hasOwnProperty(k) && exports.colors.hasOwnProperty(k)) { + exports.colors[k] = colors[k]; + } + } + this.useColors = true; + } else if (colors === false) { this.useColors = false; } + else { this.useColors = true; } + return this; + }, + /** * Log emergency `msg`. * From 224c69fcab5e66fba2604f39627a933fcfa19a36 Mon Sep 17 00:00:00 2001 From: Couto Date: Sun, 12 Feb 2012 01:36:33 +0000 Subject: [PATCH 4/7] Update docs --- Readme.md | 28 ++++++++++++++++++++++++++++ examples/file.js | 12 +++--------- examples/stdout-color.js | 31 +++++++++++++++++++++++++++++++ lib/log.js | 7 +++++-- 4 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 examples/stdout-color.js diff --git a/Readme.md b/Readme.md index 14db231..a6af36b 100644 --- a/Readme.md +++ b/Readme.md @@ -34,6 +34,34 @@ Instead of the log level constants, you may also supply a string: log.error('oh no, failed to send mail to %s.', user.email); +Turn colors on: + + log.colorful(); + +Turn colors off: + + log.colorful(false); + +Setting up our own colors : + + log.colorful({ + 'EMERGENCY' : "\033[0;31m", + 'ALERT' : "\033[0;33m", + 'CRITICAL' : "\033[0;31m", + 'ERROR' : "\033[1;31m", + 'WARNING' : "\033[0;33m", + 'NOTICE' : "\033[0;36m", + 'INFO' : "\033[0;35m", + 'DEBUG' : "\033[0m" + }); + + You don't have to specify all levels, when defining colors, if you only want to specify certain levels, you just write those: + + log.colorful({ + 'INFO' : "\033[0;35m", + 'DEBUG' : "\033[0m" + }) + ## Reader To stream a log, simply pass a readable stream instead of a writable: diff --git a/examples/file.js b/examples/file.js index 6ffd2ad..03d7d88 100644 --- a/examples/file.js +++ b/examples/file.js @@ -5,20 +5,14 @@ var Log = require('../lib/log') , fs = require('fs') - // , stream = fs.createWriteStream(__dirname + '/file.log', { flags: 'a' }) - , log = new Log('debug').colorful({ - DEBUG : '\033[1;30m', - NOTICE: '\033[0;32m' - }); + , stream = fs.createWriteStream(__dirname + '/file.log', { flags: 'a' }) + , log = new Log('debug', stream); log.debug('a debug message'); log.info('a info message'); log.notice('a notice message'); log.warning('a warning message'); - -log.colorful(false); - log.error('a error message'); log.critical('a critical message'); log.alert('a alert message'); -log.emergency('a emergency message'); +log.emergency('a emergency message'); \ No newline at end of file diff --git a/examples/stdout-color.js b/examples/stdout-color.js new file mode 100644 index 0000000..1dec3ab --- /dev/null +++ b/examples/stdout-color.js @@ -0,0 +1,31 @@ + +/** + * Module dependencies. + */ + +var Log = require('../lib/log') + , log = new Log('notice').colorful(); + +log.debug('a debug message'); +log.info('a info message'); +log.notice('a notice message'); +log.warning('a warning message'); + +/** + * Setup our own colors + */ +log.colorful({ + ERROR : "\033[0;37m", + CRITICAL : "\033[1;36m" +}); + +log.error('a error message'); +log.critical('a critical message'); +log.alert('a alert message'); + +/** + * Turn colors off + */ +log.colorful(false); +log.emergency('a emergency %s', 'message'); +log.emergency('a really %s emergency %s', 'bad', 'message'); diff --git a/lib/log.js b/lib/log.js index 27dba7d..055535b 100644 --- a/lib/log.js +++ b/lib/log.js @@ -178,7 +178,10 @@ Log.prototype = { var msg = args[0].replace(/%s/g, function(){ return args[i++]; }); - if (this.stream === process.stdout && this.useColors) { + if (this.stream === process.stdout && + this.useColors && + exports.colors[levelStr]) { + this.stream.write( exports.colors[levelStr] + '[' + new Date + ']' @@ -202,7 +205,7 @@ Log.prototype = { var k; if (Object.prototype.toString.call(colors) === '[object Object]') { for (k in colors) { - if (colors.hasOwnProperty(k) && exports.colors.hasOwnProperty(k)) { + if (colors.hasOwnProperty(k)) { exports.colors[k] = colors[k]; } } From 38f0ae56b60b8ba1a37e0fc33bd35381fe3f914a Mon Sep 17 00:00:00 2001 From: Couto Date: Tue, 20 Mar 2012 23:05:25 +0000 Subject: [PATCH 5/7] Add trace level for compatibility purposes --- lib/log.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/log.js b/lib/log.js index 055535b..4215d69 100644 --- a/lib/log.js +++ b/lib/log.js @@ -53,6 +53,7 @@ exports.colors = { 'NOTICE' : "\033[0;36m", 'INFO' : "\033[0;35m", 'DEBUG' : "\033[0m", + 'TRACE' : "\033[1;30m", 'reset' : "\033[0m" } @@ -120,6 +121,15 @@ exports.INFO = 6; exports.DEBUG = 7; +/** + * Application debug messages. + * + * @type Number + */ + +exports.TRACE = 8; + + /** * prototype. */ @@ -301,7 +311,19 @@ Log.prototype = { debug: function(msg){ this.log('DEBUG', arguments); + }, + + /** + * Log trace `msg`. + * + * @param {String} msg + * @api public + */ + + trace: function(msg){ + this.log('TRACE', arguments); } + }; /** From 2c8b2c9ab61392b3d506645033c07ba93909a597 Mon Sep 17 00:00:00 2001 From: neekey Date: Fri, 3 Aug 2012 21:15:48 +0800 Subject: [PATCH 6/7] add method `customLevels` for customizing levels and `setLevel` to set current level --- examples/stdout-color.js | 43 +++- lib/log.js | 485 ++++++++++++++++++--------------------- 2 files changed, 257 insertions(+), 271 deletions(-) diff --git a/examples/stdout-color.js b/examples/stdout-color.js index 1dec3ab..c5ce7f0 100644 --- a/examples/stdout-color.js +++ b/examples/stdout-color.js @@ -4,12 +4,20 @@ */ var Log = require('../lib/log') - , log = new Log('notice').colorful(); + , log = new Log('trace').colorful(); +/** + * Default log. + */ +log.trace( 'Below is default logs.'); log.debug('a debug message'); log.info('a info message'); log.notice('a notice message'); log.warning('a warning message'); +log.error('a error message'); +log.critical('a critical message'); +log.alert('a alert message'); +log.emergency('a emergency %s', 'message'); /** * Setup our own colors @@ -19,13 +27,40 @@ log.colorful({ CRITICAL : "\033[1;36m" }); +log.trace( 'Below is color-customed logs.'); +log.debug('a debug message'); +log.info('a info message'); +log.notice('a notice message'); +log.warning('a warning message'); log.error('a error message'); log.critical('a critical message'); log.alert('a alert message'); +log.emergency('a emergency %s', 'message'); /** - * Turn colors off + * Turn off color. */ -log.colorful(false); +log.colorful( false ); +log.trace( 'Below is color-offed logs.'); +log.debug('a debug message'); +log.info('a info message'); +log.notice('a notice message'); +log.warning('a warning message'); +log.error('a error message'); +log.critical('a critical message'); +log.alert('a alert message'); log.emergency('a emergency %s', 'message'); -log.emergency('a really %s emergency %s', 'bad', 'message'); + +/** + * Customize levels and colors + */ +log.customLevels({ + COMMAND: "\x1b[0;35m", + DATA: "\x1b[0;33m", + RESULT: "\x1b[0;36m" +}).colorful( true).setLevel( 'result' ); + +log.command( 'a command message' ); +log.data( 'a data message' ); +log.result( 'a result message' ); + diff --git a/lib/log.js b/lib/log.js index 4215d69..9871016 100644 --- a/lib/log.js +++ b/lib/log.js @@ -4,20 +4,22 @@ * MIT Licensed * * Colorful stdout messages by Couto + * + * CustomLevel by Neekey */ - /** - * Colors Cheat-sheet - * white : "\033[1;37m", black : "\033[0;30m", - * red : "\033[0;31m", light_red : "\033[1;31m", - * green : "\033[0;32m", light_green : "\033[1;32m", - * yellow : "\033[0;33m", light_yellow : "\033[1;33m", - * blue : "\033[0;34m", light_blue : "\033[1;34m", - * purple : "\033[0;35m", light_purple : "\033[1;35m", - * cyan : "\033[0;36m", light_cyan : "\033[1;36m", - * gray : "\033[1;30m", light_gray : "\033[0;37m", - * reset : "\033[0m" - */ +/** + * Colors Cheat-sheet + * white : "\033[1;37m", black : "\033[0;30m", + * red : "\033[0;31m", light_red : "\033[1;31m", + * green : "\033[0;32m", light_green : "\033[1;32m", + * yellow : "\033[0;33m", light_yellow : "\033[1;33m", + * blue : "\033[0;34m", light_blue : "\033[1;34m", + * purple : "\033[0;35m", light_purple : "\033[1;35m", + * cyan : "\033[0;36m", light_cyan : "\033[1;36m", + * gray : "\033[1;30m", light_gray : "\033[0;37m", + * reset : "\033[0m" + */ /** * Module dependencies. @@ -34,298 +36,247 @@ var EventEmitter = require('events').EventEmitter; * @api public */ -var Log = exports = module.exports = function Log(level, stream){ - if ('string' == typeof level) level = exports[level.toUpperCase()]; - this.level = level || exports.DEBUG; - this.stream = stream || process.stdout; - if (this.stream.readable) this.read(); +var Log = exports = module.exports = function Log(level, stream, custom) { + + // Add custom levels. + if( custom ) this.customLevels( custom ); + this.setLevel( level ); + this.stream = stream || process.stdout; + if (this.stream.readable) this.read(); }; /** - * Define Colors for levels + * Define Levels for levels. */ exports.colors = { - 'EMERGENCY' : "\033[0;31m", - 'ALERT' : "\033[0;33m", - 'CRITICAL' : "\033[0;31m", - 'ERROR' : "\033[1;31m", - 'WARNING' : "\033[0;33m", - 'NOTICE' : "\033[0;36m", - 'INFO' : "\033[0;35m", - 'DEBUG' : "\033[0m", - 'TRACE' : "\033[1;30m", - 'reset' : "\033[0m" + /** + * System is unusable. + */ + 'EMERGENCY':"\033[0;31m", + /** + * Action must be taken immediately. + */ + 'ALERT':"\033[0;33m", + /** + * Critical condition. + */ + 'CRITICAL':"\033[0;31m", + /** + * Error condition. + */ + 'ERROR':"\033[1;31m", + /** + * Warning condition. + */ + 'WARNING':"\033[0;33m", + /** + * Normal but significant condition. + */ + 'NOTICE':"\033[0;36m", + /** + * Purely informational message. + */ + 'INFO':"\033[0;35m", + /** + * Application debug messages. + */ + 'DEBUG':"\033[0m", + /** + * Application debug messages. + */ + 'TRACE':"\033[1;30m", + /** + * Reset log color. + */ + 'reset':"\033[0m" } /** - * System is unusable. + * The latest enum value for level. * - * @type Number + * @type {Number} */ - -exports.EMERGENCY = 0; +var enumCount = 0; /** - * Action must be taken immediately. + * Add an new log level to exports, and assign an new enum value to it. * - * @type Number + * @param levelName */ +var addLevelEnumerable = function (levelName) { -exports.ALERT = 1; + if (!( levelName in exports )) { + exports[ levelName ] = ++enumCount; + } -/** - * Critical condition. - * - * @type Number - */ + var lowCaseLevelName = levelName.toLowerCase(); -exports.CRITICAL = 2; + if(!( lowCaseLevelName in Log.prototype )) { -/** - * Error condition. - * - * @type Number - */ - -exports.ERROR = 3; + Log.prototype[ lowCaseLevelName ] = function(msg){ + this.log(levelName, arguments); + } + } +}; /** - * Warning condition. - * - * @type Number + * prototype. */ -exports.WARNING = 4; - -/** - * Normal but significant condition. - * - * @type Number - */ +Log.prototype = { -exports.NOTICE = 5; + /** + * Start emitting "line" events. + * + * @api public + */ + + read:function () { + var buf = '' + , self = this + , stream = this.stream; + + stream.setEncoding('ascii'); + stream.on('data', function (chunk) { + buf += chunk; + if ('\n' != buf[buf.length - 1]) return; + buf.split('\n').map(function (line) { + if (!line.length) return; + try { + var captures = line.match(/^\[([^\]]+)\] (\w+) (.*)/); + var obj = { + date:new Date(captures[1]), level:exports[captures[2]], levelString:captures[2], msg:captures[3] + }; + self.emit('line', obj); + } catch (err) { + // Ignore + } + }); + buf = ''; + }); + + stream.on('end', function () { + self.emit('end'); + }); + }, + + /** + * Log output message. + * + * @param {String} levelStr + * @param {Array} args + * @api private + */ + + log:function (levelStr, args) { + if (exports[levelStr] <= this.level) { + var i = 1; + var msg = args[0].replace(/%s/g, function () { + return args[i++]; + }); + if (this.stream === process.stdout && + this.useColors && + exports.colors[levelStr]) { + + this.stream.write( + exports.colors[levelStr] + + '[' + new Date + ']' + + ' ' + levelStr + + ' ' + msg + + exports.colors.reset + + '\n' + ); + } else { + this.stream.write( + '[' + new Date + ']' + + ' ' + levelStr + + ' ' + msg + + '\n' + ); + } + } + }, + + /** + * Customize colors for stout. + * + * @param {Object|Boolean} colors + * @return {*} + * @example + * `colorful({ DEBUG: "\033[0m"});` set level `DEBUG` color to `\033[0m`. + * `colorful(false)` turn off color. + * `colorful()` or `colorful(true)` turn on color. + * + */ + colorful:function (colors) { + var k; + if (Object.prototype.toString.call(colors) === '[object Object]') { + for (k in colors) { + if (colors.hasOwnProperty(k)) { + exports.colors[k] = colors[k]; + } + } + this.useColors = true; + } else if (colors === false) { + this.useColors = false; + } + else { + this.useColors = true; + } + return this; + }, -/** - * Purely informational message. - * - * @type Number - */ + /** + * Customize Levels and Colors. + * + * @param {Object|Array} levels + * @example + * `customLevels([ 'DEBUG', 'ERROR', 'WARNING' ];` + * `customLevels({ DEBUG: "\033[0m"});` + */ + customLevels: function( levels ){ -exports.INFO = 6; + var k; -/** - * Application debug messages. - * - * @type Number - */ + if( Object.prototype.toString.call( levels ) === '[object Array]' ){ -exports.DEBUG = 7; + for( k = 0; k < levels.length; k++ ){ -/** - * Application debug messages. - * - * @type Number - */ + addLevelEnumerable( levels[ k ] ); + } + } + else if( Object.prototype.toString.call( levels ) === '[object Object]' ){ + this.colorful( levels ); -exports.TRACE = 8; + for( k in levels ){ + addLevelEnumerable( k ); + } + } + return this; + }, -/** - * prototype. - */ + /** + * Set log level + * + * @param level + * @return {*} + */ + setLevel: function( level ){ -Log.prototype = { + if ('string' == typeof level) level = exports[level.toUpperCase()]; + this.level = level || this.level || exports.DEBUG; - /** - * Start emitting "line" events. - * - * @api public - */ - - read: function(){ - var buf = '' - , self = this - , stream = this.stream; - - stream.setEncoding('ascii'); - stream.on('data', function(chunk){ - buf += chunk; - if ('\n' != buf[buf.length - 1]) return; - buf.split('\n').map(function(line){ - if (!line.length) return; - try { - var captures = line.match(/^\[([^\]]+)\] (\w+) (.*)/); - var obj = { - date: new Date(captures[1]) - , level: exports[captures[2]] - , levelString: captures[2] - , msg: captures[3] - }; - self.emit('line', obj); - } catch (err) { - // Ignore - } - }); - buf = ''; - }); - - stream.on('end', function(){ - self.emit('end'); - }); - }, - - /** - * Log output message. - * - * @param {String} levelStr - * @param {Array} args - * @api private - */ - - log: function(levelStr, args) { - if (exports[levelStr] <= this.level) { - var i = 1; - var msg = args[0].replace(/%s/g, function(){ - return args[i++]; - }); - if (this.stream === process.stdout && - this.useColors && - exports.colors[levelStr]) { - - this.stream.write( - exports.colors[levelStr] - + '[' + new Date + ']' - + ' ' + levelStr - + ' ' + msg - + exports.colors.reset - + '\n' - ); - } else { - this.stream.write( - '[' + new Date + ']' - + ' ' + levelStr - + ' ' + msg - + '\n' - ); - } + return this; } - }, - - colorful : function (colors) { - var k; - if (Object.prototype.toString.call(colors) === '[object Object]') { - for (k in colors) { - if (colors.hasOwnProperty(k)) { - exports.colors[k] = colors[k]; - } - } - this.useColors = true; - } else if (colors === false) { this.useColors = false; } - else { this.useColors = true; } - return this; - }, - - /** - * Log emergency `msg`. - * - * @param {String} msg - * @api public - */ - - emergency: function(msg){ - this.log('EMERGENCY', arguments); - }, - - /** - * Log alert `msg`. - * - * @param {String} msg - * @api public - */ - - alert: function(msg){ - this.log('ALERT', arguments); - }, - - /** - * Log critical `msg`. - * - * @param {String} msg - * @api public - */ - - critical: function(msg){ - this.log('CRITICAL', arguments); - }, - - /** - * Log error `msg`. - * - * @param {String} msg - * @api public - */ - - error: function(msg){ - this.log('ERROR', arguments); - }, - - /** - * Log warning `msg`. - * - * @param {String} msg - * @api public - */ - - warning: function(msg){ - this.log('WARNING', arguments); - }, - - /** - * Log notice `msg`. - * - * @param {String} msg - * @api public - */ - - notice: function(msg){ - this.log('NOTICE', arguments); - }, - - /** - * Log info `msg`. - * - * @param {String} msg - * @api public - */ - - info: function(msg){ - this.log('INFO', arguments); - }, - - /** - * Log debug `msg`. - * - * @param {String} msg - * @api public - */ - - debug: function(msg){ - this.log('DEBUG', arguments); - }, - - /** - * Log trace `msg`. - * - * @param {String} msg - * @api public - */ - - trace: function(msg){ - this.log('TRACE', arguments); - } }; +/** + * Add Default levels to exports and add methods to Log.prototype. + */ +Log.prototype.customLevels( exports.colors ); + /** * Inherit from `EventEmitter`. */ From 8d72047e1d51e68688e8069a1616ded3cdcae885 Mon Sep 17 00:00:00 2001 From: neekey Date: Sun, 5 Aug 2012 22:56:16 +0800 Subject: [PATCH 7/7] update readme.md --- Readme.md | 17 +++++++++++++++++ lib/log.js | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index a6af36b..fd778ad 100644 --- a/Readme.md +++ b/Readme.md @@ -61,6 +61,23 @@ Setting up our own colors : 'INFO' : "\033[0;35m", 'DEBUG' : "\033[0m" }) + +Setting up your own log levels: + + log.customLevels([ 'COMMAND', 'DATA' ] ); + +Which will simple add methods `command` and `data` to the `Log` object, and you can use them as the way that default log methods be used, like `log.command( "a command message")`. + +Also, you can setting up your own log levels and their colors at the same time: + + log.customLevels({ + COMMAND: '\033[0;31m', + DATA: '\033[0;31m' + }); + + + + ## Reader diff --git a/lib/log.js b/lib/log.js index 9871016..2e3a2f4 100644 --- a/lib/log.js +++ b/lib/log.js @@ -231,7 +231,7 @@ Log.prototype = { * * @param {Object|Array} levels * @example - * `customLevels([ 'DEBUG', 'ERROR', 'WARNING' ];` + * `customLevels([ 'DEBUG', 'ERROR', 'WARNING' ]);` * `customLevels({ DEBUG: "\033[0m"});` */ customLevels: function( levels ){