Skip to content

Commit

Permalink
Adding levelDecorators and the option to redirect debug messages to s…
Browse files Browse the repository at this point in the history
…tdOut
  • Loading branch information
Ken Perkins committed Jan 17, 2012
1 parent 1438e3d commit d9c100a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ test/fixtures/*.json
test/fixtures/logs/*.log
node_modules/
node_modules/*
npm-debug.log
npm-debug.log
.idea/*
3 changes: 2 additions & 1 deletion lib/winston/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ exports.log = function (options) {
var timestampFn = typeof options.timestamp === 'function' ? options.timestamp : exports.timestamp,
timestamp = options.timestamp ? timestampFn() : null,
meta = options.meta ? exports.clone(options.meta) : null,
decoratedLevel = options.levelDecorators[0] + options.level + options.levelDecorators[1],
output;

if (options.json) {
Expand All @@ -120,7 +121,7 @@ exports.log = function (options) {
}

output = timestamp ? timestamp + ' - ' : '';
output += options.colorize ? config.colorize(options.level) : options.level;
output += options.colorize ? config.colorize(options.level, options.levelDecorators) : decoratedLevel;
output += (': ' + options.message);

if (meta) {
Expand Down
5 changes: 3 additions & 2 deletions lib/winston/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ config.addColors = function (colors) {
mixin(allColors, colors);
};

config.colorize = function (level) {
return level[allColors[level]];
config.colorize = function (level, levelDecorators) {
var formattedLevel = levelDecorators[0] + level + levelDecorators[1];
return formattedLevel[allColors[level]];
};

//
Expand Down
19 changes: 16 additions & 3 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,23 @@ var Logger = exports.Logger = function (options) {
this.level = options.level || 'info';
this.emitErrs = options.emitErrs || false;
this.stripColors = options.stripColors || false;
this.exitOnError = typeof options.exitOnError !== 'undefined'
? options.exitOnError
this.exitOnError = typeof options.exitOnError !== 'undefined'
? options.exitOnError
: true;


//
// For now this is a hack as I'm still figuring out what I want
// the decorators to look like
//
if (options.levelDecorators && options.levelDecorators.length == 2) {
this.levelDecorators = options.levelDecorators;
}
else {
this.levelDecorators = ['',''];
}

this.debugToStdErr = options.debugToStdOut || false;

//
// Setup other intelligent default settings.
//
Expand Down
11 changes: 7 additions & 4 deletions lib/winston/transports/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ var Console = exports.Console = function (options) {
this.json = options.json || false;
this.colorize = options.colorize || false;
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false;

this.levelDecorators = options.levelDecorators;
this.debugToStdOut = options.debugToStdOut || false;

if (this.json) {
this.stringify = options.stringify || function (obj) {
return JSON.stringify(obj, null, 2);
Expand Down Expand Up @@ -64,10 +66,11 @@ Console.prototype.log = function (level, msg, meta, callback) {
message: msg,
meta: meta,
stringify: this.stringify,
timestamp: this.timestamp
timestamp: this.timestamp,
levelDecorators: this.levelDecorators
});
if (level === 'error' || level === 'debug') {

if (level === 'error' || (level === 'debug' && !self.debugToStdOut)) {
util.error(output);
}
else {
Expand Down

0 comments on commit d9c100a

Please sign in to comment.