-
Notifications
You must be signed in to change notification settings - Fork 7
/
log.js
67 lines (64 loc) · 2.41 KB
/
log.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
62
63
64
65
66
67
var enums = require('./enums'),
winston = require('winston');
exports.init = function (config) {
// adding log file and logging support.
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
handleExceptions: enums.productionMode
}),
new (winston.transports.File)({
filename: config.serverlogfile,
handleExceptions: enums.productionMode
})
]
}),
requestlog;
//had to give r an empty object.
console.r = {};
if (config.requestlogfile !== false) {
requestlog = new (winston.Logger)({
transports: [
new (winston.transports.File)({
filename: config.requestlogfile
})
]
});
// FIXME: Shouldn't be outputting messages into response
// because response may be JSON output.
console.r.info = function (req, statusCode, err) {
//requestlog.info((err === null ? '' : ('Code:' + err.code + ' Message:' + err.message)) + ' StatusCode:' + statusCode + ' User IP:' + req.ip + ' Request URL:' + req.url+' POST Data:' + req.url);
var error = (err === null ? null : {
code: err.code,
msg: err.message
}),
user_info = {
statusCode: statusCode,
IP: req.ip,
URL: req.url
};
//console.log( JSON.stringify(user_info).replace("'", r"\'"));
requestlog.info(JSON.stringify(error) + JSON.stringify(user_info));
};
console.r.error = function (req, statusCode, err) {
var error = (err === null ? null : {
code: err.code,
msg: err.message
}),
user_info = {
statusCode: statusCode,
IP: req.ip,
URL: req.url
};
requestlog.error(JSON.stringify(error) + JSON.stringify(user_info));
};
} else {
console.r.info = console.r.error = function () {
return null;
};
}
//give logger function to a global variable, to make sure other files can access it easily.
console.logger = logger;
//example of logging.levels of log: info warn error
//logger.log('info', 'Hello distributed log files!');
};