sasai-common-utils 1.0.0 → 1.0.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sasai-common-utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Reusable utility library for common logging and other shared features.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,9 +1,102 @@
1
- const createLogger = require("./logger");
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");
2
8
  const { redactInformation } = require("./redact");
3
- const { LOG_LEVELS } = require("./constants");
9
+ const jwt = require("jsonwebtoken");
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
+ };
4
96
 
5
97
  module.exports = {
6
- createLogger,
7
- redactInformation,
8
- LOG_LEVELS,
98
+ logInfo,
99
+ logError,
100
+ logDebug,
101
+ logger: defaultLogger
9
102
  };