@tsed/di 8.6.0 → 8.6.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/lib/esm/node/domain/ContextLogger.js +18 -3
- package/lib/esm/node/domain/ContextLogger.js.map +1 -1
- package/lib/types/node/domain/ContextLogger.d.ts +2 -1
- package/package.json +9 -9
- package/src/node/domain/ContextLogger.spec.ts +123 -0
- package/src/node/domain/ContextLogger.ts +24 -5
- package/src/node/domain/DIContext.spec.ts +2 -0
|
@@ -4,17 +4,29 @@ import { logger as injectLogger } from "../fn/logger.js";
|
|
|
4
4
|
const LEVELS = levels();
|
|
5
5
|
export class ContextLogger {
|
|
6
6
|
#additionalProps;
|
|
7
|
+
#level;
|
|
7
8
|
#hooks;
|
|
8
9
|
#stack;
|
|
9
10
|
#logger;
|
|
10
|
-
constructor({ id, logger, dateStart = new Date(), level
|
|
11
|
+
constructor({ id, logger, dateStart = new Date(), level, maxStackSize = 30, additionalProps }) {
|
|
11
12
|
this.dateStart = dateStart;
|
|
12
13
|
this.id = id;
|
|
13
14
|
this.#logger = logger || injectLogger();
|
|
14
15
|
this.#additionalProps = additionalProps;
|
|
15
|
-
this.level = (LEVELS[level.toUpperCase()] || LEVELS.ALL);
|
|
16
|
+
this.level = (LEVELS[(level || this.#logger.level || "").toUpperCase()] || LEVELS.ALL);
|
|
16
17
|
this.maxStackSize = maxStackSize;
|
|
17
18
|
}
|
|
19
|
+
set level(level) {
|
|
20
|
+
if (typeof level === "string") {
|
|
21
|
+
this.#level = LEVELS[level.toUpperCase()];
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.#level = level;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
get level() {
|
|
28
|
+
return this.#level;
|
|
29
|
+
}
|
|
18
30
|
get hooks() {
|
|
19
31
|
return (this.#hooks = this.#hooks || new Hooks());
|
|
20
32
|
}
|
|
@@ -53,9 +65,12 @@ export class ContextLogger {
|
|
|
53
65
|
}
|
|
54
66
|
flush(stream = false) {
|
|
55
67
|
if (this.stack.length) {
|
|
68
|
+
const level = this.#logger.level;
|
|
69
|
+
this.#logger.level = this.#level.levelStr.toLowerCase();
|
|
56
70
|
this.stack.forEach(({ level, data }) => {
|
|
57
71
|
this.#logger[level](data);
|
|
58
72
|
});
|
|
73
|
+
this.#logger.level = level;
|
|
59
74
|
this.#stack = [];
|
|
60
75
|
}
|
|
61
76
|
if (stream) {
|
|
@@ -63,7 +78,7 @@ export class ContextLogger {
|
|
|
63
78
|
}
|
|
64
79
|
}
|
|
65
80
|
isLevelEnabled(otherLevel) {
|
|
66
|
-
return this
|
|
81
|
+
return this.#level.isLessThanOrEqualTo(otherLevel);
|
|
67
82
|
}
|
|
68
83
|
/**
|
|
69
84
|
* Return the duration between the time when LogIncomingRequest has handle the request and now.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContextLogger.js","sourceRoot":"","sources":["../../../../src/node/domain/ContextLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,aAAa,CAAC;AAClC,OAAO,EAAC,MAAM,EAAW,MAAM,cAAc,CAAC;AAG9C,OAAO,EAAC,MAAM,IAAI,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAUvD,MAAM,MAAM,GAA6B,MAAM,EAAE,CAAC;AAElD,MAAM,OAAO,aAAa;IAGf,gBAAgB,CAA2B;
|
|
1
|
+
{"version":3,"file":"ContextLogger.js","sourceRoot":"","sources":["../../../../src/node/domain/ContextLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,aAAa,CAAC;AAClC,OAAO,EAAC,MAAM,EAAW,MAAM,cAAc,CAAC;AAG9C,OAAO,EAAC,MAAM,IAAI,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAUvD,MAAM,MAAM,GAA6B,MAAM,EAAE,CAAC;AAElD,MAAM,OAAO,aAAa;IAGf,gBAAgB,CAA2B;IAGpD,MA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AI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;YAE9C,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;YAE1E,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAC,CAAC,CAAC;QAChD,CAAC;QAED,IAAI,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9F,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC;IACH,CAAC;CACF"}
|
|
@@ -13,8 +13,9 @@ export declare class ContextLogger {
|
|
|
13
13
|
readonly dateStart: Date;
|
|
14
14
|
readonly id: string;
|
|
15
15
|
maxStackSize: number;
|
|
16
|
-
level: LogLevel;
|
|
17
16
|
constructor({ id, logger, dateStart, level, maxStackSize, additionalProps }: ContextLoggerOptions);
|
|
17
|
+
set level(level: "debug" | "info" | "warn" | "error" | "off" | "all" | LogLevel);
|
|
18
|
+
get level(): "debug" | "info" | "warn" | "error" | "off" | "all" | LogLevel;
|
|
18
19
|
get hooks(): Hooks;
|
|
19
20
|
private get stack();
|
|
20
21
|
alterLog(cb: (data: any, level: "debug" | "info" | "warn" | "error" | "all", withRequest: boolean) => any): Hooks;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/di",
|
|
3
3
|
"description": "DI module for Ts.ED Framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.6.
|
|
5
|
+
"version": "8.6.1",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -30,12 +30,12 @@
|
|
|
30
30
|
"uuid": "^10.0.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@tsed/barrels": "8.6.
|
|
34
|
-
"@tsed/core": "8.6.
|
|
35
|
-
"@tsed/hooks": "8.6.
|
|
33
|
+
"@tsed/barrels": "8.6.1",
|
|
34
|
+
"@tsed/core": "8.6.1",
|
|
35
|
+
"@tsed/hooks": "8.6.1",
|
|
36
36
|
"@tsed/logger": "^7.0.1",
|
|
37
|
-
"@tsed/schema": "8.6.
|
|
38
|
-
"@tsed/typescript": "8.6.
|
|
37
|
+
"@tsed/schema": "8.6.1",
|
|
38
|
+
"@tsed/typescript": "8.6.1",
|
|
39
39
|
"cross-env": "7.0.3",
|
|
40
40
|
"eslint": "9.12.0",
|
|
41
41
|
"typescript": "5.4.5",
|
|
@@ -43,10 +43,10 @@
|
|
|
43
43
|
"webpack": "^5.75.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@tsed/core": ">=8.6.
|
|
47
|
-
"@tsed/hooks": ">=8.6.
|
|
46
|
+
"@tsed/core": ">=8.6.1",
|
|
47
|
+
"@tsed/hooks": ">=8.6.1",
|
|
48
48
|
"@tsed/logger": ">=7.0.1",
|
|
49
|
-
"@tsed/schema": ">=8.6.
|
|
49
|
+
"@tsed/schema": ">=8.6.1"
|
|
50
50
|
},
|
|
51
51
|
"peerDependenciesMeta": {
|
|
52
52
|
"@tsed/core": {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {levels, Logger} from "@tsed/logger";
|
|
2
|
+
|
|
1
3
|
import {ContextLogger} from "./ContextLogger.js";
|
|
2
4
|
|
|
3
5
|
function getIgnoreLogFixture(ignore: string[], url: string) {
|
|
@@ -102,6 +104,127 @@ describe("ContextLogger", () => {
|
|
|
102
104
|
time: expect.any(Date)
|
|
103
105
|
});
|
|
104
106
|
});
|
|
107
|
+
it("should create a new Context with initial log level (error) and change the request log level (debug)", () => {
|
|
108
|
+
const logger = new Logger();
|
|
109
|
+
logger.level = "error";
|
|
110
|
+
|
|
111
|
+
vi.spyOn(logger, "error").mockReturnValue(undefined as never);
|
|
112
|
+
vi.spyOn(logger, "debug").mockReturnValue(undefined as never);
|
|
113
|
+
vi.spyOn(logger, "info").mockReturnValue(undefined as never);
|
|
114
|
+
vi.spyOn(logger, "warn").mockReturnValue(undefined as never);
|
|
115
|
+
vi.spyOn(logger, "trace").mockReturnValue(undefined as never);
|
|
116
|
+
vi.spyOn(logger, "fatal").mockReturnValue(undefined as never);
|
|
117
|
+
|
|
118
|
+
const contextLogger = new ContextLogger({
|
|
119
|
+
event: {
|
|
120
|
+
request: {},
|
|
121
|
+
response: {}
|
|
122
|
+
},
|
|
123
|
+
logger,
|
|
124
|
+
id: "id",
|
|
125
|
+
dateStart: new Date("2019-01-01")
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
expect(logger.level).toEqual("ERROR");
|
|
129
|
+
expect(contextLogger.level).toEqual(levels().ERROR);
|
|
130
|
+
|
|
131
|
+
// WHEN
|
|
132
|
+
contextLogger.level = levels().DEBUG;
|
|
133
|
+
|
|
134
|
+
contextLogger.debug({test: "test"});
|
|
135
|
+
contextLogger.info({test: "test"});
|
|
136
|
+
contextLogger.warn({test: "test"});
|
|
137
|
+
contextLogger.error({test: "test"});
|
|
138
|
+
contextLogger.fatal({test: "test"});
|
|
139
|
+
contextLogger.trace({test: "test"});
|
|
140
|
+
|
|
141
|
+
contextLogger.flush();
|
|
142
|
+
|
|
143
|
+
// THEN
|
|
144
|
+
expect(logger.level).toEqual("ERROR");
|
|
145
|
+
|
|
146
|
+
expect(logger.info).toHaveBeenCalledWith({
|
|
147
|
+
duration: expect.any(Number),
|
|
148
|
+
reqId: "id",
|
|
149
|
+
test: "test",
|
|
150
|
+
time: expect.any(Date)
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(logger.debug).toHaveBeenCalledWith({
|
|
154
|
+
duration: expect.any(Number),
|
|
155
|
+
reqId: "id",
|
|
156
|
+
test: "test",
|
|
157
|
+
time: expect.any(Date)
|
|
158
|
+
});
|
|
159
|
+
expect(logger.warn).toHaveBeenCalledWith({
|
|
160
|
+
duration: expect.any(Number),
|
|
161
|
+
reqId: "id",
|
|
162
|
+
test: "test",
|
|
163
|
+
time: expect.any(Date)
|
|
164
|
+
});
|
|
165
|
+
expect(logger.error).toHaveBeenCalledWith({
|
|
166
|
+
duration: expect.any(Number),
|
|
167
|
+
reqId: "id",
|
|
168
|
+
test: "test",
|
|
169
|
+
time: expect.any(Date)
|
|
170
|
+
});
|
|
171
|
+
expect(logger.fatal).toHaveBeenCalledWith({
|
|
172
|
+
duration: expect.any(Number),
|
|
173
|
+
reqId: "id",
|
|
174
|
+
test: "test",
|
|
175
|
+
time: expect.any(Date)
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
it("should create a new Context with initial log level (error) and not change the request log level", () => {
|
|
179
|
+
const logger = new Logger();
|
|
180
|
+
logger.level = "error";
|
|
181
|
+
|
|
182
|
+
vi.spyOn(logger, "error").mockReturnValue(undefined as never);
|
|
183
|
+
vi.spyOn(logger, "debug").mockReturnValue(undefined as never);
|
|
184
|
+
vi.spyOn(logger, "info").mockReturnValue(undefined as never);
|
|
185
|
+
vi.spyOn(logger, "warn").mockReturnValue(undefined as never);
|
|
186
|
+
vi.spyOn(logger, "trace").mockReturnValue(undefined as never);
|
|
187
|
+
vi.spyOn(logger, "fatal").mockReturnValue(undefined as never);
|
|
188
|
+
|
|
189
|
+
const contextLogger = new ContextLogger({
|
|
190
|
+
event: {
|
|
191
|
+
request: {},
|
|
192
|
+
response: {}
|
|
193
|
+
},
|
|
194
|
+
logger,
|
|
195
|
+
id: "id",
|
|
196
|
+
dateStart: new Date("2019-01-01")
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
expect(contextLogger.level).toEqual(levels().ERROR);
|
|
200
|
+
|
|
201
|
+
// WHEN
|
|
202
|
+
contextLogger.debug({test: "test"});
|
|
203
|
+
contextLogger.info({test: "test"});
|
|
204
|
+
contextLogger.warn({test: "test"});
|
|
205
|
+
contextLogger.error({test: "test"});
|
|
206
|
+
contextLogger.fatal({test: "test"});
|
|
207
|
+
contextLogger.trace({test: "test"});
|
|
208
|
+
|
|
209
|
+
contextLogger.flush();
|
|
210
|
+
|
|
211
|
+
// THEN
|
|
212
|
+
expect(logger.debug).not.toHaveBeenCalled();
|
|
213
|
+
expect(logger.trace).not.toHaveBeenCalled();
|
|
214
|
+
expect(logger.info).not.toHaveBeenCalled();
|
|
215
|
+
expect(logger.error).toHaveBeenCalledWith({
|
|
216
|
+
duration: expect.any(Number),
|
|
217
|
+
reqId: "id",
|
|
218
|
+
test: "test",
|
|
219
|
+
time: expect.any(Date)
|
|
220
|
+
});
|
|
221
|
+
expect(logger.fatal).toHaveBeenCalledWith({
|
|
222
|
+
duration: expect.any(Number),
|
|
223
|
+
reqId: "id",
|
|
224
|
+
test: "test",
|
|
225
|
+
time: expect.any(Date)
|
|
226
|
+
});
|
|
227
|
+
});
|
|
105
228
|
it("should create a new Context and log all (with minimalRequestPicker)", () => {
|
|
106
229
|
const logger = {
|
|
107
230
|
info: vi.fn(),
|
|
@@ -20,21 +20,34 @@ export class ContextLogger {
|
|
|
20
20
|
readonly #additionalProps?: Record<string, unknown>;
|
|
21
21
|
|
|
22
22
|
maxStackSize: number;
|
|
23
|
-
level: LogLevel;
|
|
24
|
-
|
|
23
|
+
#level: LogLevel;
|
|
25
24
|
#hooks?: Hooks;
|
|
26
25
|
#stack?: any[];
|
|
27
26
|
#logger: any;
|
|
28
27
|
|
|
29
|
-
constructor({id, logger, dateStart = new Date(), level
|
|
28
|
+
constructor({id, logger, dateStart = new Date(), level, maxStackSize = 30, additionalProps}: ContextLoggerOptions) {
|
|
30
29
|
this.dateStart = dateStart;
|
|
31
30
|
this.id = id;
|
|
32
31
|
this.#logger = logger || injectLogger();
|
|
33
32
|
this.#additionalProps = additionalProps;
|
|
34
|
-
|
|
33
|
+
|
|
34
|
+
this.level = (LEVELS[(level || this.#logger.level || "").toUpperCase()] || LEVELS.ALL) as LogLevel;
|
|
35
|
+
|
|
35
36
|
this.maxStackSize = maxStackSize;
|
|
36
37
|
}
|
|
37
38
|
|
|
39
|
+
set level(level: "debug" | "info" | "warn" | "error" | "off" | "all" | LogLevel) {
|
|
40
|
+
if (typeof level === "string") {
|
|
41
|
+
this.#level = LEVELS[level.toUpperCase()];
|
|
42
|
+
} else {
|
|
43
|
+
this.#level = level;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get level() {
|
|
48
|
+
return this.#level;
|
|
49
|
+
}
|
|
50
|
+
|
|
38
51
|
get hooks() {
|
|
39
52
|
return (this.#hooks = this.#hooks || new Hooks());
|
|
40
53
|
}
|
|
@@ -83,10 +96,16 @@ export class ContextLogger {
|
|
|
83
96
|
|
|
84
97
|
public flush(stream = false) {
|
|
85
98
|
if (this.stack.length) {
|
|
99
|
+
const level = this.#logger.level;
|
|
100
|
+
|
|
101
|
+
this.#logger.level = this.#level.levelStr.toLowerCase();
|
|
102
|
+
|
|
86
103
|
this.stack.forEach(({level, data}: any) => {
|
|
87
104
|
this.#logger[level](data);
|
|
88
105
|
});
|
|
89
106
|
|
|
107
|
+
this.#logger.level = level;
|
|
108
|
+
|
|
90
109
|
this.#stack = [];
|
|
91
110
|
}
|
|
92
111
|
|
|
@@ -96,7 +115,7 @@ export class ContextLogger {
|
|
|
96
115
|
}
|
|
97
116
|
|
|
98
117
|
public isLevelEnabled(otherLevel: string | LogLevel) {
|
|
99
|
-
return this
|
|
118
|
+
return this.#level.isLessThanOrEqualTo(otherLevel);
|
|
100
119
|
}
|
|
101
120
|
|
|
102
121
|
/**
|
|
@@ -27,6 +27,7 @@ describe("DIContext", () => {
|
|
|
27
27
|
expect(context.env).toEqual("test");
|
|
28
28
|
expect(context.PLATFORM).toEqual("DI");
|
|
29
29
|
|
|
30
|
+
context.logger.level = "info";
|
|
30
31
|
context.logger.info("test");
|
|
31
32
|
|
|
32
33
|
expect(logger().info).toHaveBeenCalled();
|
|
@@ -53,6 +54,7 @@ describe("DIContext", () => {
|
|
|
53
54
|
expect(context.PLATFORM).toEqual("OTHER");
|
|
54
55
|
|
|
55
56
|
context.next();
|
|
57
|
+
context.logger.level = "info";
|
|
56
58
|
context.logger.info("test");
|
|
57
59
|
|
|
58
60
|
expect(logger().info).toHaveBeenCalled();
|