sasai-common-utils 1.0.47 → 1.0.49
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/index.d.ts +88 -88
- package/package.json +30 -30
- package/src/features/axios-logger/axiosLogger.js +85 -85
- package/src/features/axios-logger/index.js +5 -5
- package/src/features/express-logger/expressLogger.js +46 -46
- package/src/features/express-logger/index.js +5 -5
- package/src/features/logger/config.js +6 -6
- package/src/features/logger/constants.js +36 -36
- package/src/features/logger/index.js +206 -177
- package/src/features/logger/logWorker.js +119 -119
- package/src/features/logger/redact.js +61 -61
- package/src/features/logger/winstonLogger.js +186 -186
- package/src/index.js +36 -36
- package/src/utils/index.js +11 -11
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
/** This file will be used to redact the sensitive informations to be logged */
|
|
2
|
-
|
|
3
|
-
const traverse = require("traverse");
|
|
4
|
-
const { klona } = require("klona/full");
|
|
5
|
-
|
|
6
|
-
const defautSensitiveKeys = [/cookie/i, /passw(or)?d/i, /^pw$/, /^pass$/i, /secret/i, /api[-._]?key/i, /token/i, /authorization/i, /otp/i, /^pin$/i ];
|
|
7
|
-
let SENSITIVE_KEYS;
|
|
8
|
-
function isSensitiveKey(keyStr) {
|
|
9
|
-
if (!keyStr) return false;
|
|
10
|
-
// Exception for "tokenDetails"
|
|
11
|
-
if (/^tokenDetails$/i.test(keyStr)) {
|
|
12
|
-
return false; // Do not redact this key
|
|
13
|
-
}
|
|
14
|
-
return SENSITIVE_KEYS.some(regex => regex.test(keyStr));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function redactNestedFields(obj) {
|
|
18
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
19
|
-
if (isSensitiveKey(key)) {
|
|
20
|
-
obj[key] = "[REDACTED]";
|
|
21
|
-
} else if (typeof value === "object" && value !== null) {
|
|
22
|
-
redactNestedFields(value); // Recursively process nested objects
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function redactObject(obj) {
|
|
28
|
-
traverse(obj).forEach(function redactor() {
|
|
29
|
-
if (isSensitiveKey(this.key )) {
|
|
30
|
-
this.update("[REDACTED]");
|
|
31
|
-
}
|
|
32
|
-
if (this.key === "message" && typeof this.node === "string") {
|
|
33
|
-
let parsedMessage
|
|
34
|
-
try {
|
|
35
|
-
parsedMessage = JSON.parse(this?.node);
|
|
36
|
-
redactNestedFields(parsedMessage);
|
|
37
|
-
} catch (error) {
|
|
38
|
-
parsedMessage = {};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
this.update(JSON.stringify(parsedMessage)); // Update with the redacted string
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const redactInformation=(obj, {sensitiveKeys})=> {
|
|
47
|
-
const regexArray = JSON.parse(sensitiveKeys).map((key) => {
|
|
48
|
-
const parts = key.match(/^\/(.*?)\/([gimsuy]*)$/);
|
|
49
|
-
return parts ? new RegExp(parts[1], parts[2]) : new RegExp(key); // Handle conversion
|
|
50
|
-
});
|
|
51
|
-
SENSITIVE_KEYS = regexArray || defautSensitiveKeys;
|
|
52
|
-
const copy = klona(obj); // Making a deep copy to prevent side effects
|
|
53
|
-
redactObject(copy);
|
|
54
|
-
|
|
55
|
-
const splat = copy[Symbol.for("splat")];
|
|
56
|
-
redactObject(splat); // Specifically redact splat Symbol
|
|
57
|
-
|
|
58
|
-
return copy;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
module.exports = { redactInformation }
|
|
1
|
+
/** This file will be used to redact the sensitive informations to be logged */
|
|
2
|
+
|
|
3
|
+
const traverse = require("traverse");
|
|
4
|
+
const { klona } = require("klona/full");
|
|
5
|
+
|
|
6
|
+
const defautSensitiveKeys = [/cookie/i, /passw(or)?d/i, /^pw$/, /^pass$/i, /secret/i, /api[-._]?key/i, /token/i, /authorization/i, /otp/i, /^pin$/i ];
|
|
7
|
+
let SENSITIVE_KEYS;
|
|
8
|
+
function isSensitiveKey(keyStr) {
|
|
9
|
+
if (!keyStr) return false;
|
|
10
|
+
// Exception for "tokenDetails"
|
|
11
|
+
if (/^tokenDetails$/i.test(keyStr)) {
|
|
12
|
+
return false; // Do not redact this key
|
|
13
|
+
}
|
|
14
|
+
return SENSITIVE_KEYS.some(regex => regex.test(keyStr));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function redactNestedFields(obj) {
|
|
18
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
19
|
+
if (isSensitiveKey(key)) {
|
|
20
|
+
obj[key] = "[REDACTED]";
|
|
21
|
+
} else if (typeof value === "object" && value !== null) {
|
|
22
|
+
redactNestedFields(value); // Recursively process nested objects
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function redactObject(obj) {
|
|
28
|
+
traverse(obj).forEach(function redactor() {
|
|
29
|
+
if (isSensitiveKey(this.key )) {
|
|
30
|
+
this.update("[REDACTED]");
|
|
31
|
+
}
|
|
32
|
+
if (this.key === "message" && typeof this.node === "string") {
|
|
33
|
+
let parsedMessage
|
|
34
|
+
try {
|
|
35
|
+
parsedMessage = JSON.parse(this?.node);
|
|
36
|
+
redactNestedFields(parsedMessage);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
parsedMessage = {};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.update(JSON.stringify(parsedMessage)); // Update with the redacted string
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const redactInformation=(obj, {sensitiveKeys})=> {
|
|
47
|
+
const regexArray = JSON.parse(sensitiveKeys).map((key) => {
|
|
48
|
+
const parts = key.match(/^\/(.*?)\/([gimsuy]*)$/);
|
|
49
|
+
return parts ? new RegExp(parts[1], parts[2]) : new RegExp(key); // Handle conversion
|
|
50
|
+
});
|
|
51
|
+
SENSITIVE_KEYS = regexArray || defautSensitiveKeys;
|
|
52
|
+
const copy = klona(obj); // Making a deep copy to prevent side effects
|
|
53
|
+
redactObject(copy);
|
|
54
|
+
|
|
55
|
+
const splat = copy[Symbol.for("splat")];
|
|
56
|
+
redactObject(splat); // Specifically redact splat Symbol
|
|
57
|
+
|
|
58
|
+
return copy;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { redactInformation }
|
|
@@ -1,187 +1,187 @@
|
|
|
1
|
-
// const winston = require("winston");
|
|
2
|
-
// const { LOG_LEVELS, DEBUG_MODES, HTTP_STATUS_CODES } = require("./constants");
|
|
3
|
-
// const { ecsStringify } = require("@elastic/ecs-winston-format");
|
|
4
|
-
// const { redactInformation } = require("./redact");
|
|
5
|
-
// const jwt = require("jsonwebtoken");
|
|
6
|
-
|
|
7
|
-
// let contextProvider = () => ({});
|
|
8
|
-
|
|
9
|
-
// function setContextProvider(provider) {
|
|
10
|
-
// contextProvider = provider;
|
|
11
|
-
// }
|
|
12
|
-
|
|
13
|
-
// const logFormat = winston.format.combine(
|
|
14
|
-
// winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
|
15
|
-
// winston.format((info) => redactInformation(info, {sensitiveKeys: globalConfig?.SENSITIVE_KEYS}))(), // Redact sensitive data
|
|
16
|
-
// winston.format.errors({ stack: true }),
|
|
17
|
-
// winston.format.json(),
|
|
18
|
-
// ecsStringify(),
|
|
19
|
-
// winston.format.printf((info) => {
|
|
20
|
-
// const traceContext = contextProvider();
|
|
21
|
-
// info.time = info.timestamp;
|
|
22
|
-
// delete info.timestamp;
|
|
23
|
-
// info.LEVEL = info.level.toUpperCase();
|
|
24
|
-
// delete info.level;
|
|
25
|
-
// info.trace = traceContext?.traceId|| "";
|
|
26
|
-
// info.span = traceContext?.spanId|| "";
|
|
27
|
-
// const orderedLog = {
|
|
28
|
-
// app: info.app ,
|
|
29
|
-
// trace: info.trace,
|
|
30
|
-
// span: info.span,
|
|
31
|
-
// time: info.time,
|
|
32
|
-
// LEVEL: info.LEVEL,
|
|
33
|
-
// thread: info.thread ?? "",
|
|
34
|
-
// class: info.class,
|
|
35
|
-
// message: info.message
|
|
36
|
-
// }
|
|
37
|
-
// return JSON.stringify(orderedLog, null, 0);
|
|
38
|
-
// }),
|
|
39
|
-
|
|
40
|
-
// );
|
|
41
|
-
|
|
42
|
-
// let silentLogs = false;
|
|
43
|
-
// let transports = [];
|
|
44
|
-
|
|
45
|
-
// let loggerInstance; // Variable to store the logger instance
|
|
46
|
-
// let globalConfig = {}
|
|
47
|
-
// const setGlobalConfig = (config) => {
|
|
48
|
-
// globalConfig.SERVICE_NAME = config?.SERVICE_NAME;
|
|
49
|
-
// globalConfig.LOG_LEVEL = config?.LOG_LEVEL;
|
|
50
|
-
// globalConfig.DEBUG_MODE = config?.DEBUG_MODE;
|
|
51
|
-
// globalConfig.SENSITIVE_KEYS = config?.SENSITIVE_KEYS;
|
|
52
|
-
// }
|
|
53
|
-
|
|
54
|
-
// const createLogger = (config) => {
|
|
55
|
-
// if (loggerInstance) {
|
|
56
|
-
// return loggerInstance; // If logger instance already exists, return it
|
|
57
|
-
// }
|
|
58
|
-
// let silentLogs = false;
|
|
59
|
-
// let transports = [];
|
|
60
|
-
// setGlobalConfig(config)
|
|
61
|
-
// if (globalConfig.DEBUG_MODE === DEBUG_MODES.DISABLE) {
|
|
62
|
-
// silentLogs = true;
|
|
63
|
-
// }
|
|
64
|
-
|
|
65
|
-
// if (globalConfig.DEBUG_MODE === DEBUG_MODES.FILE) {
|
|
66
|
-
// transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
67
|
-
// } else if (globalConfig.DEBUG_MODE === DEBUG_MODES.CONSOLE) {
|
|
68
|
-
// transports.push(new winston.transports.Console());
|
|
69
|
-
// } else {
|
|
70
|
-
// transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
71
|
-
// transports.push(new winston.transports.Console());
|
|
72
|
-
// }
|
|
73
|
-
// loggerInstance = winston.createLogger({
|
|
74
|
-
// format: logFormat,
|
|
75
|
-
// defaultMeta: { app: globalConfig.SERVICE_NAME },
|
|
76
|
-
// transports,
|
|
77
|
-
// silent: silentLogs,
|
|
78
|
-
// level: globalConfig.LOG_LEVEL,
|
|
79
|
-
// });
|
|
80
|
-
|
|
81
|
-
// // Attach log methods to the logger instance
|
|
82
|
-
// loggerInstance.logInfo = (message, data = {}) => {
|
|
83
|
-
// data = { ...data };
|
|
84
|
-
// data.thread = "";
|
|
85
|
-
// data.class = "";
|
|
86
|
-
// data.message = {};
|
|
87
|
-
// data.message.step = data?.step;
|
|
88
|
-
// data.message.message = message
|
|
89
|
-
// data.message.method = data?.method?.toUpperCase() ?? "";
|
|
90
|
-
// if(data?.method){
|
|
91
|
-
// delete data.method;
|
|
92
|
-
// }
|
|
93
|
-
|
|
94
|
-
// data.message.parameters = data?.parameters ?? "";
|
|
95
|
-
// if(data?.parameters){
|
|
96
|
-
// delete data.parameters;
|
|
97
|
-
// }
|
|
98
|
-
|
|
99
|
-
// data.message.path = data?.url ?? "";
|
|
100
|
-
// data.message.headers = data?.headers ?? {};
|
|
101
|
-
// data.message.responseHeaders = data?.responseHeaders ?? {};
|
|
102
|
-
// data.message.api = data?.api ?? "";
|
|
103
|
-
// if (data?.api) {
|
|
104
|
-
// delete data.api;
|
|
105
|
-
// }
|
|
106
|
-
// const authHeader = data?.headers?.authorization || data?.headers?.Authorization;
|
|
107
|
-
// const tokenData = jwt.decode(authHeader?.split(" ")[1], { complete: true })?.payload;
|
|
108
|
-
// data.message.tokenDetails = {
|
|
109
|
-
// "customerId":tokenData?.customerId ?? "",
|
|
110
|
-
// "mid":tokenData?.mid ?? "",
|
|
111
|
-
// "tenantId":tokenData?.tenantId ?? ""
|
|
112
|
-
// }
|
|
113
|
-
// data.message.responseStatus = data?.statusCode ?? "";
|
|
114
|
-
// if (data?.statusCode) {
|
|
115
|
-
// delete data.statusCode;
|
|
116
|
-
// }
|
|
117
|
-
// if (data?.headers) {
|
|
118
|
-
// delete data?.headers;
|
|
119
|
-
// }
|
|
120
|
-
// if (data?.responseHeaders) {
|
|
121
|
-
// delete data?.responseHeaders;
|
|
122
|
-
// }
|
|
123
|
-
// if (data?.url) {
|
|
124
|
-
// delete data.url;
|
|
125
|
-
// }
|
|
126
|
-
// if (data?.query) {
|
|
127
|
-
// delete data.query;
|
|
128
|
-
// }
|
|
129
|
-
// data.message.requestBody = data?.requestBody ?? {};
|
|
130
|
-
// if (data?.requestBody) {
|
|
131
|
-
// delete data.requestBody;
|
|
132
|
-
// }
|
|
133
|
-
|
|
134
|
-
// data.message.responseBody = data?.responseBody ?? {};
|
|
135
|
-
// if (data?.responseBody) {
|
|
136
|
-
// delete data.responseBody;
|
|
137
|
-
// }
|
|
138
|
-
// data.message = JSON.stringify(data.message);
|
|
139
|
-
// loggerInstance.info(data);
|
|
140
|
-
// };
|
|
141
|
-
|
|
142
|
-
// loggerInstance.logError = (data = {}, req = {}) => {
|
|
143
|
-
// data = { ...data };
|
|
144
|
-
// const errorResponse = data?.error?.response;
|
|
145
|
-
// let requestId = req?.headers?.requestid || "";
|
|
146
|
-
// let userInfo = req?.user || {};
|
|
147
|
-
// data.message = {
|
|
148
|
-
// step: data?.step,
|
|
149
|
-
// error: data?.error?.message,
|
|
150
|
-
// stack: data?.error?.stack,
|
|
151
|
-
// responseStatus: errorResponse?.status || HTTP_STATUS_CODES.BAD_REQUEST,
|
|
152
|
-
// path: errorResponse?.config?.url || req?.originalUrl,
|
|
153
|
-
// headers: errorResponse?.config?.headers || {},
|
|
154
|
-
// requestBody: errorResponse?.config?.body || req?.body,
|
|
155
|
-
// method: errorResponse?.config?.method?.toUpperCase() || req?.method?.toUpperCase(),
|
|
156
|
-
// responseBody: errorResponse?.data || {},
|
|
157
|
-
// };
|
|
158
|
-
// data.thread = "";
|
|
159
|
-
// data.class = "";
|
|
160
|
-
// const authHeader = data?.headers?.authorization || data?.headers?.Authorization;
|
|
161
|
-
// const tokenData = jwt.decode(authHeader?.split(" ")[1], { complete: true })?.payload;
|
|
162
|
-
// data.message.tokenDetails = {
|
|
163
|
-
// "customerId":tokenData?.customerId ?? "",
|
|
164
|
-
// "mid":tokenData?.mid ?? "",
|
|
165
|
-
// "tenantId":tokenData?.tenantId ?? ""
|
|
166
|
-
// }
|
|
167
|
-
// if (data?.step) delete data?.step;
|
|
168
|
-
// if (data?.error) delete data.error;
|
|
169
|
-
// data["level"] = LOG_LEVELS.ERROR;
|
|
170
|
-
// data.message = JSON.stringify(data.message);
|
|
171
|
-
// loggerInstance.error(data);
|
|
172
|
-
// };
|
|
173
|
-
|
|
174
|
-
// loggerInstance.logDebug = (message, data = {}) => {
|
|
175
|
-
// data.message = message;
|
|
176
|
-
// loggerInstance.debug(data);
|
|
177
|
-
// };
|
|
178
|
-
|
|
179
|
-
// return loggerInstance;
|
|
180
|
-
// };
|
|
181
|
-
|
|
182
|
-
// module.exports = {
|
|
183
|
-
// logger: loggerInstance,
|
|
184
|
-
// createLogger,
|
|
185
|
-
// setContextProvider,
|
|
186
|
-
// attachAxiosLogger: require("../axios-logger/axiosLogger"), // Export axios logger here
|
|
1
|
+
// const winston = require("winston");
|
|
2
|
+
// const { LOG_LEVELS, DEBUG_MODES, HTTP_STATUS_CODES } = require("./constants");
|
|
3
|
+
// const { ecsStringify } = require("@elastic/ecs-winston-format");
|
|
4
|
+
// const { redactInformation } = require("./redact");
|
|
5
|
+
// const jwt = require("jsonwebtoken");
|
|
6
|
+
|
|
7
|
+
// let contextProvider = () => ({});
|
|
8
|
+
|
|
9
|
+
// function setContextProvider(provider) {
|
|
10
|
+
// contextProvider = provider;
|
|
11
|
+
// }
|
|
12
|
+
|
|
13
|
+
// const logFormat = winston.format.combine(
|
|
14
|
+
// winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
|
15
|
+
// winston.format((info) => redactInformation(info, {sensitiveKeys: globalConfig?.SENSITIVE_KEYS}))(), // Redact sensitive data
|
|
16
|
+
// winston.format.errors({ stack: true }),
|
|
17
|
+
// winston.format.json(),
|
|
18
|
+
// ecsStringify(),
|
|
19
|
+
// winston.format.printf((info) => {
|
|
20
|
+
// const traceContext = contextProvider();
|
|
21
|
+
// info.time = info.timestamp;
|
|
22
|
+
// delete info.timestamp;
|
|
23
|
+
// info.LEVEL = info.level.toUpperCase();
|
|
24
|
+
// delete info.level;
|
|
25
|
+
// info.trace = traceContext?.traceId|| "";
|
|
26
|
+
// info.span = traceContext?.spanId|| "";
|
|
27
|
+
// const orderedLog = {
|
|
28
|
+
// app: info.app ,
|
|
29
|
+
// trace: info.trace,
|
|
30
|
+
// span: info.span,
|
|
31
|
+
// time: info.time,
|
|
32
|
+
// LEVEL: info.LEVEL,
|
|
33
|
+
// thread: info.thread ?? "",
|
|
34
|
+
// class: info.class,
|
|
35
|
+
// message: info.message
|
|
36
|
+
// }
|
|
37
|
+
// return JSON.stringify(orderedLog, null, 0);
|
|
38
|
+
// }),
|
|
39
|
+
|
|
40
|
+
// );
|
|
41
|
+
|
|
42
|
+
// let silentLogs = false;
|
|
43
|
+
// let transports = [];
|
|
44
|
+
|
|
45
|
+
// let loggerInstance; // Variable to store the logger instance
|
|
46
|
+
// let globalConfig = {}
|
|
47
|
+
// const setGlobalConfig = (config) => {
|
|
48
|
+
// globalConfig.SERVICE_NAME = config?.SERVICE_NAME;
|
|
49
|
+
// globalConfig.LOG_LEVEL = config?.LOG_LEVEL;
|
|
50
|
+
// globalConfig.DEBUG_MODE = config?.DEBUG_MODE;
|
|
51
|
+
// globalConfig.SENSITIVE_KEYS = config?.SENSITIVE_KEYS;
|
|
52
|
+
// }
|
|
53
|
+
|
|
54
|
+
// const createLogger = (config) => {
|
|
55
|
+
// if (loggerInstance) {
|
|
56
|
+
// return loggerInstance; // If logger instance already exists, return it
|
|
57
|
+
// }
|
|
58
|
+
// let silentLogs = false;
|
|
59
|
+
// let transports = [];
|
|
60
|
+
// setGlobalConfig(config)
|
|
61
|
+
// if (globalConfig.DEBUG_MODE === DEBUG_MODES.DISABLE) {
|
|
62
|
+
// silentLogs = true;
|
|
63
|
+
// }
|
|
64
|
+
|
|
65
|
+
// if (globalConfig.DEBUG_MODE === DEBUG_MODES.FILE) {
|
|
66
|
+
// transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
67
|
+
// } else if (globalConfig.DEBUG_MODE === DEBUG_MODES.CONSOLE) {
|
|
68
|
+
// transports.push(new winston.transports.Console());
|
|
69
|
+
// } else {
|
|
70
|
+
// transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
71
|
+
// transports.push(new winston.transports.Console());
|
|
72
|
+
// }
|
|
73
|
+
// loggerInstance = winston.createLogger({
|
|
74
|
+
// format: logFormat,
|
|
75
|
+
// defaultMeta: { app: globalConfig.SERVICE_NAME },
|
|
76
|
+
// transports,
|
|
77
|
+
// silent: silentLogs,
|
|
78
|
+
// level: globalConfig.LOG_LEVEL,
|
|
79
|
+
// });
|
|
80
|
+
|
|
81
|
+
// // Attach log methods to the logger instance
|
|
82
|
+
// loggerInstance.logInfo = (message, data = {}) => {
|
|
83
|
+
// data = { ...data };
|
|
84
|
+
// data.thread = "";
|
|
85
|
+
// data.class = "";
|
|
86
|
+
// data.message = {};
|
|
87
|
+
// data.message.step = data?.step;
|
|
88
|
+
// data.message.message = message
|
|
89
|
+
// data.message.method = data?.method?.toUpperCase() ?? "";
|
|
90
|
+
// if(data?.method){
|
|
91
|
+
// delete data.method;
|
|
92
|
+
// }
|
|
93
|
+
|
|
94
|
+
// data.message.parameters = data?.parameters ?? "";
|
|
95
|
+
// if(data?.parameters){
|
|
96
|
+
// delete data.parameters;
|
|
97
|
+
// }
|
|
98
|
+
|
|
99
|
+
// data.message.path = data?.url ?? "";
|
|
100
|
+
// data.message.headers = data?.headers ?? {};
|
|
101
|
+
// data.message.responseHeaders = data?.responseHeaders ?? {};
|
|
102
|
+
// data.message.api = data?.api ?? "";
|
|
103
|
+
// if (data?.api) {
|
|
104
|
+
// delete data.api;
|
|
105
|
+
// }
|
|
106
|
+
// const authHeader = data?.headers?.authorization || data?.headers?.Authorization;
|
|
107
|
+
// const tokenData = jwt.decode(authHeader?.split(" ")[1], { complete: true })?.payload;
|
|
108
|
+
// data.message.tokenDetails = {
|
|
109
|
+
// "customerId":tokenData?.customerId ?? "",
|
|
110
|
+
// "mid":tokenData?.mid ?? "",
|
|
111
|
+
// "tenantId":tokenData?.tenantId ?? ""
|
|
112
|
+
// }
|
|
113
|
+
// data.message.responseStatus = data?.statusCode ?? "";
|
|
114
|
+
// if (data?.statusCode) {
|
|
115
|
+
// delete data.statusCode;
|
|
116
|
+
// }
|
|
117
|
+
// if (data?.headers) {
|
|
118
|
+
// delete data?.headers;
|
|
119
|
+
// }
|
|
120
|
+
// if (data?.responseHeaders) {
|
|
121
|
+
// delete data?.responseHeaders;
|
|
122
|
+
// }
|
|
123
|
+
// if (data?.url) {
|
|
124
|
+
// delete data.url;
|
|
125
|
+
// }
|
|
126
|
+
// if (data?.query) {
|
|
127
|
+
// delete data.query;
|
|
128
|
+
// }
|
|
129
|
+
// data.message.requestBody = data?.requestBody ?? {};
|
|
130
|
+
// if (data?.requestBody) {
|
|
131
|
+
// delete data.requestBody;
|
|
132
|
+
// }
|
|
133
|
+
|
|
134
|
+
// data.message.responseBody = data?.responseBody ?? {};
|
|
135
|
+
// if (data?.responseBody) {
|
|
136
|
+
// delete data.responseBody;
|
|
137
|
+
// }
|
|
138
|
+
// data.message = JSON.stringify(data.message);
|
|
139
|
+
// loggerInstance.info(data);
|
|
140
|
+
// };
|
|
141
|
+
|
|
142
|
+
// loggerInstance.logError = (data = {}, req = {}) => {
|
|
143
|
+
// data = { ...data };
|
|
144
|
+
// const errorResponse = data?.error?.response;
|
|
145
|
+
// let requestId = req?.headers?.requestid || "";
|
|
146
|
+
// let userInfo = req?.user || {};
|
|
147
|
+
// data.message = {
|
|
148
|
+
// step: data?.step,
|
|
149
|
+
// error: data?.error?.message,
|
|
150
|
+
// stack: data?.error?.stack,
|
|
151
|
+
// responseStatus: errorResponse?.status || HTTP_STATUS_CODES.BAD_REQUEST,
|
|
152
|
+
// path: errorResponse?.config?.url || req?.originalUrl,
|
|
153
|
+
// headers: errorResponse?.config?.headers || {},
|
|
154
|
+
// requestBody: errorResponse?.config?.body || req?.body,
|
|
155
|
+
// method: errorResponse?.config?.method?.toUpperCase() || req?.method?.toUpperCase(),
|
|
156
|
+
// responseBody: errorResponse?.data || {},
|
|
157
|
+
// };
|
|
158
|
+
// data.thread = "";
|
|
159
|
+
// data.class = "";
|
|
160
|
+
// const authHeader = data?.headers?.authorization || data?.headers?.Authorization;
|
|
161
|
+
// const tokenData = jwt.decode(authHeader?.split(" ")[1], { complete: true })?.payload;
|
|
162
|
+
// data.message.tokenDetails = {
|
|
163
|
+
// "customerId":tokenData?.customerId ?? "",
|
|
164
|
+
// "mid":tokenData?.mid ?? "",
|
|
165
|
+
// "tenantId":tokenData?.tenantId ?? ""
|
|
166
|
+
// }
|
|
167
|
+
// if (data?.step) delete data?.step;
|
|
168
|
+
// if (data?.error) delete data.error;
|
|
169
|
+
// data["level"] = LOG_LEVELS.ERROR;
|
|
170
|
+
// data.message = JSON.stringify(data.message);
|
|
171
|
+
// loggerInstance.error(data);
|
|
172
|
+
// };
|
|
173
|
+
|
|
174
|
+
// loggerInstance.logDebug = (message, data = {}) => {
|
|
175
|
+
// data.message = message;
|
|
176
|
+
// loggerInstance.debug(data);
|
|
177
|
+
// };
|
|
178
|
+
|
|
179
|
+
// return loggerInstance;
|
|
180
|
+
// };
|
|
181
|
+
|
|
182
|
+
// module.exports = {
|
|
183
|
+
// logger: loggerInstance,
|
|
184
|
+
// createLogger,
|
|
185
|
+
// setContextProvider,
|
|
186
|
+
// attachAxiosLogger: require("../axios-logger/axiosLogger"), // Export axios logger here
|
|
187
187
|
// };
|
package/src/index.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
// Logger features
|
|
2
|
-
const {
|
|
3
|
-
logInfo,
|
|
4
|
-
logError,
|
|
5
|
-
logDebug,
|
|
6
|
-
logger,
|
|
7
|
-
createLogger,
|
|
8
|
-
setContextProvider,
|
|
9
|
-
} = require("./features/logger");
|
|
10
|
-
|
|
11
|
-
// Express logger feature
|
|
12
|
-
const {logExpressApis} = require("./features/express-logger");
|
|
13
|
-
|
|
14
|
-
// Axios logger feature
|
|
15
|
-
const {attachAxiosLogger} = require("./features/axios-logger");
|
|
16
|
-
|
|
17
|
-
// Utility functions (if needed in the future)
|
|
18
|
-
const utils = require("./utils");
|
|
19
|
-
|
|
20
|
-
module.exports = {
|
|
21
|
-
// Logger-related exports
|
|
22
|
-
logInfo,
|
|
23
|
-
logError,
|
|
24
|
-
logDebug,
|
|
25
|
-
logger,
|
|
26
|
-
attachAxiosLogger,
|
|
27
|
-
createLogger,
|
|
28
|
-
// Express-related logging
|
|
29
|
-
logExpressApis,
|
|
30
|
-
|
|
31
|
-
// Axios-related logging
|
|
32
|
-
|
|
33
|
-
// Future utilities
|
|
34
|
-
utils,
|
|
35
|
-
setContextProvider,
|
|
36
|
-
};
|
|
1
|
+
// Logger features
|
|
2
|
+
const {
|
|
3
|
+
logInfo,
|
|
4
|
+
logError,
|
|
5
|
+
logDebug,
|
|
6
|
+
logger,
|
|
7
|
+
createLogger,
|
|
8
|
+
setContextProvider,
|
|
9
|
+
} = require("./features/logger");
|
|
10
|
+
|
|
11
|
+
// Express logger feature
|
|
12
|
+
const {logExpressApis} = require("./features/express-logger");
|
|
13
|
+
|
|
14
|
+
// Axios logger feature
|
|
15
|
+
const {attachAxiosLogger} = require("./features/axios-logger");
|
|
16
|
+
|
|
17
|
+
// Utility functions (if needed in the future)
|
|
18
|
+
const utils = require("./utils");
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
// Logger-related exports
|
|
22
|
+
logInfo,
|
|
23
|
+
logError,
|
|
24
|
+
logDebug,
|
|
25
|
+
logger,
|
|
26
|
+
attachAxiosLogger,
|
|
27
|
+
createLogger,
|
|
28
|
+
// Express-related logging
|
|
29
|
+
logExpressApis,
|
|
30
|
+
|
|
31
|
+
// Axios-related logging
|
|
32
|
+
|
|
33
|
+
// Future utilities
|
|
34
|
+
utils,
|
|
35
|
+
setContextProvider,
|
|
36
|
+
};
|
package/src/utils/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// const envConfig = require('../config'); // dependency `elastic-apm-node`
|
|
2
|
-
|
|
3
|
-
// const apm = require('elastic-apm-node').start({
|
|
4
|
-
// serviceName: envConfig.serviceName,
|
|
5
|
-
// serverUrl: envConfig?.apm?.serverUrl,
|
|
6
|
-
// // Use if APM Server requires a token
|
|
7
|
-
// captureBody: 'all',
|
|
8
|
-
// secretToken: "",
|
|
9
|
-
// active: envConfig?.apm?.apmEnabled === "true" ? true : false,
|
|
10
|
-
// // })
|
|
11
|
-
module.exports = {}
|
|
1
|
+
// const envConfig = require('../config'); // dependency `elastic-apm-node`
|
|
2
|
+
|
|
3
|
+
// const apm = require('elastic-apm-node').start({
|
|
4
|
+
// serviceName: envConfig.serviceName,
|
|
5
|
+
// serverUrl: envConfig?.apm?.serverUrl,
|
|
6
|
+
// // Use if APM Server requires a token
|
|
7
|
+
// captureBody: 'all',
|
|
8
|
+
// secretToken: "",
|
|
9
|
+
// active: envConfig?.apm?.apmEnabled === "true" ? true : false,
|
|
10
|
+
// // })
|
|
11
|
+
module.exports = {}
|