winston-middleware 0.1.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 ADDED
@@ -0,0 +1,5 @@
1
+ language: node_js
2
+ node_js:
3
+ - 0.6
4
+ - 0.8
5
+ script: "npm test"
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Firebase.co - http://www.firebase.co
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/Readme.md ADDED
@@ -0,0 +1,240 @@
1
+ # winston-middleware
2
+ [![Build Status](https://secure.travis-ci.org/firebaseco/winston-middleware.png)](http://travis-ci.org/firebaseco/winston-middleware)
3
+
4
+ > [winston](https://github.com/flatiron/winston) middleware for express.js
5
+
6
+ ## Installation
7
+
8
+ npm install winston-middleware
9
+
10
+ ## Usage
11
+
12
+ winston-middleware provides middlewares for request and error logging of your express.js application.
13
+
14
+ ### Error Logging
15
+
16
+ Use `expressWinston.errorLogger(options)` to create a middleware that log the errors of the pipeline.
17
+
18
+ ``` js
19
+ app.use(app.router); // notice how the router goes first.
20
+ app.use(expressWinston.errorHandler({
21
+ transports: [
22
+ new winston.transports.Console({
23
+ json: true,
24
+ colorize: true
25
+ })
26
+ ]
27
+ }));
28
+ ```
29
+
30
+ The logger needs to be added AFTER the express router(`app.router)`) and BEFORE any of your custom error handlers(`express.handler`). Since winston-middleware will just log the errors and not __handle__ them, you can still use your custom error handler like `express.handler`, just be sure to put the logger before any of your handlers.
31
+
32
+ ### Request Logging
33
+
34
+ Use `expressWinston.logger(options)` to create a middleware to log your HTTP requests.
35
+
36
+ ``` js
37
+
38
+ app.use(expressWinston.logger({
39
+ transports: [
40
+ new winston.transports.Console({
41
+ json: true,
42
+ colorize: true
43
+ })
44
+ ]
45
+ }));
46
+ app.use(app.router); // notice how the router goes after the logger.
47
+ ```
48
+
49
+ ## Example
50
+
51
+ ``` js
52
+ var express = require('express');
53
+ var expressWinston = require('winston-middleware');
54
+ var winston = require('winston'); // for transports.Console
55
+ var app = module.exports = express.createServer();
56
+
57
+ app.use(express.bodyParser());
58
+ app.use(express.methodOverride());
59
+
60
+ // winston-middleware logger makes sense BEFORE the router.
61
+ app.use(expressWinston.logger({
62
+ transports: [
63
+ new winston.transports.Console({
64
+ json: true,
65
+ colorize: true
66
+ })
67
+ ]
68
+ }));
69
+
70
+ app.use(app.router);
71
+
72
+ // winston-middleware errorHandler makes sense AFTER the router.
73
+ app.use(expressWinston.errorHandler({
74
+ transports: [
75
+ new winston.transports.Console({
76
+ json: true,
77
+ colorize: true
78
+ })
79
+ ]
80
+ }));
81
+
82
+ // Optionally you can include your custom error handler after the logging.
83
+ app.use(express.errorHandler({
84
+ dumpExceptions: true,
85
+ showStack: true
86
+ }));
87
+
88
+ app.get('/error', function(req, res, next) {
89
+ // here we cause an error in the pipeline so we see winston-middleware in action.
90
+ return next(new Error("This is an error and it should be logged to the console"));
91
+ });
92
+
93
+ app.get('/', function(req, res, next) {
94
+ res.write('This is a normal request, it should be logged to the console too');
95
+ res.end();
96
+ });
97
+
98
+ app.listen(3000, function(){
99
+ console.log("winston-middleware demo listening on port %d in %s mode", app.address().port, app.settings.env);
100
+ });
101
+ ```
102
+
103
+ Browse `/` to see a regular HTTP logging like this:
104
+
105
+ {
106
+ "req": {
107
+ "httpVersion": "1.1",
108
+ "headers": {
109
+ "host": "localhost:3000",
110
+ "connection": "keep-alive",
111
+ "accept": "*/*",
112
+ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
113
+ "accept-encoding": "gzip,deflate,sdch",
114
+ "accept-language": "en-US,en;q=0.8,es-419;q=0.6,es;q=0.4",
115
+ "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
116
+ "cookie": "connect.sid=nGspCCSzH1qxwNTWYAoexI23.seE%2B6Whmcwd"
117
+ },
118
+ "url": "/",
119
+ "method": "GET",
120
+ "originalUrl": "/",
121
+ "query": {}
122
+ },
123
+ "level": "info",
124
+ "message": "HTTP GET /favicon.ico"
125
+ }
126
+
127
+ Browse `/error` will show you how winston-middleware handles and logs the errors in the express pipeline like this:
128
+
129
+ {
130
+ "date": "Thu Jul 19 2012 23:39:44 GMT-0500 (COT)",
131
+ "process": {
132
+ "pid": 35719,
133
+ "uid": 501,
134
+ "gid": 20,
135
+ "cwd": "/Users/thepumpkin/Projects/testExpressWinston",
136
+ "execPath": "/usr/local/bin/node",
137
+ "version": "v0.6.18",
138
+ "argv": [
139
+ "node",
140
+ "/Users/thepumpkin/Projects/testExpressWinston/app.js"
141
+ ],
142
+ "memoryUsage": {
143
+ "rss": 14749696,
144
+ "heapTotal": 7033664,
145
+ "heapUsed": 5213280
146
+ }
147
+ },
148
+ "os": {
149
+ "loadavg": [
150
+ 1.95068359375,
151
+ 1.5166015625,
152
+ 1.38671875
153
+ ],
154
+ "uptime": 498086
155
+ },
156
+ "trace": [
157
+ ...,
158
+ {
159
+ "column": 3,
160
+ "file": "Object].log (/Users/thepumpkin/Projects/testExpressWinston/node_modules/winston/lib/winston/transports/console.js",
161
+ "function": "[object",
162
+ "line": 87,
163
+ "method": null,
164
+ "native": false
165
+ }
166
+ ],
167
+ "stack": [
168
+ "Error: This is an error and it should be logged to the console",
169
+ " at /Users/thepumpkin/Projects/testExpressWinston/app.js:39:15",
170
+ " at callbacks (/Users/thepumpkin/Projects/testExpressWinston/node_modules/express/lib/router/index.js:272:11)",
171
+ " at param (/Users/thepumpkin/Projects/testExpressWinston/node_modules/express/lib/router/index.js:246:11)",
172
+ " at pass (/Users/thepumpkin/Projects/testExpressWinston/node_modules/express/lib/router/index.js:253:5)",
173
+ " at Router._dispatch (/Users/thepumpkin/Projects/testExpressWinston/node_modules/express/lib/router/index.js:280:4)",
174
+ " at Object.handle (/Users/thepumpkin/Projects/testExpressWinston/node_modules/express/lib/router/index.js:45:10)",
175
+ " at next (/Users/thepumpkin/Projects/testExpressWinston/node_modules/express/node_modules/connect/lib/http.js:204:15)",
176
+ " at done (/Users/thepumpkin/Dropbox/Projects/winston-middleware/index.js:91:14)",
177
+ " at /Users/thepumpkin/Dropbox/Projects/winston-middleware/node_modules/async/lib/async.js:94:25",
178
+ " at [object Object].log (/Users/thepumpkin/Projects/testExpressWinston/node_modules/winston/lib/winston/transports/console.js:87:3)"
179
+ ],
180
+ "req": {
181
+ "httpVersion": "1.1",
182
+ "headers": {
183
+ "host": "localhost:3000",
184
+ "connection": "keep-alive",
185
+ "cache-control": "max-age=0",
186
+ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
187
+ "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
188
+ "accept-encoding": "gzip,deflate,sdch",
189
+ "accept-language": "en-US,en;q=0.8,es-419;q=0.6,es;q=0.4",
190
+ "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
191
+ "cookie": "connect.sid=nGspCCSzH1qxwNTWYAoexI23.seE%2B6WhmcwdzFEjqhMDuIIl3mAUY7dT4vn%2BkWvRPhZc"
192
+ },
193
+ "url": "/error",
194
+ "method": "GET",
195
+ "originalUrl": "/error",
196
+ "query": {}
197
+ },
198
+ "level": "error",
199
+ "message": "middlewareError"
200
+ }
201
+
202
+ ## Tests
203
+
204
+ npm test
205
+
206
+ ## Issues and Collaboration
207
+
208
+ * Add option to set the log level.
209
+ * Add support for filtering of __req.body__. At this moment `body` is not included in the logging because it can contain sensitive fields like 'password' or 'password_confirmation'.
210
+ * Implement a chain of requestFilters. Currently only one requestFilter is allowed in the options.
211
+
212
+ We are accepting pull-request for this features.
213
+
214
+ If you ran into any problems, please use the project [Issues section](https://github.com/firebaseco/winston-middleware/issues) to search or post any bug.
215
+
216
+ ## Contributors
217
+
218
+ * Johan (author). Email: *johan@firebase.co*
219
+
220
+ ## MIT License
221
+
222
+ Copyright (c) 2012 Firebase.co and Contributors - http://www.firebase.co
223
+
224
+ Permission is hereby granted, free of charge, to any person obtaining a copy
225
+ of this software and associated documentation files (the "Software"), to deal
226
+ in the Software without restriction, including without limitation the rights
227
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
228
+ copies of the Software, and to permit persons to whom the Software is
229
+ furnished to do so, subject to the following conditions:
230
+
231
+ The above copyright notice and this permission notice shall be included in
232
+ all copies or substantial portions of the Software.
233
+
234
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
235
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
236
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
237
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
238
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
239
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
240
+ THE SOFTWARE.
package/index.js ADDED
@@ -0,0 +1,104 @@
1
+ // Copyright (c) 2012 Firebase.co and Contributors - http://www.firebase.co
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+ //
21
+
22
+ var winston = require('winston');
23
+ var async = require('async');
24
+ var util = require('util');
25
+
26
+ // default list of properties in the request object that are allowed to be logged.
27
+ // these properties will be safely included in the meta of the log.
28
+ // 'body' is not included in this list because it can contains passwords and stuff that are sensitive for logging.
29
+ // TODO: Include 'body' and get the defaultRequestFilter to filter the inner properties like 'password' or 'password_confirmation', etc. Pull requests anyone?
30
+ var requestWhitelist = ['url', 'headers', 'method', 'httpVersion', 'originalUrl', 'query'];
31
+
32
+ // default function to filter the properties of the req object.
33
+ var defaultRequestFilter = function(req, propName) {
34
+ return req[propName];
35
+ };
36
+
37
+ function filterRequest(originalReq, initialFilter) {
38
+ var req = {};
39
+ Object.keys(originalReq).forEach(function(propName) {
40
+ // if the property is not in the whitelist, we return undefined so is not logged as part of the meta.
41
+ if(requestWhitelist.indexOf(propName) == -1) return;
42
+ var value = initialFilter(originalReq, propName);
43
+ if(typeof(value) !== 'undefined') {
44
+ req[propName] = value;
45
+ }
46
+ });
47
+ return req;
48
+ };
49
+
50
+ //
51
+ // ### function errorLogger(options)
52
+ // #### @options {Object} options to initialize the middleware.
53
+ //
54
+ function errorLogger(options) {
55
+ if(!options) throw new Error("options are required by winston-middleware middleware");
56
+ if(!options.transports || !(options.transports.length > 0)) throw new Error("transports are required by winston-middleware middleware");
57
+ options.requestFilter = options.requestFilter || defaultRequestFilter;
58
+ return function(err, req, res, next) {
59
+ // let winston gather all the error data.
60
+ var exceptionMeta = winston.exception.getAllInfo(err);
61
+ exceptionMeta.req = filterRequest(req, options.requestFilter);
62
+
63
+ function logOnTransport(transport, nextTransport) {
64
+ return transport.logException('middlewareError', exceptionMeta, nextTransport);
65
+ };
66
+
67
+ function done() {
68
+ return next(err);
69
+ };
70
+ // iterate all the transports
71
+ async.forEach(options.transports, logOnTransport, done);
72
+ };
73
+ };
74
+
75
+ //
76
+ // ### function logger(options)
77
+ // #### @options {Object} options to initialize the middleware.
78
+ //
79
+ function logger(options) {
80
+ if(!options) throw new Error("options are required by winston-middleware middleware");
81
+ if(!options.transports || !(options.transports.length > 0)) throw new Error("transports are required by winston-middleware middleware");
82
+ options.requestFilter = options.requestFilter || defaultRequestFilter;
83
+ return function(req, res, next) {
84
+ var meta = {
85
+ req: filterRequest(req, options.requestFilter)
86
+ };
87
+ var msg = util.format("HTTP %s %s", req.method, req.url);
88
+
89
+ function logOnTransport(transport, nextTransport) {
90
+ return transport.log('info', msg, meta, nextTransport);
91
+ };
92
+
93
+ function done() {
94
+ return next();
95
+ };
96
+ // iterate all the transports
97
+ async.forEach(options.transports, logOnTransport, done);
98
+ };
99
+ };
100
+
101
+ module.exports.errorLogger = errorLogger;
102
+ module.exports.logger = logger;
103
+ module.exports.requestWhitelist = requestWhitelist;
104
+ module.exports.defaultRequestFilter = defaultRequestFilter;
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "author": "Firebase.co <npm@firebase.co> (http://www.firebase.co/)",
3
+ "name": "winston-middleware",
4
+ "description": "express.js middleware for flatiron/winston",
5
+ "keywords": ["winston", "flatiron", "logging", "express", "log", "error", "handler", "middleware"],
6
+ "version": "0.1.0",
7
+ "repository": {
8
+ "url": "https://github.com/firebaseco/winston-middleware.git"
9
+ },
10
+ "main": "index.js",
11
+ "scripts": {
12
+ "test": "node test/test.js"
13
+ },
14
+ "dependencies": {
15
+ "winston": "0.6.x",
16
+ "async": "0.1.x"
17
+ },
18
+ "devDependencies": {
19
+ "vows": "0.6.x"
20
+ },
21
+ "engines": {
22
+ "node": "0.6.x"
23
+ },
24
+ "licenses" : [
25
+ {
26
+ "type" : "MIT",
27
+ "url" : "https://github.com/firebaseco/winston-middleware/blob/master/LICENSE"
28
+ }
29
+ ]
30
+ }
package/test/test.js ADDED
@@ -0,0 +1,243 @@
1
+ var vows = require('vows');
2
+ var assert = require('assert');
3
+ var winston = require('winston');
4
+ var expressWinston = require('../index');
5
+ var util = require('util');
6
+ var events = require('events');
7
+
8
+ var MockTransport = function(options) {
9
+ winston.Transport.call(this, options);
10
+ };
11
+ util.inherits(MockTransport, winston.Transport);
12
+
13
+ vows.describe("exports").addBatch({
14
+ "When I check the exported properties": {
15
+ topic: function() {
16
+ return expressWinston;
17
+ },
18
+ "an array with all the properties whilelist in the req object": function(exports) {
19
+ assert.isArray(exports.requestWhitelist);
20
+ },
21
+ "and the factory should contain a default request filter function": function(exports) {
22
+ assert.isFunction(exports.defaultRequestFilter);
23
+ },
24
+ "it should export a function for the creation of error loggers middlewares": function(exports) {
25
+ assert.isFunction(exports.errorLogger);
26
+ },
27
+ "it should export a function for the creation of request logger middlewares": function(exports) {
28
+ assert.isFunction(exports.logger);
29
+ }
30
+ }
31
+ }).export(module);
32
+
33
+ vows.describe("errorLogger").addBatch({
34
+ "when I run the middleware factory": {
35
+ topic: function() {
36
+ return expressWinston.errorLogger;
37
+ },
38
+ "without options": {
39
+ "an error should be raised": function(factory) {
40
+ assert.throws(function() {
41
+ factory();
42
+ }, Error);
43
+ }
44
+ },
45
+ "without any transport specified": {
46
+ "an error should be raised": function(factory) {
47
+ assert.throws(function() {
48
+ factory({});
49
+ }, Error);
50
+ }
51
+ },
52
+ "with an empty list of transports": {
53
+ "an error should be raised": function(factory) {
54
+ assert.throws(function() {
55
+ factory({transports:[]});
56
+ }, Error);
57
+ }
58
+ },
59
+ "with proper options": {
60
+ "the result should be a function with four arguments that fit err, req, res, next": function (factory) {
61
+ var middleware = factory({
62
+ transports: [
63
+ new MockTransport({
64
+
65
+ })
66
+ ]
67
+ });
68
+ assert.equal(middleware.length, 4);
69
+ }
70
+ }
71
+ },
72
+ "When the winston-middleware middleware encounter an error in the pipeline": {
73
+ topic: function() {
74
+ var factory = expressWinston.errorLogger;
75
+ var callback = this.callback;
76
+ var req = {
77
+ url: "/hello?val=1",
78
+ headers: {
79
+ 'header-1': 'value 1'
80
+ },
81
+ method: 'GET',
82
+ query: {
83
+ val: '1'
84
+ },
85
+ originalUrl: "/hello?val=1",
86
+ params: {
87
+ id: 20
88
+ },
89
+ filteredProperty: "value that should not be logged"
90
+ };
91
+ var res = {
92
+
93
+ };
94
+ var originalError = new Error("This is the Error");
95
+ var test = {
96
+ req: req,
97
+ res: res,
98
+ log: {},
99
+ originalError: originalError,
100
+ pipelineError: null
101
+ };
102
+ var next = function(pipelineError) {
103
+ test.pipelineError = pipelineError;
104
+ return callback(null, test);
105
+ };
106
+
107
+ var transport = new MockTransport({});
108
+ transport.log = function(level, msg, meta, cb) {
109
+ test.transportInvoked = true;
110
+ test.log.level = level;
111
+ test.log.msg = msg;
112
+ test.log.meta = meta;
113
+ this.emit('logged');
114
+ return cb();
115
+ };
116
+ var middleware = factory({
117
+ transports: [transport]
118
+ });
119
+ middleware(originalError, req, res, next);
120
+ }
121
+ , "then the transport should be invoked": function(result){
122
+ assert.isTrue(result.transportInvoked);
123
+ }
124
+ , "the error level should be error": function(result){
125
+ assert.equal(result.log.level, "error");
126
+ }
127
+ , "the msg should be middlewareError": function(result){
128
+ assert.equal(result.log.msg, "middlewareError");
129
+ }
130
+ , "the meta should contain a filtered request": function(result){
131
+ assert.isTrue(!!result.log.meta.req, "req should be defined in meta");
132
+ assert.isNotNull(result.log.meta.req);
133
+ assert.equal(result.log.meta.req.method, "GET");
134
+ assert.deepEqual(result.log.meta.req.query, {
135
+ val: '1'
136
+ });
137
+ assert.isUndefined(result.log.meta.req.filteredProperty);
138
+ },
139
+ "the winston-middleware middleware should not swallow the pipeline error": function(result) {
140
+ assert.isNotNull(result.pipelineError);
141
+ assert.strictEqual(result.pipelineError, result.originalError);
142
+ }
143
+ }
144
+ }).export(module);
145
+
146
+ vows.describe("logger").addBatch({
147
+ "when I run the middleware factory": {
148
+ topic: function() {
149
+ return expressWinston.logger;
150
+ },
151
+ "without options": {
152
+ "an error should be raised": function(factory) {
153
+ assert.throws(function() {
154
+ factory();
155
+ }, Error);
156
+ }
157
+ },
158
+ "without any transport specified": {
159
+ "an error should be raised": function(factory) {
160
+ assert.throws(function() {
161
+ factory({});
162
+ }, Error);
163
+ }
164
+ },
165
+ "with an empty list of transports": {
166
+ "an error should be raised": function(factory) {
167
+ assert.throws(function() {
168
+ factory({transports:[]});
169
+ }, Error);
170
+ }
171
+ },
172
+ "with proper options": {
173
+ "the result should be a function with three arguments that fit req, res, next": function (factory) {
174
+ var middleware = factory({
175
+ transports: [
176
+ new MockTransport({
177
+
178
+ })
179
+ ]
180
+ });
181
+ assert.equal(middleware.length, 3);
182
+ }
183
+ }
184
+ },
185
+ "When the winston-middleware middleware is invoked in pipeline": {
186
+ topic: function() {
187
+ var factory = expressWinston.logger;
188
+ var callback = this.callback;
189
+ var req = {
190
+ url: "/hello?val=1",
191
+ headers: {
192
+ 'header-1': 'value 1'
193
+ },
194
+ method: 'GET',
195
+ query: {
196
+ val: '2'
197
+ },
198
+ originalUrl: "/hello?val=2",
199
+ params: {
200
+ id: 20
201
+ },
202
+ filteredProperty: "value that should not be logged"
203
+ };
204
+ var res = {
205
+
206
+ };
207
+ var test = {
208
+ req: req,
209
+ res: res,
210
+ log: {}
211
+ };
212
+ var next = function() {
213
+ return callback(null, test);
214
+ };
215
+
216
+ var transport = new MockTransport({});
217
+ transport.log = function(level, msg, meta, cb) {
218
+ test.transportInvoked = true;
219
+ test.log.level = level;
220
+ test.log.msg = msg;
221
+ test.log.meta = meta;
222
+ this.emit('logged');
223
+ return cb();
224
+ };
225
+ var middleware = factory({
226
+ transports: [transport]
227
+ });
228
+ middleware(req, res, next);
229
+ }
230
+ , "then the transport should be invoked": function(result){
231
+ assert.isTrue(result.transportInvoked);
232
+ }
233
+ , "the meta should contain a filtered request": function(result){
234
+ assert.isTrue(!!result.log.meta.req, "req should be defined in meta");
235
+ assert.isNotNull(result.log.meta.req);
236
+ assert.equal(result.log.meta.req.method, "GET");
237
+ assert.deepEqual(result.log.meta.req.query, {
238
+ val: '2'
239
+ });
240
+ assert.isUndefined(result.log.meta.req.filteredProperty);
241
+ }
242
+ }
243
+ }).export(module);