Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse log level order for all levels so that syslog can be used again #290

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/custom-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ var winston = require('../lib/winston');
//
var config = {
levels: {
silly: 0,
verbose: 1,
info: 2,
error: 0,
debug: 1,
warn: 2,
data: 3,
warn: 4,
debug: 5,
error: 6
info: 4,
verbose: 5,
silly: 6
},
colors: {
silly: 'magenta',
Expand All @@ -41,4 +41,4 @@ var logger = module.exports = new (winston.Logger)({
colors: config.colors
});

logger.data('hello')
logger.data('hello')
22 changes: 11 additions & 11 deletions lib/winston/config/cli-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
var cliConfig = exports;

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

cliConfig.colors = {
Expand All @@ -32,4 +32,4 @@ cliConfig.colors = {
help: 'cyan',
warn: 'yellow',
error: 'red'
};
};
14 changes: 7 additions & 7 deletions lib/winston/config/npm-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
var npmConfig = exports;

npmConfig.levels = {
silly: 0,
debug: 1,
verbose: 2,
info: 3,
warn: 4,
error: 5
error: 0,
warn: 1,
info: 2,
debug: 3,
verbose: 4,
silly: 5
};

npmConfig.colors = {
Expand All @@ -24,4 +24,4 @@ npmConfig.colors = {
info: 'green',
warn: 'yellow',
error: 'red'
};
};
34 changes: 32 additions & 2 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ Logger.prototype.extend = function (target) {
return this;
};

//
// ### function shouldWriteMessage (transport, messageLevel)
// #### @transport {object} The current transport in use
// #### @messageLevel {string} Level at which to log the message.
// Helper method for working out if we should write a message or not.
// Used internally + in tests
//
Logger.prototype.shouldWriteMessage = function (transport, messageLevel) {

// By default, our minimum level is the one defined by winston
var minimumLevel = this.level;

// If the transport has a minimum level set, use it
if (transport.level){
minimumLevel = transport.level;
}

// Return whether the provided level is important enough to be logged
// It's important to note that LOWER numbers actually have higher
// priority due to the sysLog
return (this.levels[messageLevel] <= this.levels[minimumLevel]);
}

//
// ### function log (level, msg, [meta], callback)
// #### @level {string} Level at which to log the message.
Expand Down Expand Up @@ -181,8 +204,7 @@ Logger.prototype.log = function (level) {
//
function emit(name, next) {
var transport = self.transports[name];
if ((transport.level && self.levels[transport.level] <= self.levels[level])
|| (!transport.level && self.levels[self.level] <= self.levels[level])) {
if (self.shouldWriteMessage(transport, level)){
transport.log(level, msg, meta, function (err) {
if (err) {
err.transport = transport;
Expand Down Expand Up @@ -598,6 +620,14 @@ Logger.prototype.setLevels = function (target) {
return common.setLevels(this, this.levels, target);
};

//
// ### function getLevels (target)
// Gets the `target` levels specified on this instance.
//
Logger.prototype.getLevels = function () {
return this.levels;
};

//
// ### function cli ()
// Configures this instance to have the default
Expand Down
103 changes: 103 additions & 0 deletions test/level-order-test-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* level-order-test.js: Regression test for making sure log levels stay in the correct order
*
* (C) 2013 Michael Heap
* MIT LICENSE
*
*/

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

vows.describe('winston/levels/order/syslog').addBatch({
"Winston using the cli config": {
topic: function(){
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)()
]
});

logger.setLevels(winston.config.cli.levels);
return logger;
},
"the error level": {
"is equal to zero": function(logger){
assert.strictEqual(logger.getLevels().error, 0);
},
"is of a higher priority than warn": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"warn"}, "error"));
}
},
"the warn level": {
"is equal to one": function(logger){
assert.strictEqual(logger.getLevels().warn, 1);
},
"is of a higher priority than help": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"help"}, "warn"));
}
},
"the help level": {
"is equal to two": function(logger){
assert.strictEqual(logger.getLevels().help, 2);
},
"is of a higher priority than error": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"data"}, "help"));
}
},
"the data level": {
"is equal to three": function(logger){
assert.strictEqual(logger.getLevels().data, 3);
},
"is of a higher priority than warning": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"info"}, "data"));
}
},
"the info level": {
"is equal to four": function(logger){
assert.strictEqual(logger.getLevels().info, 4);
},
"is of a higher priority than notice": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"debug"}, "info"));
}
},
"the debug level": {
"is equal to five": function(logger){
assert.strictEqual(logger.getLevels().debug, 5);
},
"is of a higher priority than info": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"prompt"}, "debug"));
}
},
"the prompt level": {
"is equal to six": function(logger){
assert.strictEqual(logger.getLevels().prompt, 6);
},
"is of a higher priority than debug": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"verbose"}, "prompt"));
}
},
"the verbose level": {
"is equal to seven": function(logger){
assert.strictEqual(logger.getLevels().verbose, 7);
},
"is of a higher priority than input": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"input"}, "verbose"));
}
},
"the input level": {
"is equal to eight": function(logger){
assert.strictEqual(logger.getLevels().input, 8);
},
"is of a higher priority than debug": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"silly"}, "input"));
}
},
"the silly level": {
"is equal to nine": function(logger){
assert.strictEqual(logger.getLevels().silly, 9);
}
},
}
}).export(module);
69 changes: 69 additions & 0 deletions test/level-order-test-npm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* level-order-test.js: Regression test for making sure log levels stay in the correct order
*
* (C) 2013 Michael Heap
* MIT LICENSE
*
*/

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

vows.describe('winston/levels/order/npm').addBatch({
"Winston using the npm config": {
topic: function(){
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)()
]
});
return logger;
},
"the error level": {
"is equal to zero": function(logger){
assert.strictEqual(logger.getLevels().error, 0);
},
"is of a higher priority than warn": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"warn"}, "error"));
}
},
"the warn level": {
"is equal to one": function(logger){
assert.strictEqual(logger.getLevels().warn, 1);
},
"is of a higher priority than crit": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"info"}, "warn"));
}
},
"the info level": {
"is equal to two": function(logger){
assert.strictEqual(logger.getLevels().info, 2);
},
"is of a higher priority than error": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"debug"}, "info"));
}
},
"the debug level": {
"is equal to three": function(logger){
assert.strictEqual(logger.getLevels().debug, 3);
},
"is of a higher priority than verbosej": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"verbose"}, "debug"));
}
},
"the verbose level": {
"is equal to four": function(logger){
assert.strictEqual(logger.getLevels().verbose, 4);
},
"is of a higher priority than silly": function(logger){
assert.ok(logger.shouldWriteMessage({"level":"silly"}, "verbose"));
}
},
"the silly level": {
"is equal to five": function(logger){
assert.strictEqual(logger.getLevels().silly, 5);
}
}
}
}).export(module);
Loading