winston-middleware 0.2.1 → 0.2.2

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.
Files changed (4) hide show
  1. package/AUTHORS +1 -0
  2. package/Readme.md +26 -2
  3. package/index.js +23 -14
  4. package/package.json +15 -5
package/AUTHORS CHANGED
@@ -1,3 +1,4 @@
1
1
  Heapsource <npm@heapsource.com> (http://www.heapsource.com)
2
2
  Lars Jacob (http://jaclar.net)
3
3
  Jonathan Lomas (http://feedbackular.com)
4
+ Xavier Damman (http://xdamman.com)
package/Readme.md CHANGED
@@ -11,6 +11,28 @@
11
11
 
12
12
  winston-middleware provides middlewares for request and error logging of your express.js application. It uses 'whitelists' to select properties from the request and (new in 0.2.x) response objects.
13
13
 
14
+ To make use of winston-middleware, you need to add the following to your application:
15
+
16
+ In `package.json`:
17
+
18
+ ```
19
+ {
20
+ "dependencies": {
21
+ "...": "...",
22
+ "winston": "0.6.x",
23
+ "winston-middleware": "0.2.x",
24
+ "...": "..."
25
+ }
26
+ }
27
+ ```
28
+
29
+ In `server.js` (or wherever you need it):
30
+
31
+ ```
32
+ var winston = require('winston'),
33
+ expressWinston = require('winston-middleware');
34
+ ```
35
+
14
36
  ### Error Logging
15
37
 
16
38
  Use `expressWinston.errorLogger(options)` to create a middleware that log the errors of the pipeline.
@@ -48,7 +70,9 @@ Use `expressWinston.logger(options)` to create a middleware to log your HTTP req
48
70
  json: true,
49
71
  colorize: true
50
72
  })
51
- ]
73
+ ],
74
+ meta: true, // optional: control whether you want to log the meta data about the request (default to true)
75
+ msg: "HTTP {{req.method}} {{req.url}}" // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
52
76
  }));
53
77
  app.use(app.router); // notice how the router goes after the logger.
54
78
  ```
@@ -275,7 +299,7 @@ Also see AUTHORS file, add yourself if you are missing.
275
299
 
276
300
  ## MIT License
277
301
 
278
- Copyright (c) 2012-2013 Heapsource.com and Contributors - http://www.heapsource.com
302
+ Copyright (c) 2012-2014 Heapsource.com and Contributors - http://www.heapsource.com
279
303
 
280
304
  Permission is hereby granted, free of charge, to any person obtaining a copy
281
305
  of this software and associated documentation files (the "Software"), to deal
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2012 Firebase.co and Contributors - http://www.firebase.co
1
+ // Copyright (c) 2012-2014 Heapsource.com and Contributors - http://www.heapsource.com
2
2
  //
3
3
  // Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  // of this software and associated documentation files (the "Software"), to deal
@@ -20,6 +20,7 @@
20
20
  //
21
21
  var winston = require('winston');
22
22
  var util = require('util');
23
+ var _ = require('underscore');
23
24
 
24
25
  /**
25
26
  * A default list of properties in the request object that are allowed to be logged.
@@ -122,6 +123,7 @@ function logger(options) {
122
123
  options.requestFilter = options.requestFilter || defaultRequestFilter;
123
124
  options.responseFilter = options.responseFilter || defaultResponseFilter;
124
125
  options.level = options.level || "info";
126
+ options.msg = options.msg || "HTTP {{req.method}} {{req.url}}";
125
127
 
126
128
  return function (req, res, next) {
127
129
 
@@ -136,30 +138,37 @@ function logger(options) {
136
138
  // Manage to get information from the response too, just like Connect.logger does:
137
139
  var end = res.end;
138
140
  res.end = function(chunk, encoding) {
139
- var responseTime = (new Date) - req._startTime;
141
+ res.responseTime = (new Date) - req._startTime;
140
142
 
141
143
  res.end = end;
142
144
  res.end(chunk, encoding);
143
145
 
144
- var meta = {};
146
+ if(options.meta !== false) {
147
+ var meta = {};
145
148
 
146
- var bodyWhitelist;
149
+ var bodyWhitelist;
147
150
 
148
- requestWhitelist = requestWhitelist.concat(req._routeWhitelists.req || []);
149
- responseWhitelist = responseWhitelist.concat(req._routeWhitelists.res || []);
151
+ requestWhitelist = requestWhitelist.concat(req._routeWhitelists.req || []);
152
+ responseWhitelist = responseWhitelist.concat(req._routeWhitelists.res || []);
150
153
 
151
- meta.req = filterObject(req, requestWhitelist, options.requestFilter);
152
- meta.res = filterObject(res, responseWhitelist, options.responseFilter);
154
+ meta.req = filterObject(req, requestWhitelist, options.requestFilter);
155
+ meta.res = filterObject(res, responseWhitelist, options.responseFilter);
153
156
 
154
- bodyWhitelist = req._routeWhitelists.body || [];
157
+ bodyWhitelist = req._routeWhitelists.body || [];
155
158
 
156
- if (bodyWhitelist) {
157
- meta.req.body = filterObject(req.body, bodyWhitelist, options.requestFilter);
158
- };
159
+ if (bodyWhitelist) {
160
+ meta.req.body = filterObject(req.body, bodyWhitelist, options.requestFilter);
161
+ };
159
162
 
160
- meta.responseTime = responseTime;
163
+ meta.responseTime = res.responseTime;
164
+ }
161
165
 
162
- var msg = util.format("HTTP %s %s", req.method, req.url);
166
+ // Using mustache style templating
167
+ _.templateSettings = {
168
+ interpolate: /\{\{(.+?)\}\}/g
169
+ };
170
+ var template = _.template(options.msg);
171
+ var msg = template({req: req, res: res});
163
172
 
164
173
  // This is fire and forget, we don't want logging to hold up the request so don't wait for the callback
165
174
  for(var i = 0; i < options.transports.length; i++) {
package/package.json CHANGED
@@ -2,8 +2,17 @@
2
2
  "author": "Heapsource.com <npm@heapsource.com> (http://www.heapsource.com)",
3
3
  "name": "winston-middleware",
4
4
  "description": "express.js middleware for flatiron/winston",
5
- "keywords": ["winston", "flatiron", "logging", "express", "log", "error", "handler", "middleware"],
6
- "version": "0.2.1",
5
+ "keywords": [
6
+ "winston",
7
+ "flatiron",
8
+ "logging",
9
+ "express",
10
+ "log",
11
+ "error",
12
+ "handler",
13
+ "middleware"
14
+ ],
15
+ "version": "0.2.2",
7
16
  "repository": {
8
17
  "url": "https://github.com/heapsource/winston-middleware.git"
9
18
  },
@@ -12,13 +21,14 @@
12
21
  "test": "vows --spec"
13
22
  },
14
23
  "dependencies": {
15
- "winston": "0.6.x"
24
+ "winston": "0.6.x",
25
+ "underscore": "~1.5.2"
16
26
  },
17
27
  "devDependencies": {
18
- "vows": "0.6.x"
28
+ "vows": "0.7.x"
19
29
  },
20
30
  "engines": {
21
31
  "node": "*"
22
32
  },
23
- "license": "MIT"
33
+ "license": "MIT"
24
34
  }