Changes so that the entire message is in the color specified. #363

Closed
wants to merge 1 commit into from
View
109 lib/winston/common.js
@@ -7,9 +7,9 @@
*/
var util = require('util'),
- crypto = require('crypto'),
- cycle = require('cycle'),
- config = require('./config');
+ crypto = require('crypto'),
+ cycle = require('cycle'),
+ config = require('./config');
//
// ### function setLevels (target, past, current)
@@ -20,9 +20,9 @@ var util = require('util'),
// in current.levels. If past is defined, remove functions
// for each of those levels.
//
-exports.setLevels = function (target, past, current, isDefault) {
+exports.setLevels = function(target, past, current, isDefault) {
if (past) {
- Object.keys(past).forEach(function (level) {
+ Object.keys(past).forEach(function(level) {
delete target[level];
});
}
@@ -36,8 +36,8 @@ exports.setLevels = function (target, past, current, isDefault) {
// Define prototype methods for each log level
// e.g. target.log('info', msg) <=> target.info(msg)
//
- Object.keys(target.levels).forEach(function (level) {
- target[level] = function (msg) {
+ Object.keys(target.levels).forEach(function(level) {
+ target[level] = function(msg) {
// build argument list (level, msg, ... [string interpolate], [{metadata}], [callback])
var args = [level].concat(Array.prototype.slice.call(arguments));
target.log.apply(target, args);
@@ -52,10 +52,12 @@ exports.setLevels = function (target, past, current, isDefault) {
// #### @xs {Array} Array to calculate against
// Returns the longest element in the `xs` array.
//
-exports.longestElement = function (xs) {
+exports.longestElement = function(xs) {
return Math.max.apply(
null,
- xs.map(function (x) { return x.length; })
+ xs.map(function(x) {
+ return x.length;
+ })
);
};
@@ -65,27 +67,23 @@ exports.longestElement = function (xs) {
// Helper method for deep cloning pure JSON objects
// i.e. JSON objects that are either literals or objects (no Arrays, etc)
//
-exports.clone = function (obj) {
+exports.clone = function(obj) {
// we only need to clone refrence types (Object)
if (!(obj instanceof Object)) {
return obj;
- }
- else if (obj instanceof Date) {
+ } else if (obj instanceof Date) {
return obj;
}
var copy = {};
for (var i in obj) {
if (Array.isArray(obj[i])) {
copy[i] = obj[i].slice(0);
- }
- else if (obj[i] instanceof Buffer) {
- copy[i] = obj[i].slice(0);
- }
- else if (typeof obj[i] != 'function') {
+ } else if (obj[i] instanceof Buffer) {
+ copy[i] = obj[i].slice(0);
+ } else if (typeof obj[i] != 'function') {
copy[i] = obj[i] instanceof Object ? exports.clone(obj[i]) : obj[i];
- }
- else if (typeof obj[i] === 'function') {
+ } else if (typeof obj[i] === 'function') {
copy[i] = obj[i];
}
}
@@ -108,23 +106,23 @@ exports.clone = function (obj) {
// label: 'label to prepend the message'
// }
//
-exports.log = function (options) {
- var timestampFn = typeof options.timestamp === 'function'
- ? options.timestamp
- : exports.timestamp,
- timestamp = options.timestamp ? timestampFn() : null,
- meta = options.meta !== undefined || options.meta !== null ? exports.clone(cycle.decycle(options.meta)) : null,
- output;
+exports.log = function(options) {
+ var timestampFn = typeof options.timestamp === 'function' ? options.timestamp : exports.timestamp,
+ timestamp = options.timestamp ? timestampFn() : null,
+ meta = options.meta !== undefined || options.meta !== null ? exports.clone(cycle.decycle(options.meta)) : null,
+ output;
//
// raw mode is intended for outputing winston as streaming JSON to STDOUT
//
if (options.raw) {
if (typeof meta !== 'object' && meta != null) {
- meta = { meta: meta };
+ meta = {
+ meta: meta
+ };
}
- output = exports.clone(meta) || {};
- output.level = options.level;
+ output = exports.clone(meta) || {};
+ output.level = options.level;
output.message = options.message.stripColors;
return JSON.stringify(output);
}
@@ -134,17 +132,19 @@ exports.log = function (options) {
//
if (options.json) {
if (typeof meta !== 'object' && meta != null) {
- meta = { meta: meta };
+ meta = {
+ meta: meta
+ };
}
- output = exports.clone(meta) || {};
- output.level = options.level;
+ output = exports.clone(meta) || {};
+ output.level = options.level;
output.message = options.message;
if (timestamp) {
output.timestamp = timestamp;
}
-
+
if (options.logstash === true) {
// use logstash format
var logstashOutput = {};
@@ -166,18 +166,16 @@ exports.log = function (options) {
return options.stringify(output);
}
- return JSON.stringify(output, function (key, value) {
- return value instanceof Buffer
- ? value.toString('base64')
- : value;
+ return JSON.stringify(output, function(key, value) {
+ return value instanceof Buffer ? value.toString('base64') : value;
});
}
output = timestamp ? timestamp + ' - ' : '';
output += options.colorize ? config.colorize(options.level) : options.level;
output += ': ';
output += options.label ? ('[' + options.label + '] ') : '';
- output += options.message;
+ output += options.colorize ? config.colorize(options.level, options.message) : options.message;
if (meta !== null && meta !== undefined) {
if (meta && meta instanceof Error && meta.stack) {
@@ -186,20 +184,17 @@ exports.log = function (options) {
if (typeof meta !== 'object') {
output += ' ' + meta;
- }
- else if (Object.keys(meta).length > 0) {
+ } else if (Object.keys(meta).length > 0) {
output += ' ' + (
- options.prettyPrint
- ? ('\n' + util.inspect(meta, false, null, options.colorize))
- : exports.serialize(meta)
+ options.prettyPrint ? ('\n' + util.inspect(meta, false, null, options.colorize)) : exports.serialize(meta)
);
}
}
return output;
};
-exports.capitalize = function (str) {
+exports.capitalize = function(str) {
return str && str[0].toUpperCase() + str.slice(1);
};
@@ -209,23 +204,23 @@ exports.capitalize = function (str) {
// Utility function for creating unique ids
// e.g. Profiling incoming HTTP requests on the same tick
//
-exports.hash = function (str) {
+exports.hash = function(str) {
return crypto.createHash('sha1').update(str).digest('hex');
};
//
// ### function pad (n)
// Returns a padded string if `n < 10`.
//
-exports.pad = function (n) {
+exports.pad = function(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
};
//
// ### function timestamp ()
// Returns a timestamp string for the current time.
//
-exports.timestamp = function () {
+exports.timestamp = function() {
return new Date().toISOString();
};
@@ -236,14 +231,12 @@ exports.timestamp = function () {
// Performs simple comma-separated, `key=value` serialization for Loggly when
// logging to non-JSON inputs.
//
-exports.serialize = function (obj, key) {
+exports.serialize = function(obj, key) {
if (obj === null) {
obj = 'null';
- }
- else if (obj === undefined) {
+ } else if (obj === undefined) {
obj = 'undefined';
- }
- else if (obj === false) {
+ } else if (obj === false) {
obj = 'false';
}
@@ -256,8 +249,8 @@ exports.serialize = function (obj, key) {
}
var msg = '',
- keys = Object.keys(obj),
- length = keys.length;
+ keys = Object.keys(obj),
+ length = keys.length;
for (var i = 0; i < length; i++) {
if (Array.isArray(obj[keys[i]])) {
@@ -271,11 +264,9 @@ exports.serialize = function (obj, key) {
}
msg += ']';
- }
- else if (obj[keys[i]] instanceof Date) {
+ } else if (obj[keys[i]] instanceof Date) {
msg += keys[i] + '=' + obj[keys[i]];
- }
- else {
+ } else {
msg += exports.serialize(obj[keys[i]], keys[i]);
}
@@ -285,4 +276,4 @@ exports.serialize = function (obj, key) {
}
return msg;
-};
+};
View
21 lib/winston/config.js
@@ -9,14 +9,17 @@
var colors = require('colors');
var config = exports,
- allColors = exports.allColors = {};
+ allColors = exports.allColors = {};
-config.addColors = function (colors) {
+config.addColors = function(colors) {
mixin(allColors, colors);
};
-config.colorize = function (level) {
- var colorized = level;
+config.colorize = function(level, message) {
+ if (message === undefined) {
+ message = level
+ }
+ var colorized = message;
if (allColors[level] instanceof Array) {
for (var i = 0, l = allColors[level].length; i < l; ++i) {
colorized = colorized[allColors[level][i]];
@@ -36,8 +39,8 @@ config.colorize = function (level) {
//
// Export config sets
//
-config.cli = require('./config/cli-config');
-config.npm = require('./config/npm-config');
+config.cli = require('./config/cli-config');
+config.npm = require('./config/npm-config');
config.syslog = require('./config/syslog-config');
//
@@ -46,14 +49,14 @@ config.syslog = require('./config/syslog-config');
config.addColors(config.npm.colors);
config.addColors(config.syslog.colors);
-function mixin (target) {
+function mixin(target) {
var args = Array.prototype.slice.call(arguments, 1);
- args.forEach(function (a) {
+ args.forEach(function(a) {
var keys = Object.keys(a);
for (var i = 0; i < keys.length; i++) {
target[keys[i]] = a[keys[i]];
}
});
return target;
-};
+};