sasai-common-utils 1.0.1 → 1.0.2
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/package.json +1 -1
- package/src/features/logger/index.js +5 -98
- package/src/index.js +97 -10
package/package.json
CHANGED
|
@@ -1,102 +1,9 @@
|
|
|
1
|
-
const
|
|
2
|
-
let { LOG_LEVELS, DEBUG_MODES } = require("../constants/logConstants");
|
|
3
|
-
const { HTTP_STATUS_CODES } = require("../constants/apiResponseConstants");
|
|
4
|
-
const apmTracing = require("../../apmTracing");
|
|
5
|
-
const { extractDeviceInfoFromHeaders } = require("../utils/utils");
|
|
6
|
-
const envConfig = require("../config");
|
|
7
|
-
const { ecsFormat, ecsFields, ecsStringify } = require("@elastic/ecs-winston-format");
|
|
1
|
+
const createLogger = require("./logger");
|
|
8
2
|
const { redactInformation } = require("./redact");
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const logFormat = winston.format.combine(
|
|
12
|
-
winston.format(info => redactInformation(info))(), // Prevent logging sensitive data
|
|
13
|
-
winston.format.errors({ stack: true }),
|
|
14
|
-
winston.format.json(),
|
|
15
|
-
winston.format.printf(info => {
|
|
16
|
-
info.time = info.timestamp;
|
|
17
|
-
delete info.timestamp;
|
|
18
|
-
info.LEVEL = info.level.toUpperCase();
|
|
19
|
-
delete info.level;
|
|
20
|
-
info.trace = apmTracing?.currentTraceIds?.["trace.id"] ?? "";
|
|
21
|
-
info.span = apmTracing?.currentTraceIds?.["transaction.id"] ?? "";
|
|
22
|
-
return JSON.stringify(info, null, 4);
|
|
23
|
-
}),
|
|
24
|
-
ecsStringify()
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
let silentLogs = false;
|
|
28
|
-
let transports = [];
|
|
29
|
-
|
|
30
|
-
if (envConfig?.debugging?.debugMode === DEBUG_MODES.DISABLE) {
|
|
31
|
-
silentLogs = true;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (envConfig?.debugging?.debugMode === DEBUG_MODES.FILE) {
|
|
35
|
-
transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
36
|
-
} else if (envConfig?.debugging?.debugMode === DEBUG_MODES.CONSOLE) {
|
|
37
|
-
transports.push(new winston.transports.Console());
|
|
38
|
-
} else {
|
|
39
|
-
transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
40
|
-
transports.push(new winston.transports.Console());
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Default logger instance
|
|
44
|
-
const defaultLogger = winston.createLogger({
|
|
45
|
-
format: logFormat,
|
|
46
|
-
defaultMeta: { app: envConfig?.serviceName },
|
|
47
|
-
transports,
|
|
48
|
-
silent: silentLogs,
|
|
49
|
-
level: envConfig.logLevel,
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const logInfo = (message, data = {}) => {
|
|
53
|
-
data.info = message;
|
|
54
|
-
data.path = data?.url ?? "";
|
|
55
|
-
const tokenData = jwt.decode(data?.headers?.Authorization?.split(" ")[1], { complete: true })?.payload;
|
|
56
|
-
data.tokenDetails = {
|
|
57
|
-
"customerId": tokenData?.customerId ?? "",
|
|
58
|
-
"mid": tokenData?.mid ?? "",
|
|
59
|
-
"tenantId": tokenData?.tenantId ?? ""
|
|
60
|
-
};
|
|
61
|
-
data.queryParams = data?.query ?? {};
|
|
62
|
-
if (data?.query) delete data?.query;
|
|
63
|
-
data.requestBody = data?.body ?? {};
|
|
64
|
-
if (data?.body) delete data.body;
|
|
65
|
-
data.response = data?.data ?? {};
|
|
66
|
-
if (data?.data) delete data?.data;
|
|
67
|
-
defaultLogger.info(data);
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const logError = (data = {}, req = {}) => {
|
|
71
|
-
data = { ...data };
|
|
72
|
-
const errorResponse = data?.error?.response;
|
|
73
|
-
let requestId = req?.headers?.requestid ?? "";
|
|
74
|
-
let userInfo = req?.user ?? {};
|
|
75
|
-
data.message = {
|
|
76
|
-
step: data?.step,
|
|
77
|
-
error: data?.error?.message,
|
|
78
|
-
stack: data?.error?.stack,
|
|
79
|
-
statusCode: errorResponse?.status ?? HTTP_STATUS_CODES.BAD_REQUEST,
|
|
80
|
-
path: errorResponse?.config?.url ?? req?.originalUrl,
|
|
81
|
-
headers: errorResponse?.config?.headers ?? {},
|
|
82
|
-
requestBody: errorResponse?.config?.body ?? req?.body,
|
|
83
|
-
method: errorResponse?.config?.method ?? req?.method,
|
|
84
|
-
response: errorResponse?.data ?? {}
|
|
85
|
-
};
|
|
86
|
-
if (data?.step) delete data?.step;
|
|
87
|
-
if (data?.error) delete data.error;
|
|
88
|
-
data["level"] = LOG_LEVELS.ERROR;
|
|
89
|
-
defaultLogger.error(data);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const logDebug = (message, data = {}) => {
|
|
93
|
-
data.message = message;
|
|
94
|
-
defaultLogger.debug(data);
|
|
95
|
-
};
|
|
3
|
+
const { LOG_LEVELS } = require("./constants");
|
|
96
4
|
|
|
97
5
|
module.exports = {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
logger: defaultLogger
|
|
6
|
+
createLogger,
|
|
7
|
+
redactInformation,
|
|
8
|
+
LOG_LEVELS,
|
|
102
9
|
};
|
package/src/index.js
CHANGED
|
@@ -1,15 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const winston = require("winston");
|
|
2
|
+
let { LOG_LEVELS, DEBUG_MODES } = require("../constants/logConstants");
|
|
3
|
+
const { HTTP_STATUS_CODES } = require("../constants/apiResponseConstants");
|
|
4
|
+
const apmTracing = require("../../apmTracing");
|
|
5
|
+
const { extractDeviceInfoFromHeaders } = require("../utils/utils");
|
|
6
|
+
const envConfig = require("../config");
|
|
7
|
+
const { ecsFormat, ecsFields, ecsStringify } = require("@elastic/ecs-winston-format");
|
|
8
|
+
const { redactInformation } = require("./redact");
|
|
9
|
+
const jwt = require("jsonwebtoken");
|
|
3
10
|
|
|
4
|
-
|
|
5
|
-
|
|
11
|
+
const logFormat = winston.format.combine(
|
|
12
|
+
winston.format(info => redactInformation(info))(), // Prevent logging sensitive data
|
|
13
|
+
winston.format.errors({ stack: true }),
|
|
14
|
+
winston.format.json(),
|
|
15
|
+
winston.format.printf(info => {
|
|
16
|
+
info.time = info.timestamp;
|
|
17
|
+
delete info.timestamp;
|
|
18
|
+
info.LEVEL = info.level.toUpperCase();
|
|
19
|
+
delete info.level;
|
|
20
|
+
info.trace = apmTracing?.currentTraceIds?.["trace.id"] ?? "";
|
|
21
|
+
info.span = apmTracing?.currentTraceIds?.["transaction.id"] ?? "";
|
|
22
|
+
return JSON.stringify(info, null, 4);
|
|
23
|
+
}),
|
|
24
|
+
ecsStringify()
|
|
25
|
+
);
|
|
6
26
|
|
|
7
|
-
|
|
8
|
-
|
|
27
|
+
let silentLogs = false;
|
|
28
|
+
let transports = [];
|
|
29
|
+
|
|
30
|
+
if (envConfig?.debugging?.debugMode === DEBUG_MODES.DISABLE) {
|
|
31
|
+
silentLogs = true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (envConfig?.debugging?.debugMode === DEBUG_MODES.FILE) {
|
|
35
|
+
transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
36
|
+
} else if (envConfig?.debugging?.debugMode === DEBUG_MODES.CONSOLE) {
|
|
37
|
+
transports.push(new winston.transports.Console());
|
|
38
|
+
} else {
|
|
39
|
+
transports.push(new winston.transports.File({ filename: "app.log" }));
|
|
40
|
+
transports.push(new winston.transports.Console());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Default logger instance
|
|
44
|
+
const defaultLogger = winston.createLogger({
|
|
45
|
+
format: logFormat,
|
|
46
|
+
defaultMeta: { app: envConfig?.serviceName },
|
|
47
|
+
transports,
|
|
48
|
+
silent: silentLogs,
|
|
49
|
+
level: envConfig.logLevel,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const logInfo = (message, data = {}) => {
|
|
53
|
+
data.info = message;
|
|
54
|
+
data.path = data?.url ?? "";
|
|
55
|
+
const tokenData = jwt.decode(data?.headers?.Authorization?.split(" ")[1], { complete: true })?.payload;
|
|
56
|
+
data.tokenDetails = {
|
|
57
|
+
"customerId": tokenData?.customerId ?? "",
|
|
58
|
+
"mid": tokenData?.mid ?? "",
|
|
59
|
+
"tenantId": tokenData?.tenantId ?? ""
|
|
60
|
+
};
|
|
61
|
+
data.queryParams = data?.query ?? {};
|
|
62
|
+
if (data?.query) delete data?.query;
|
|
63
|
+
data.requestBody = data?.body ?? {};
|
|
64
|
+
if (data?.body) delete data.body;
|
|
65
|
+
data.response = data?.data ?? {};
|
|
66
|
+
if (data?.data) delete data?.data;
|
|
67
|
+
defaultLogger.info(data);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const logError = (data = {}, req = {}) => {
|
|
71
|
+
data = { ...data };
|
|
72
|
+
const errorResponse = data?.error?.response;
|
|
73
|
+
let requestId = req?.headers?.requestid ?? "";
|
|
74
|
+
let userInfo = req?.user ?? {};
|
|
75
|
+
data.message = {
|
|
76
|
+
step: data?.step,
|
|
77
|
+
error: data?.error?.message,
|
|
78
|
+
stack: data?.error?.stack,
|
|
79
|
+
statusCode: errorResponse?.status ?? HTTP_STATUS_CODES.BAD_REQUEST,
|
|
80
|
+
path: errorResponse?.config?.url ?? req?.originalUrl,
|
|
81
|
+
headers: errorResponse?.config?.headers ?? {},
|
|
82
|
+
requestBody: errorResponse?.config?.body ?? req?.body,
|
|
83
|
+
method: errorResponse?.config?.method ?? req?.method,
|
|
84
|
+
response: errorResponse?.data ?? {}
|
|
85
|
+
};
|
|
86
|
+
if (data?.step) delete data?.step;
|
|
87
|
+
if (data?.error) delete data.error;
|
|
88
|
+
data["level"] = LOG_LEVELS.ERROR;
|
|
89
|
+
defaultLogger.error(data);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const logDebug = (message, data = {}) => {
|
|
93
|
+
data.message = message;
|
|
94
|
+
defaultLogger.debug(data);
|
|
95
|
+
};
|
|
9
96
|
|
|
10
97
|
module.exports = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
98
|
+
logInfo,
|
|
99
|
+
logError,
|
|
100
|
+
logDebug,
|
|
101
|
+
logger: defaultLogger
|
|
15
102
|
};
|