request-logger.js
2.1 KB
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
const winston = require('winston');
const fs = require('fs');
const moment = require('moment');
let logDir = 'logs'; // directory path you want to set
if ( !fs.existsSync( logDir ) ) {
// Create the directory if it does not exist
fs.mkdirSync( logDir );
}
function RequestLogger() {
this.fileTransport = new winston.transports.File({
timestamp: function() {
return moment().format('YYYY-MM-DDTHH:mm:ss.SSS');
},
json: false,
filename: 'logs/app.log',
maxsize: 10240000,
maxFiles: 99,
tailable: true
});
this.logger = winston.createLogger({
transports: [this.fileTransport]
});
this.logPrefix = function(session) {
let userId = '';
let sessionId = session.id || '';
let reqId = session.reqId || '';
let msg = `phoenix-partner - :: ## ${userId} - ${sessionId} - ${reqId} ## `;
return msg;
};
}
RequestLogger.prototype.logRequest = function(reqMethod, reqUrl, session, data, header) {
header = JSON.stringify(header);
data = JSON.stringify(data);
let msg = this.logPrefix(session) + `BE Send HTTP ${reqMethod} ${reqUrl} request-header: ${header} request-body: ${data}`;
this.logger.info(msg);
};
RequestLogger.prototype.logResponse = function(reqMethod, reqUrl, session, data, header) {
header = JSON.stringify(header);
data = JSON.stringify(data);
let msg = this.logPrefix(session) + `BE Receive HTTP ${reqMethod} ${reqUrl} response-header: ${header} response-body: ${data}`;
this.logger.info(msg);
};
RequestLogger.prototype.logSqlQuery = function(session, data) {
data = JSON.stringify(data);
let msg = this.logPrefix(session) + `BE Send SQL Query: ${data}`;
this.logger.info(msg);
};
RequestLogger.prototype.logSqlResult = function(session, data) {
data = JSON.stringify(data);
let msg = this.logPrefix(session) + `BE Receive SQL Result: ${data}`;
this.logger.info(msg);
};
RequestLogger.prototype.debug = function(msg) {
msg = this.logPrefix(session) + ' ' + msg;
this.logger.debug(msg);
};
module.exports = new RequestLogger();