This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
61 lines (50 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var morgan = require('morgan');
module.exports = function(sails) {
return {
/**
* Default configuration
*
* We do this in a function since the configuration key for
* the hook is itself configurable, so we can't just return
* an object.
*/
defaults: {
requestlogger: {
// Turn morgan logging on by default in development environment
// and off for production, using the 'dev' format
//see: https://github.com/expressjs/morgan#predefined-formats for more formats
format: 'dev',
inDevelopment: true,
inProduction: false
}
},
routes: {
before: {
'all /*': function addRequestLogging (req, res, next) {
// If the hook has been deactivated, just return
//Need to define requestlogger manually, since don't have acces to this.configKey
var loggerSettings = sails.config['requestlogger'];
var isProduction = process.env.NODE_ENV === 'production';
if ((isProduction && loggerSettings.inProduction === true) ||
(!isProduction && loggerSettings.inDevelopment === true)) {
var logger = morgan(loggerSettings.format);
logger(req, res, function (err) {
if (err) next(err)
next();
});
} else {
next();
}
}
}
},
/**
* Initialize the hook
* @param {Function} cb Callback for when we're done initializing
*/
initialize: function(cb) {
// Finally
cb();
}
};
};