Browse files

[fix] Ensure that levelLength is set in the default logger. Check for…

… callbacks before returning errors in `winston.logger.log`
  • Loading branch information...
1 parent a9afd8d commit 16cf13f539c0383be39b79909d39defa8d677616 @indexzero indexzero committed May 19, 2011
Showing with 38 additions and 24 deletions.
  1. +22 −20 lib/winston.js
  2. +15 −2 lib/winston/logger.js
  3. +0 −1 lib/winston/utils.js
  4. +1 −1 test/logger-test.js
View
42 lib/winston.js
@@ -54,33 +54,35 @@ utils.setLevels(winston, null, defaultLogger.levels);
};
});
+winston.cli = function (foo, bar) {
+ winston.padLevels = true;
+ utils.setLevels(winston, defaultLogger.levels, winston.config.cli.levels);
+ winston.config.addColors(winston.config.cli.colors);
+
+ if (defaultLogger.transports.console) {
+ defaultLogger.transports.console.colorize = true;
+ defaultLogger.transports.console.timestamp = false;
+ }
+};
+
winston.setLevels = function (levels) {
utils.setLevels(winston, defaultLogger.levels, levels);
defaultLogger.setLevels(levels);
};
//
-// Define emitErrs property for default logger
-//
-Object.defineProperty(winston, 'emitErrs', {
- get: function () {
- return defaultLogger.emitErrs;
- },
- set: function (val) {
- defaultLogger.emitErrs = val;
- }
-});
-
-//
-// Define padLevels property for default logger
+// Define getters / setters for appropriate properties of the
+// default logger which need to be exposed by winston.
//
-Object.defineProperty(winston, 'padLevels', {
- get: function () {
- return defaultLogger.padLevels;
- },
- set: function (val) {
- defaultLogger.padLevels = val;
- }
+['emitErrs', 'padLevels', 'levelLength'].forEach(function (prop) {
+ Object.defineProperty(winston, prop, {
+ get: function () {
+ return defaultLogger[prop];
+ },
+ set: function (val) {
+ defaultLogger[prop] = val;
+ }
+ });
});
//
View
17 lib/winston/logger.js
@@ -92,8 +92,21 @@ Logger.prototype.log = function (level, msg) {
msg = new Array(this.levelLength - level.length).join(' ') + msg;
}
- if (this.transports.length === 0) return callback(new Error('Cannot log with no transports.'));
- else if (typeof self.levels[level] === 'undefined') return callback(new Error('Unknown log level: ' + level));
+ function onError (err) {
+ if (callback) {
+ callback(err);
+ }
+ else if (self.emitErrs){
+ throw err;
+ };
+ }
+
+ if (this.transports.length === 0) {
+ return onError(new Error('Cannot log with no transports.'));
+ }
+ else if (typeof self.levels[level] === 'undefined') {
+ return onError(new Error('Unknown log level: ' + level));
+ }
for (var key in this.transports) {
var transport = this.transports[key];
View
1 lib/winston/utils.js
@@ -25,7 +25,6 @@ var setLevels = exports.setLevels = function (target, past, current, isDefault)
}
target.levels = current || config.npm.levels;
-
if (target.padLevels) {
target.levelLength = longestElement(Object.keys(target.levels));
}
View
2 test/logger-test.js
@@ -28,7 +28,7 @@ vows.describe('winton/logger').addBatch({
}
}).addBatch({
"An instance of winston.Logger with no transports": {
- topic: new (winston.Logger)(),
+ topic: new (winston.Logger)({ emitErrs: true }),
"the log() method should throw an error": function (logger) {
assert.throws(function () { logger.log('anything') }, Error);
},

0 comments on commit 16cf13f

Please sign in to comment.