Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added util.format-like functionality #156

Merged
merged 5 commits into from

3 participants

@fb55

As requested in #121, this adds a util.format-like functionality to all .log methods. To avoid any breakages, it will use the following n arguments, where n is the number of %* sequences present.

The error logging functions should probably also support this, but I haven't read enough code (ie. currently don't understand exception handling completely) to implement this properly, so it's going to be another pull request.

@mmalecki

Thanks Felix. Two things: you should use util.format from node core. Also, just format it once, in Logger#log method.

@fb55

util.format doesn't work in this case: All methods accept two optional parameters, and the only way of distinguishing optional parameters from parameters for the message is by implementing a function for this specific case.

I just realized that there's currently a bug when a message is passed from Logger#log to the transports, limiting message formatting to a single function would solve this.

@dscape

I thought this was ready to be merged?

@indexzero @mmalecki

@fb55

@dscape I wanted to change some things, but that's done now. Should be ready to be merged.

(Array-fication using Array#slice is still the fastest option when you want to ignore the first item, at least that's what a quick benchmark showed me. But a rest parameter should be used as soon as V8 supports them.)

@mmalecki

sorry for late response, awesome work Felix! Merging!

@mmalecki mmalecki merged commit af5e1f7 into winstonjs:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 23, 2012
  1. @fb55
  2. @fb55
Commits on Jul 27, 2012
  1. @fb55
  2. @fb55

    Revert "Use common.format inside every log method"

    fb55 authored
    This reverts commit acc244423e7ebd5d122e93e61d68f060389652d1.
  3. @fb55
This page is out of date. Refresh to see the latest.
Showing with 37 additions and 10 deletions.
  1. +25 −0 lib/winston/common.js
  2. +12 −10 lib/winston/logger.js
View
25 lib/winston/common.js
@@ -257,3 +257,28 @@ exports.serialize = function (obj, key) {
return msg;
};
+
+//
+// ### function format (arr)
+// #### @arr {Array} The array that contains all parts of the message
+// similar to util.format
+// returns the formated string
+// uses the original array and removes items as it proceeds
+//
+exports.format = function (arr) {
+ var msg = arr.shift();
+
+ if (typeof msg !== "string") {
+ return msg;
+ }
+
+ return msg.replace(/%[sdj%]/g, function (x){
+ switch (x.charAt(1)) {
+ case "j": return JSON.stringify(arr.shift());
+ case "s": return String(arr.shift());
+ case "d": return Number(arr.shift());
+ case "%": return "%";
+ default: return x;
+ }
+ });
+};
View
22 lib/winston/logger.js
@@ -120,23 +120,25 @@ Logger.prototype.extend = function (target) {
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
-Logger.prototype.log = function (level, msg) {
+Logger.prototype.log = function (level) {
var self = this,
+ args = Array.prototype.slice.call(arguments, 1),
+ msg = common.format(args),
callback,
meta;
- if (arguments.length === 3) {
- if (typeof arguments[2] === 'function') {
+ if (args.length === 1) {
+ if (typeof args[0] === 'function') {
meta = {};
- callback = arguments[2];
+ callback = args[0];
}
- else if (typeof arguments[2] === 'object') {
- meta = arguments[2];
+ else if (typeof args[0] === 'object') {
+ meta = args[0];
}
}
- else if (arguments.length === 4) {
- meta = arguments[2];
- callback = arguments[3];
+ else if (args.length === 2) {
+ meta = args[0];
+ callback = args[1];
}
// If we should pad for levels, do so
@@ -150,7 +152,7 @@ Logger.prototype.log = function (level, msg) {
}
else if (self.emitErrs) {
self.emit('error', err);
- };
+ }
}
if (this.transports.length === 0) {
Something went wrong with that request. Please try again.