winston-middleware 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.travis.yml CHANGED
@@ -1,3 +1,4 @@
1
+ sudo: false
1
2
  language: node_js
2
3
  node_js:
3
4
  - "0.10"
package/AUTHORS CHANGED
@@ -5,3 +5,4 @@ Xavier Damman (http://xdamman.com)
5
5
  Quentin Rossetti <quentin.rossetti@gmail.com>
6
6
  Damian Kaczmarek <rush@rushbase.net>
7
7
  Robbie Trencheny <me@robbiet.us> (http://robbie.io)
8
+ Ross Brandes <ross.brandes@gmail.com>
package/Readme.md CHANGED
@@ -75,11 +75,17 @@ Use `expressWinston.logger(options)` to create a middleware to log your HTTP req
75
75
  meta: Boolean, // control whether you want to log the meta data about the request (default to true).
76
76
  baseMeta: Object, // default meta data to be added to log, this will be merged with the meta data.
77
77
  metaField: String, // if defined, the meta data will be added in this field instead of the meta root object.
78
- statusLevels: Boolean // different HTTP status codes caused log messages to be logged at different levels (info/warn/error), the default is false
78
+ statusLevels: Boolean or Object // different HTTP status codes caused log messages to be logged at different levels (info/warn/error), the default is false. Use an object to control the levels various status codes are logged at.
79
79
  ignoreRoute: function (req, res) { return false; } // allows to skip some log messages based on request and/or response.
80
80
  skip: function(req, res) { return false; } // function to determine if logging is skipped, defaults to false.
81
81
  requestFilter: function (req, propName) { return req[propName]; } // A function to filter/return request values, defaults to returning all values allowed by whitelist. If the function returns undefined, the key/value will not be included in the meta.
82
82
  responseFilter: function (res, propName) { return res[propName]; } // A function to filter/return response values, defaults to returning all values allowed by whitelist. If the function returns undefined, the key/value will not be included in the meta.
83
+ requestWhitelist: [String] // Array of request properties to log. Overrides global requestWhitelist for this instance
84
+ responseWhitelist: [String] // Array of response properties to log. Overrides global responseWhitelist for this instance
85
+ bodyWhitelist: [String] // Array of body properties to log. Overrides global bodyWhitelist for this instance
86
+ bodyBlacklist: [String] // Array of body properties to omit from logs. Overrides global bodyBlacklist for this instance
87
+ ignoredRoutes: [String] // Array of paths to ignore/skip logging. Overrides global ignoredRoutes for this instance
88
+
83
89
  ```
84
90
 
85
91
  ### Error Logging
@@ -111,6 +117,7 @@ The logger needs to be added AFTER the express router(`app.router)`) and BEFORE
111
117
  baseMeta: Object, // default meta data to be added to log, this will be merged with the error data.
112
118
  metaField: String, // if defined, the meta data will be added in this field instead of the meta root object.
113
119
  requestFilter: function (req, propName) { return req[propName]; } // A function to filter/return request values, defaults to returning all values allowed by whitelist. If the function returns undefined, the key/value will not be included in the meta.
120
+ requestWhitelist: [String] // Array of request properties to log. Overrides global requestWhitelist for this instance
114
121
  ```
115
122
 
116
123
  To use winston's existing transports, set `transports` to the values (as in key-value) of the `winston.default.transports` object. This may be done, for example, by using underscorejs: `transports: _.values(winston.default.transports)`.
@@ -366,6 +373,17 @@ If both `req._bodyWhitelist.body` and `req._bodyBlacklist.body` are set the resu
366
373
  excluding any black listed ones. In the above example, only 'email' and 'age' would be included.
367
374
 
368
375
 
376
+ ## Custom Status Levels
377
+
378
+ If you set statusLevels to true winston-middleware will log sub 400 responses at info level, sub 500 responses as warnings and 500+ responses as errors. To change these levels specify an object as follows
379
+ ```json
380
+ "statusLevels": {
381
+ "success": "debug",
382
+ "warn": "debug",
383
+ "error": "info"
384
+ }
385
+ ```
386
+
369
387
  ## Tests
370
388
 
371
389
  Run the basic Mocha tests:
@@ -389,6 +407,7 @@ If you ran into any problems, please use the project [Issues section](https://gi
389
407
  * [Johan Hernandez](https://github.com/bithavoc) (https://github.com/bithavoc)
390
408
  * [Lars Jacob](https://github.com/jaclar) (https://github.com/jaclar)
391
409
  * [Jonathan Lomas](https://github.com/floatingLomas) (https://github.com/floatingLomas)
410
+ * [Ross Brandes](https://github.com/rosston) (https://github.com/rosston)
392
411
 
393
412
  Also see AUTHORS file, add yourself if you are missing.
394
413
 
package/index.js CHANGED
@@ -34,34 +34,34 @@ delete require.cache[require.resolve('underscore')];
34
34
  * TODO: Include 'body' and get the defaultRequestFilter to filter the inner properties like 'password' or 'password_confirmation', etc. Pull requests anyone?
35
35
  * @type {Array}
36
36
  */
37
- var requestWhitelist = ['url', 'headers', 'method', 'httpVersion', 'originalUrl', 'query'];
37
+ exports.requestWhitelist = ['url', 'headers', 'method', 'httpVersion', 'originalUrl', 'query'];
38
38
 
39
39
  /**
40
40
  * A default list of properties in the request body that are allowed to be logged.
41
41
  * This will normally be empty here, since it should be done at the route level.
42
42
  * @type {Array}
43
43
  */
44
- var bodyWhitelist = [];
44
+ exports.bodyWhitelist = [];
45
45
 
46
46
  /**
47
47
  * A default list of properties in the request body that are not allowed to be logged.
48
48
  * @type {Array}
49
49
  */
50
- var bodyBlacklist = [];
50
+ exports.bodyBlacklist = [];
51
51
 
52
52
  /**
53
53
  * A default list of properties in the response object that are allowed to be logged.
54
54
  * These properties will be safely included in the meta of the log.
55
55
  * @type {Array}
56
56
  */
57
- var responseWhitelist = ['statusCode'];
57
+ exports.responseWhitelist = ['statusCode'];
58
58
 
59
59
  /**
60
60
  * A list of request routes that will be skipped instead of being logged. This would be useful if routes for health checks or pings would otherwise pollute
61
61
  * your log files.
62
62
  * @type {Array}
63
63
  */
64
- var ignoredRoutes = [];
64
+ exports.ignoredRoutes = [];
65
65
 
66
66
  /**
67
67
  * A default function to filter the properties of the req object.
@@ -69,7 +69,7 @@ var ignoredRoutes = [];
69
69
  * @param propName
70
70
  * @return {*}
71
71
  */
72
- var defaultRequestFilter = function (req, propName) {
72
+ exports.defaultRequestFilter = function (req, propName) {
73
73
  return req[propName];
74
74
  };
75
75
 
@@ -79,7 +79,7 @@ var defaultRequestFilter = function (req, propName) {
79
79
  * @param propName
80
80
  * @return {*}
81
81
  */
82
- var defaultResponseFilter = function (res, propName) {
82
+ exports.defaultResponseFilter = function (res, propName) {
83
83
  return res[propName];
84
84
  };
85
85
 
@@ -87,7 +87,7 @@ var defaultResponseFilter = function (res, propName) {
87
87
  * A default function to decide whether skip logging of particular request. Doesn't skip anything (i.e. log all requests).
88
88
  * @return always false
89
89
  */
90
- var defaultSkip = function() {
90
+ exports.defaultSkip = function() {
91
91
  return false;
92
92
  };
93
93
 
@@ -114,11 +114,12 @@ function filterObject(originalObj, whiteList, initialFilter) {
114
114
  //
115
115
 
116
116
 
117
- function errorLogger(options) {
117
+ exports.errorLogger = function errorLogger(options) {
118
118
 
119
119
  ensureValidOptions(options);
120
120
 
121
- options.requestFilter = options.requestFilter || defaultRequestFilter;
121
+ options.requestWhitelist = options.requestWhitelist || exports.requestWhitelist;
122
+ options.requestFilter = options.requestFilter || exports.defaultRequestFilter;
122
123
  options.winstonInstance = options.winstonInstance || (new winston.Logger ({ transports: options.transports }));
123
124
  options.msg = options.msg || 'middlewareError';
124
125
  options.baseMeta = options.baseMeta || {};
@@ -133,7 +134,7 @@ function errorLogger(options) {
133
134
 
134
135
  // Let winston gather all the error data.
135
136
  var exceptionMeta = winston.exception.getAllInfo(err);
136
- exceptionMeta.req = filterObject(req, requestWhitelist, options.requestFilter);
137
+ exceptionMeta.req = filterObject(req, options.requestWhitelist, options.requestFilter);
137
138
 
138
139
  if (options.metaField) {
139
140
  var newMeta = {};
@@ -148,7 +149,7 @@ function errorLogger(options) {
148
149
 
149
150
  next(err);
150
151
  };
151
- }
152
+ };
152
153
 
153
154
  //
154
155
  // ### function logger(options)
@@ -156,13 +157,18 @@ function errorLogger(options) {
156
157
  //
157
158
 
158
159
 
159
- function logger(options) {
160
+ exports.logger = function logger(options) {
160
161
 
161
162
  ensureValidOptions(options);
162
163
  ensureValidLoggerOptions(options);
163
164
 
164
- options.requestFilter = options.requestFilter || defaultRequestFilter;
165
- options.responseFilter = options.responseFilter || defaultResponseFilter;
165
+ options.requestWhitelist = options.requestWhitelist || exports.requestWhitelist;
166
+ options.bodyWhitelist = options.bodyWhitelist || exports.bodyWhitelist;
167
+ options.bodyBlacklist = options.bodyBlacklist || exports.bodyBlacklist;
168
+ options.responseWhitelist = options.responseWhitelist || exports.responseWhitelist;
169
+ options.requestFilter = options.requestFilter || exports.defaultRequestFilter;
170
+ options.responseFilter = options.responseFilter || exports.defaultResponseFilter;
171
+ options.ignoredRoutes = options.ignoredRoutes || exports.ignoredRoutes;
166
172
  options.winstonInstance = options.winstonInstance || (new winston.Logger ({ transports: options.transports }));
167
173
  options.level = options.level || "info";
168
174
  options.statusLevels = options.statusLevels || false;
@@ -172,7 +178,7 @@ function logger(options) {
172
178
  options.colorStatus = options.colorStatus || false;
173
179
  options.expressFormat = options.expressFormat || false;
174
180
  options.ignoreRoute = options.ignoreRoute || function () { return false; };
175
- options.skip = options.skip || defaultSkip;
181
+ options.skip = options.skip || exports.defaultSkip;
176
182
 
177
183
  // Using mustache style templating
178
184
  var template = _.template(options.msg, null, {
@@ -181,7 +187,7 @@ function logger(options) {
181
187
 
182
188
  return function (req, res, next) {
183
189
  var currentUrl = req.originalUrl || req.url;
184
- if (currentUrl && _.contains(ignoredRoutes, currentUrl)) return next();
190
+ if (currentUrl && _.contains(options.ignoredRoutes, currentUrl)) return next();
185
191
  if (options.ignoreRoute(req, res)) return next();
186
192
 
187
193
  req._startTime = (new Date);
@@ -207,9 +213,9 @@ function logger(options) {
207
213
  req.url = req.originalUrl || req.url;
208
214
 
209
215
  if (options.statusLevels) {
210
- if (res.statusCode >= 100) { options.level = "info"; }
211
- if (res.statusCode >= 400) { options.level = "warn"; }
212
- if (res.statusCode >= 500) { options.level = "error"; }
216
+ if (res.statusCode >= 100) { options.level = options.statusLevels.success || "info"; }
217
+ if (res.statusCode >= 400) { options.level = options.statusLevels.warn || "warn"; }
218
+ if (res.statusCode >= 500) { options.level = options.statusLevels.error || "error"; }
213
219
  };
214
220
 
215
221
  if (options.colorStatus || options.expressFormat) {
@@ -225,10 +231,9 @@ function logger(options) {
225
231
 
226
232
  if(options.meta !== false) {
227
233
  var logData = {};
228
- var bodyWhitelist, blacklist;
229
234
 
230
- requestWhitelist = requestWhitelist.concat(req._routeWhitelists.req || []);
231
- responseWhitelist = responseWhitelist.concat(req._routeWhitelists.res || []);
235
+ var requestWhitelist = options.requestWhitelist.concat(req._routeWhitelists.req || []);
236
+ var responseWhitelist = options.responseWhitelist.concat(req._routeWhitelists.res || []);
232
237
 
233
238
  logData.res = res;
234
239
 
@@ -244,8 +249,8 @@ function logger(options) {
244
249
  logData.req = filterObject(req, requestWhitelist, options.requestFilter);
245
250
  logData.res = filterObject(res, responseWhitelist, options.responseFilter);
246
251
 
247
- bodyWhitelist = req._routeWhitelists.body || [];
248
- blacklist = _.union(bodyBlacklist, (req._routeBlacklists.body || []));
252
+ var bodyWhitelist = _.union(options.bodyWhitelist, (req._routeWhitelists.body || []));
253
+ var blacklist = _.union(options.bodyBlacklist, (req._routeBlacklists.body || []));
249
254
 
250
255
  var filteredBody = null;
251
256
 
@@ -287,7 +292,7 @@ function logger(options) {
287
292
 
288
293
  next();
289
294
  };
290
- }
295
+ };
291
296
 
292
297
  function ensureValidOptions(options) {
293
298
  if(!options) throw new Error("options are required by winston-middleware middleware");
@@ -300,14 +305,3 @@ function ensureValidLoggerOptions(options) {
300
305
  throw new Error("`ignoreRoute` winston-middleware option should be a function");
301
306
  }
302
307
  }
303
-
304
- module.exports.errorLogger = errorLogger;
305
- module.exports.logger = logger;
306
- module.exports.requestWhitelist = requestWhitelist;
307
- module.exports.bodyWhitelist = bodyWhitelist;
308
- module.exports.bodyBlacklist = bodyBlacklist;
309
- module.exports.responseWhitelist = responseWhitelist;
310
- module.exports.defaultRequestFilter = defaultRequestFilter;
311
- module.exports.defaultResponseFilter = defaultResponseFilter;
312
- module.exports.defaultSkip = defaultSkip;
313
- module.exports.ignoredRoutes = ignoredRoutes;
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "middleware",
18
18
  "colors"
19
19
  ],
20
- "version": "1.2.0",
20
+ "version": "1.3.0",
21
21
  "repository": {
22
22
  "type": "git",
23
23
  "url": "https://github.com/bithavoc/winston-middleware.git"
@@ -52,11 +52,12 @@
52
52
  "winston": "~1.0.0"
53
53
  },
54
54
  "devDependencies": {
55
- "blanket": "~1.1.6",
56
- "mocha": "~2.1.0",
57
- "node-mocks-http": "~1.2.3",
58
- "should": "~4.6.0",
59
- "travis-cov": "~0.2.5"
55
+ "blanket": "^1.2.2",
56
+ "mocha": "^2.4.5",
57
+ "node-mocks-http": "^1.5.1",
58
+ "promise": "^7.1.1",
59
+ "should": "^8.2.2",
60
+ "travis-cov": "^0.2.5"
60
61
  },
61
62
  "engines": {
62
63
  "node": ">=0.10.0"