-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
275 lines (256 loc) · 9.45 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Add this polyfill at the top of your main file
// Partial Fix of https://github.com/winstonjs/winston-transport/issues/247
if (typeof globalThis === 'undefined') {
global.globalThis = global;
}
const { winstonLoggerClient } = require('./winstonClient');
const httpContext = require('express-http-context');
const uuid = require('uuid');
const morgan = require('morgan');
const Joi = require('joi');
const util = require('util');
// const morganFormat = "combined";
const apiLogLevel = "INFO";
const defaultLevel = "info";
const IS_EXPRESS = true;
const validLogLevels = ["crawlerror", "crawlui", "crawlinfo", "error", "warn", "info", "http", "verbose", "debug", "silly", "usersessionactivity", "crawlalert", "sualert"];
let logger = null;
let serviceName = null;
let expressApp = null;
let apmClient = null;
let LoggerObject = function (level, message, args, filename, isExpress) {
this.level = level;
this.label = serviceName;
this.message = message;
this.args = args;
this.filename = filename;
// this.functionName = functionName;
this.isExpress = isExpress;
if (logger) {
logger.log(this);
} else {
throw new Error("Not able to initialize the logger object.");
}
}
/**
* @author Ankur Mahajan
* @class LoggerBuilder
* @summary This is a LoggerBuilder, builder of logger object.
* @param {string} filename filename with path.
*/
const LoggerBuilder = function (filename, isExpress) {
return {
info: function (...args) {
this.level = "info";
this.message = util.format(...args);
return this.build();
},
debug: function (...args) {
this.level = "debug";
this.message = util.format(...args);
return this.build();
},
error: function (...args) {
this.level = "error";
this.message = util.format(...args);
if (apmClient) apmClient.captureError(this.message);
return this.build();
},
warn: function (...args) {
this.level = "warn";
this.message = util.format(...args);
return this.build();
},
crawlError: function (...args) {
this.level = "crawlerror";
this.message = util.format(...args);
if (apmClient) apmClient.captureError(this.message);
return this.build();
},
crawlInfo: function (...args) {
this.level = "crawlinfo";
this.message = util.format(...args);
return this.build();
},
crawlUi: function (...args) {
this.level = "crawlui";
this.message = util.format(...args);
return this.build();
},
userSessionActivity: function (...args) {
this.level = "usersessionactivity";
this.message = util.format(...args);
return this.build();
},
crawlAlert: function (...args) {
this.level = "crawlalert";
this.message = util.format(...args);
return this.build();
},
suAlert: function (...args) {
this.level = "sualert";
this.message = util.format(...args);
return this.build();
},
build: function () {
let customLogger = new LoggerObject(this.level, this.message, this.args, filename, isExpress);
return customLogger;
}
};
};
/**
* @author Ankur Mahajan
* @class LoggerFactory
* @summary This is a LoggerFactory, an entry point to the logger module.
* @param {string} service service name.
* @param {string} level log level i.e info, error, warn and debug.
* @param {object} options file and rotation object.
* @returns {LoggerBuilder} LoggerBuilder
*/
const LoggerFactory = function (service, level, options) {
if (!service) {
throw new Error("Service name is required.");
}
if (level && !validLogLevels.includes(level)) {
throw new Error(`Invalid log level: ${level}. Supported levels are ${validLogLevels}`);
}
serviceName = service;
this.level = level || defaultLevel;
this.options = options;
// initialize the winson logger.
logger = winstonLoggerClient(level, options);
return {
getLogger: (filename) => {
filename = filename.replace(/^.*[\\\/]/, '');
return new LoggerBuilder(filename);
}
}
}
/**
* @author Ankur Mahajan
* @class ExpressLoggerFactory
* @summary This is an ExpressLoggerFactory, configuration point for Motifer's express logger.
* @param {string} service service name
* @param {string} level log level i.e info, error, warn and debug.
* @param {Object} express instance of express server.
* @param {object} options file and rotation object.
* @returns {LoggerBuilder} LoggerBuilder
*/
const ExpressLoggerFactory = function (service, level, express = null, options) {
if (!service) {
throw new Error("Service name is required.");
}
if (level && !validLogLevels.includes(level)) {
throw new Error(`Invalid log level: ${level}. Supported levels are ${validLogLevels}`);
}
serviceName = service;
this.options = options;
expressApp = express;
this.level = level || defaultLevel;
if (express) {
express.use(httpContext.middleware);
// Run the context for each request. Assign a unique identifier to each request
express.use((req, res, next) => {
// Bug fix for requestId gets undefind in some contexts.
if (!httpContext.ns.active) {
let context = httpContext.ns.createContext();
httpContext.ns.context = context;
httpContext.ns.active = context;
}
httpContext.ns.bindEmitter(req);
httpContext.ns.bindEmitter(res);
// Enhancement - request id chaining across the microservices.
let requestId = req.headers['request-id'];
if (!requestId) {
requestId = uuid.v4();
req.headers['request-id'] = requestId;
}
httpContext.set('requestId', requestId);
req.id = requestId;
// Request Logging
let dto = {
message: `${new Date().toISOString()} [request] [${requestId}] [${serviceName}] [${apiLogLevel}] [${req.method}] [${req.ip}] [${req.originalUrl}] [${req.body ? JSON.stringify(req.body) : null}]`,
api: true
};
logger.info(dto);
next();
});
// Morgan to track the response.
express.use(morgan(`:date[iso] [response] [:id] [${serviceName}] [${apiLogLevel}] [:method] [:remote-addr] [:url] [:status] [:res[content-length]] [:response-time] [:user-agent]`, { "stream": loggerStream }));
}
// initialize the winson logger.
logger = winstonLoggerClient(level, options);
// }
return {
getLogger: (filename) => {
filename = filename.replace(/^.*[\\\/]/, '');
return new LoggerBuilder(filename, true);
}
}
}
/**
* @author Mohan Rana
* @class ApmFactory
* @summary This is an ApmFactory, used to integrate with elastic APM. Need to use this factory at very first line in the application.
* @param configObject.serviceName Your application name.
* @param configObject.apmServerUrl APM server url.
* @param configObject.secretToken APM secret token.
* @param configObject.logLevel APM log level, default value 'error'.
* @param configObject.environment Your application instance, default value 'production'.
*/
const ApmFactory = function (configObject) {
const isValid = validateParameters(configObject);
if (isValid.error) throw new Error(isValid.error.message);
const parameters = isValid.value;
apmClient = require('elastic-apm-node').start({
serviceName: parameters.serviceName,
serverUrl: parameters.apmServerUrl,
secretToken: parameters.secretToken,
logLevel: parameters.logLevel || 'error',
environment: parameters.environment || 'production',
transactionIgnoreUrls: parameters.transactionIgnoreUrls || [],
usePathAsTransactionName: false
});
return apmClient;
}
const validateParameters = (options) => {
const schema = Joi.object().keys({
serviceName: Joi.string().required(),
apmServerUrl: Joi.string().uri().required(),
secretToken: Joi.string().required(),
transactionIgnoreUrls: Joi.array()
}).unknown(true);
const result = schema.validate(options);
return result;
}
// Custom request Id token for Morgan.
morgan.token('id', function getId(req) {
return req.id
});
const loggerStream = {
write: (message) => {
message = message.substring(0, message.lastIndexOf('\n'));
let dto = { message, api: true };
logger.info(dto);
}
}
class Logger {
/**
* @author Ankur Mahajan
* @class Logger
* @summary This is a actual logger object that prints the logs in the console and file appenders.
* @param {string} filename Filename of the javascript file.
* @param {boolean} isExpress Boolean if using this with express js, default is true.
* @returns {LoggerBuilder} LoggerBuilder.
*/
static getLogger(filename, isExpress) {
filename = filename.replace(/^.*[\\\/]/, '');
return new LoggerBuilder(filename, (isExpress != undefined && typeof isExpress === "boolean") ? isExpress : IS_EXPRESS);
}
}
module.exports = {
LoggerFactory,
ExpressLoggerFactory,
Logger,
ApmFactory
}