sasai-common-utils 1.0.26 → 1.0.28
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 -0
- package/package.json +1 -1
- package/src/features/logger/index.js +1 -0
- package/src/features/logger/redact.js +9 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
import { Logger } from "winston";
|
|
3
|
+
import { NextFunction, Request, Response } from "express";
|
|
4
|
+
import { Logger as WinstonLogger } from "winston";
|
|
5
|
+
|
|
6
|
+
// Extend the Winston Logger to include custom methods
|
|
7
|
+
export interface ExtendedLogger extends WinstonLogger {
|
|
8
|
+
logError: (data: Record<string, any>, req?: Record<string, any>) => void;
|
|
9
|
+
logInfo: (message: string, data?: Record<string, any>) => void;
|
|
10
|
+
logDebug: (message: string, data?: Record<string, any>) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Export the logger with the extended type
|
|
14
|
+
export const logger: ExtendedLogger;
|
|
15
|
+
/**
|
|
16
|
+
* Logger configuration options.
|
|
17
|
+
*/
|
|
18
|
+
export interface LoggerConfig {
|
|
19
|
+
level?: string;
|
|
20
|
+
SERVICE_NAME?: string;
|
|
21
|
+
DEBUG_MODE?: string;
|
|
22
|
+
LOG_LEVEL?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Context provider type for custom context setup.
|
|
27
|
+
*/
|
|
28
|
+
export type ContextProvider = () => Record<string, any>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Logs information-level messages.
|
|
32
|
+
* @param message The message to log.
|
|
33
|
+
* @param data Additional data to include in the log.
|
|
34
|
+
*/
|
|
35
|
+
export function logInfo(message: string, data?: Record<string, any>): void;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Logs error-level messages.
|
|
39
|
+
* @param error The error object or message to log.
|
|
40
|
+
* @param data Additional data to include in the log.
|
|
41
|
+
*/
|
|
42
|
+
export function logError(error: Error | string, data?: Record<string, any>): void;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Logs debug-level messages.
|
|
46
|
+
* @param message The message to log.
|
|
47
|
+
* @param data Additional data to include in the log.
|
|
48
|
+
*/
|
|
49
|
+
export function logDebug(message: string, data?: Record<string, any>): void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A shared logger instance configured with the provided settings.
|
|
53
|
+
*/
|
|
54
|
+
// export const logger: Logger;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Creates a new logger instance.
|
|
58
|
+
* @param config Optional configuration for the logger.
|
|
59
|
+
*/
|
|
60
|
+
export function createLogger(config?: LoggerConfig): Logger;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sets a context provider for adding custom context to logs.
|
|
64
|
+
* @param provider A function that returns custom context as an object.
|
|
65
|
+
*/
|
|
66
|
+
export function setContextProvider(provider: ContextProvider): void;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Middleware to log Express API requests and responses.
|
|
70
|
+
*/
|
|
71
|
+
export function logExpressApis(logger: Logger): void;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Attaches Axios request and response interceptors for logging.
|
|
75
|
+
* @param axiosInstance An Axios instance.
|
|
76
|
+
* @param logger A logger instance.
|
|
77
|
+
* @param apmTracing An object containing APM trace ID and span ID.
|
|
78
|
+
*/
|
|
79
|
+
export function attachAxiosLogger(
|
|
80
|
+
axiosInstance: AxiosInstance,
|
|
81
|
+
logger: Logger,
|
|
82
|
+
apmTracing?: Record<string, any>
|
|
83
|
+
): void;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Utility functions (future scope).
|
|
87
|
+
*/
|
|
88
|
+
export const utils: Record<string, any>;
|
package/package.json
CHANGED
|
@@ -85,6 +85,7 @@ if (globalConfig.DEBUG_MODE === DEBUG_MODES.FILE) {
|
|
|
85
85
|
data.class = "";
|
|
86
86
|
data.message = {};
|
|
87
87
|
data.message.step = data?.step;
|
|
88
|
+
data.message.message = message
|
|
88
89
|
data.message.method = data?.method?.toUpperCase() ?? "";
|
|
89
90
|
if(data?.method){
|
|
90
91
|
delete data.method;
|
|
@@ -17,15 +17,21 @@ function redactObject(obj) {
|
|
|
17
17
|
this.update("[REDACTED]");
|
|
18
18
|
}
|
|
19
19
|
if (this.key === "message" && typeof this.node === "string") {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
let parsedMessage
|
|
21
|
+
try {
|
|
22
|
+
parsedMessage = JSON.parse(this?.node);
|
|
23
|
+
if (Object.keys(parsedMessage?.headers).length ) {
|
|
22
24
|
parsedMessage.headers.authorization = "[REDACTED]";
|
|
23
25
|
parsedMessage.headers.Authorization = "[REDACTED]";
|
|
24
26
|
}
|
|
25
27
|
if (parsedMessage?.responseHeaders) {
|
|
26
28
|
parsedMessage.responseHeaders.authorization = "[REDACTED]";
|
|
27
29
|
parsedMessage.responseHeaders.Authorization = "[REDACTED]";
|
|
28
|
-
}
|
|
30
|
+
}
|
|
31
|
+
} catch (error) {
|
|
32
|
+
parsedMessage = {};
|
|
33
|
+
}
|
|
34
|
+
|
|
29
35
|
this.update(JSON.stringify(parsedMessage)); // Update with the redacted string
|
|
30
36
|
}
|
|
31
37
|
});
|