Skip to content

Commit

Permalink
[api test] Added .cli() method for pretty formatting CLI output
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed May 20, 2011
1 parent f45c7fb commit 6b540f8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/winston.js
Expand Up @@ -48,7 +48,7 @@ winston.Logger = require('winston/logger').Logger;
var defaultLogger = new (winston.Logger)({ transports: [new (winston.transports.Console)()] });
utils.setLevels(winston, null, defaultLogger.levels);

['log', 'add', 'remove', 'profile', 'extend'].forEach(function (method) {
['log', 'add', 'remove', 'profile', 'extend', 'cli'].forEach(function (method) {
winston[method] = function () {
return defaultLogger[method].apply(defaultLogger, arguments);
};
Expand Down
3 changes: 2 additions & 1 deletion lib/winston/config.js
Expand Up @@ -10,7 +10,7 @@ require.paths.unshift(__dirname);

var colors = require('colors'),
config = exports,
allColors = {};
allColors = exports.allColors = {};

function mixin (target) {
var args = Array.prototype.slice.call(arguments, 1);
Expand All @@ -35,6 +35,7 @@ config.colorize = function (level) {
//
// Export config sets
//
config.cli = require('config/cli-config');
config.npm = require('config/npm-config');
config.syslog = require('config/syslog-config');

Expand Down
35 changes: 35 additions & 0 deletions lib/winston/config/cli-config.js
@@ -0,0 +1,35 @@
/*
* cli-config.js: Config that conform to commonly used CLI logging levels.
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/

var cliConfig = exports;

cliConfig.levels = {
silly: 0,
input: 1,
verbose: 2,
prompt: 3,
info: 4,
data: 5,
help: 6,
warn: 7,
debug: 8,
error: 9
};

cliConfig.colors = {
silly: 'magenta',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
};
11 changes: 11 additions & 0 deletions lib/winston/logger.js
Expand Up @@ -176,4 +176,15 @@ Logger.prototype.profile = function (id) {

Logger.prototype.setLevels = function (current) {
return utils.setLevels(this, this.levels, current);
};

Logger.prototype.cli = function () {
this.padLevels = true;
this.setLevels(winston.config.cli.levels);
winston.config.addColors(winston.config.cli.colors);

if (this.transports.console) {
this.transports.console.colorize = true;
this.transports.console.timestamp = false;
}
};
42 changes: 42 additions & 0 deletions test/cli-test.js
@@ -0,0 +1,42 @@
/*
* cli-test.js: Tests for the cli levels available in winston.
*
* (C) 2010 Charlie Robbins
* MIT LICENSE
*
*/

require.paths.unshift(require('path').join(__dirname, '..', 'lib'));

var path = require('path'),
vows = require('vows'),
assert = require('assert'),
winston = require('winston'),
helpers = require('./helpers');

vows.describe('winston/logger/cli').addBatch({
"When an instance of winston.Logger": {
topic: function () {
return new winston.Logger({
transports: [
new winston.transports.Console()
]
})
},
"the cli() method": {
"should set the appropriate values on the logger": function (logger) {
logger.cli();
assert.isTrue(logger.padLevels);
assert.isTrue(logger.transports.console.colorize);
assert.isFalse(logger.transports.console.timestamp);
Object.keys(winston.config.cli.levels).forEach(function (level) {
assert.isNumber(logger.levels[level]);
});

Object.keys(winston.config.cli.colors).forEach(function (color) {
assert.isString(winston.config.allColors[color]);
});
}
}
}
}).export(module);

0 comments on commit 6b540f8

Please sign in to comment.