@takentrade/takentrade-libs 4.1.11 → 4.1.13
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/dist/logging/index.d.ts
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface LogContext {
|
|
2
|
+
correlationId?: string;
|
|
3
|
+
userId?: string;
|
|
4
|
+
service?: string;
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
}
|
|
7
|
+
export type LoggerContext = string | LogContext;
|
|
8
|
+
export interface TntLogger {
|
|
9
|
+
setContext(context: LogContext): void;
|
|
10
|
+
log(message: unknown, context?: LoggerContext): void;
|
|
11
|
+
error(message: unknown, trace?: unknown, context?: LoggerContext): void;
|
|
12
|
+
warn(message: unknown, context?: LoggerContext): void;
|
|
13
|
+
debug(message: unknown, context?: LoggerContext): void;
|
|
14
|
+
verbose(message: unknown, context?: LoggerContext): void;
|
|
15
|
+
logRequest(req: any, context?: LogContext): void;
|
|
16
|
+
logResponse(req: any, res: any, responseTime: number, context?: LogContext): void;
|
|
17
|
+
}
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import { LoggerService as NestLoggerService } from '@nestjs/common';
|
|
2
|
-
|
|
3
|
-
correlationId?: string;
|
|
4
|
-
userId?: string;
|
|
5
|
-
service?: string;
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
}
|
|
8
|
-
type LoggerContext = string | LogContext;
|
|
2
|
+
import { LogContext, LoggerContext, TntLogger } from './logger.interface';
|
|
9
3
|
/**
|
|
10
4
|
* TNT Logger Service - Pino-based structured JSON logger
|
|
11
5
|
* Provides consistent logging across all microservices
|
|
12
6
|
*/
|
|
13
|
-
export declare class TntLoggerService implements NestLoggerService {
|
|
7
|
+
export declare class TntLoggerService implements NestLoggerService, TntLogger {
|
|
14
8
|
private logger;
|
|
15
9
|
private context;
|
|
16
10
|
constructor(context?: LogContext);
|
|
@@ -19,26 +13,28 @@ export declare class TntLoggerService implements NestLoggerService {
|
|
|
19
13
|
*/
|
|
20
14
|
setContext(context: LogContext): void;
|
|
21
15
|
private normalizeContext;
|
|
16
|
+
private formatValue;
|
|
17
|
+
private formatMessage;
|
|
22
18
|
/**
|
|
23
19
|
* Log info message
|
|
24
20
|
*/
|
|
25
|
-
log(message:
|
|
21
|
+
log(message: unknown, context?: LoggerContext): void;
|
|
26
22
|
/**
|
|
27
23
|
* Log error message
|
|
28
24
|
*/
|
|
29
|
-
error(message:
|
|
25
|
+
error(message: unknown, trace?: unknown, context?: LoggerContext): void;
|
|
30
26
|
/**
|
|
31
27
|
* Log warning message
|
|
32
28
|
*/
|
|
33
|
-
warn(message:
|
|
29
|
+
warn(message: unknown, context?: LoggerContext): void;
|
|
34
30
|
/**
|
|
35
31
|
* Log debug message
|
|
36
32
|
*/
|
|
37
|
-
debug(message:
|
|
33
|
+
debug(message: unknown, context?: LoggerContext): void;
|
|
38
34
|
/**
|
|
39
35
|
* Log verbose message
|
|
40
36
|
*/
|
|
41
|
-
verbose(message:
|
|
37
|
+
verbose(message: unknown, context?: LoggerContext): void;
|
|
42
38
|
/**
|
|
43
39
|
* Log HTTP request
|
|
44
40
|
*/
|
|
@@ -48,4 +44,3 @@ export declare class TntLoggerService implements NestLoggerService {
|
|
|
48
44
|
*/
|
|
49
45
|
logResponse(req: any, res: any, responseTime: number, context?: LogContext): void;
|
|
50
46
|
}
|
|
51
|
-
export {};
|
|
@@ -49,39 +49,73 @@ let TntLoggerService = class TntLoggerService {
|
|
|
49
49
|
return {};
|
|
50
50
|
}
|
|
51
51
|
if (typeof context === 'string') {
|
|
52
|
-
return {
|
|
52
|
+
return {};
|
|
53
53
|
}
|
|
54
54
|
return context;
|
|
55
55
|
}
|
|
56
|
+
formatValue(value) {
|
|
57
|
+
if (value instanceof Error) {
|
|
58
|
+
return value.stack ?? value.message;
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === 'string') {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
64
|
+
return String(value);
|
|
65
|
+
}
|
|
66
|
+
if (value === null || value === undefined) {
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
if (typeof value === 'object') {
|
|
70
|
+
try {
|
|
71
|
+
return JSON.stringify(value);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return Object.prototype.toString.call(value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return String(value);
|
|
78
|
+
}
|
|
79
|
+
formatMessage(message, context) {
|
|
80
|
+
const normalizedMessage = this.formatValue(message);
|
|
81
|
+
if (typeof context === 'string' && context.trim().length > 0) {
|
|
82
|
+
return `[${context.trim()}] ${normalizedMessage}`;
|
|
83
|
+
}
|
|
84
|
+
return normalizedMessage;
|
|
85
|
+
}
|
|
56
86
|
/**
|
|
57
87
|
* Log info message
|
|
58
88
|
*/
|
|
59
89
|
log(message, context) {
|
|
60
|
-
this.logger.info({ ...this.context, ...this.normalizeContext(context) }, message);
|
|
90
|
+
this.logger.info({ ...this.context, ...this.normalizeContext(context) }, this.formatMessage(message, context));
|
|
61
91
|
}
|
|
62
92
|
/**
|
|
63
93
|
* Log error message
|
|
64
94
|
*/
|
|
65
95
|
error(message, trace, context) {
|
|
66
|
-
this.logger.error({
|
|
96
|
+
this.logger.error({
|
|
97
|
+
...this.context,
|
|
98
|
+
...this.normalizeContext(context),
|
|
99
|
+
trace: this.formatValue(trace),
|
|
100
|
+
}, this.formatMessage(message, context));
|
|
67
101
|
}
|
|
68
102
|
/**
|
|
69
103
|
* Log warning message
|
|
70
104
|
*/
|
|
71
105
|
warn(message, context) {
|
|
72
|
-
this.logger.warn({ ...this.context, ...this.normalizeContext(context) }, message);
|
|
106
|
+
this.logger.warn({ ...this.context, ...this.normalizeContext(context) }, this.formatMessage(message, context));
|
|
73
107
|
}
|
|
74
108
|
/**
|
|
75
109
|
* Log debug message
|
|
76
110
|
*/
|
|
77
111
|
debug(message, context) {
|
|
78
|
-
this.logger.debug({ ...this.context, ...this.normalizeContext(context) }, message);
|
|
112
|
+
this.logger.debug({ ...this.context, ...this.normalizeContext(context) }, this.formatMessage(message, context));
|
|
79
113
|
}
|
|
80
114
|
/**
|
|
81
115
|
* Log verbose message
|
|
82
116
|
*/
|
|
83
117
|
verbose(message, context) {
|
|
84
|
-
this.logger.trace({ ...this.context, ...this.normalizeContext(context) }, message);
|
|
118
|
+
this.logger.trace({ ...this.context, ...this.normalizeContext(context) }, this.formatMessage(message, context));
|
|
85
119
|
}
|
|
86
120
|
/**
|
|
87
121
|
* Log HTTP request
|