@pcg/core 1.0.0-alpha.0 → 1.0.0-alpha.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/dist/index.d.ts +21 -1189
- package/dist/index.js +76 -1867
- package/dist/index.js.map +1 -1
- package/package.json +16 -3
- package/.turbo/turbo-build.log +0 -15
- package/CHANGELOG.md +0 -7
- package/src/abstracts/index.ts +0 -3
- package/src/abstracts/nestjs-resource-service.ts +0 -154
- package/src/abstracts/nestjs-service.ts +0 -25
- package/src/configs/app.config.ts +0 -185
- package/src/configs/db.config.ts +0 -122
- package/src/configs/index.ts +0 -4
- package/src/configs/logger.config.ts +0 -62
- package/src/context/action-context.ts +0 -34
- package/src/context/current-user.ts +0 -49
- package/src/context/index.ts +0 -5
- package/src/context/platform-method-context.ts +0 -5
- package/src/context/service-method-context.ts +0 -47
- package/src/db/snake-naming.strategy.ts +0 -277
- package/src/enums/app-env.enum.ts +0 -36
- package/src/enums/app-mode.enum.ts +0 -5
- package/src/enums/app-server.enum.ts +0 -39
- package/src/enums/index.ts +0 -4
- package/src/enums/worker-mode.enum.ts +0 -11
- package/src/errors/access-denied.error.ts +0 -18
- package/src/errors/bad-request.error.ts +0 -9
- package/src/errors/forbidden.error.ts +0 -9
- package/src/errors/index.ts +0 -8
- package/src/errors/input-validation.error.ts +0 -16
- package/src/errors/nest-error.filter.ts +0 -70
- package/src/errors/nest-error.ts +0 -63
- package/src/errors/not-found.error.ts +0 -9
- package/src/errors/unauthorized.error.ts +0 -9
- package/src/exceptions/http-exception-response.ts +0 -34
- package/src/exceptions/http-exceptions.filter.ts +0 -95
- package/src/index.ts +0 -32
- package/src/jwt/extractors.ts +0 -80
- package/src/jwt/types.ts +0 -209
- package/src/logger/classes/logger-factory.ts +0 -54
- package/src/logger/classes/logger.ts +0 -340
- package/src/logger/classes/nest-system-logger.ts +0 -63
- package/src/logger/classes/typeorm-logger.ts +0 -83
- package/src/logger/index.ts +0 -20
- package/src/logger/logger.constants.ts +0 -24
- package/src/logger/logger.interfaces.ts +0 -98
- package/src/logger/logger.module.ts +0 -45
- package/src/logger/logger.providers.ts +0 -140
- package/src/logger/winston.tools.ts +0 -241
- package/src/middlewares/app.middleware.ts +0 -26
- package/src/middlewares/index.ts +0 -1
- package/src/modules/hooks/base-hook.ts +0 -64
- package/src/modules/hooks/decorators/on-hook.decorator.ts +0 -19
- package/src/modules/hooks/hooks.module.ts +0 -10
- package/src/modules/hooks/hooks.service.ts +0 -28
- package/src/modules/hooks/index.ts +0 -11
- package/src/modules/id/id.module.ts +0 -26
- package/src/modules/id/id.service.ts +0 -57
- package/src/modules/id/index.ts +0 -2
- package/src/modules/postgres-pubsub/index.ts +0 -3
- package/src/modules/postgres-pubsub/postgres-pubsub.module.ts +0 -14
- package/src/modules/postgres-pubsub/postgres-pubsub.ts +0 -461
- package/src/pagination/constants.ts +0 -9
- package/src/pagination/cursor/cursor-pagination.exception.ts +0 -16
- package/src/pagination/cursor/cursor-pagination.helpers.ts +0 -145
- package/src/pagination/cursor/cursor-pagination.input.ts +0 -96
- package/src/pagination/cursor/cursor-pagination.types.ts +0 -127
- package/src/pagination/index.ts +0 -9
- package/src/pagination/offset/offset-pagination.exception.ts +0 -15
- package/src/pagination/offset/offset-pagination.helpers.ts +0 -122
- package/src/pagination/offset/offset-pagination.input.ts +0 -30
- package/src/pagination/offset/offset-pagination.types.ts +0 -82
- package/src/pagination/tools.ts +0 -53
- package/src/tools/compose.ts +0 -92
- package/src/tools/convert-to-bigint.ts +0 -27
- package/src/tools/create-list-meta.ts +0 -64
- package/src/tools/define-statuses.ts +0 -15
- package/src/tools/env.ts +0 -139
- package/src/tools/fetch-total-with-query.ts +0 -48
- package/src/tools/generate-entity-id.ts +0 -23
- package/src/tools/get-request-language.ts +0 -13
- package/src/tools/is-object.ts +0 -10
- package/src/tools/postgres/locale-to-pg-collate.ts +0 -21
- package/src/tools/remove-undefined-properties.ts +0 -20
- package/src/tools/request-id.ts +0 -25
- package/src/tools/stringify-opts.ts +0 -20
- package/src/tools/typeorm/add-filter.ts +0 -164
- package/src/tools/typeorm/ensure-inner-join.ts +0 -36
- package/src/tools/typeorm/ensure-left-join.ts +0 -36
- package/src/tools/typeorm/is-alias-already-busy.ts +0 -25
- package/src/tools/wait.ts +0 -26
- package/src/types/express-request.ts +0 -8
- package/src/types/list-mehod-options.ts +0 -32
- package/src/types/list-meta.ts +0 -16
- package/src/types/maybe.ts +0 -2
- package/src/validation/index.ts +0 -1
- package/src/validation/validation-pipe.ts +0 -14
- package/tsconfig.lib.json +0 -9
- package/tsdown.config.ts +0 -15
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
;
|
|
2
|
-
import { isObject } from '#/tools/is-object.js';
|
|
3
|
-
import { ApolloError } from '@apollo/client/core';
|
|
4
|
-
import { HttpException } from '@nestjs/common';
|
|
5
|
-
import { GraphQLError } from 'graphql';
|
|
6
|
-
import { LogEntry, Logger as WinstonLogger } from 'winston';
|
|
7
|
-
|
|
8
|
-
import { BaseServiceMethodContext } from '#/context';
|
|
9
|
-
import { NestError } from '#/errors/index.js';
|
|
10
|
-
import { HttpExceptionResponse } from '#/exceptions/http-exception-response';
|
|
11
|
-
// import { getErrorMessages as getApolloErrorMessages } from '@deep/graphql-kit';
|
|
12
|
-
import type {
|
|
13
|
-
DestructuredError,
|
|
14
|
-
InfoObject, LoggerContext,
|
|
15
|
-
WinstonLogLevel,
|
|
16
|
-
} from '../logger.interfaces.js';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Logger with context for NestJS (from NestKit)
|
|
20
|
-
*/
|
|
21
|
-
export class Logger {
|
|
22
|
-
constructor(
|
|
23
|
-
private readonly winstonLogger: WinstonLogger,
|
|
24
|
-
private context: LoggerContext = {
|
|
25
|
-
},
|
|
26
|
-
) {}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Created child logger with specific context
|
|
30
|
-
* @param context - object with scope, action and other data
|
|
31
|
-
* @returns - new logger with context
|
|
32
|
-
* @example
|
|
33
|
-
* ```ts
|
|
34
|
-
* class SomeService {
|
|
35
|
-
* logger: Logger;
|
|
36
|
-
*
|
|
37
|
-
* constructor(
|
|
38
|
-
* @InjectLoggerFactory() private readonly loggerFactory: LoggerFactory,
|
|
39
|
-
* ) {
|
|
40
|
-
* // Create logger for this service
|
|
41
|
-
* this.winstonLogger = this.winstonLoggerFactory.create({
|
|
42
|
-
* scope: SomeService.name,
|
|
43
|
-
* })
|
|
44
|
-
* }
|
|
45
|
-
*
|
|
46
|
-
* // Create child logger for some method
|
|
47
|
-
* getOne(id: string) {
|
|
48
|
-
* const logger = this.winstonLogger.child({
|
|
49
|
-
* action: this.someMethod.name,
|
|
50
|
-
* id,
|
|
51
|
-
* });
|
|
52
|
-
* }
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
// https://www.npmjs.com/package/json-log#log2--logchilddata
|
|
56
|
-
child(context: LoggerContext): Logger {
|
|
57
|
-
return new Logger(this.winstonLogger, {
|
|
58
|
-
...this.context,
|
|
59
|
-
...context,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Created child logger for nest service method
|
|
65
|
-
* Automatically parse service method context and add it to logger context
|
|
66
|
-
* @param name - service method name
|
|
67
|
-
* @param ctx - service method context
|
|
68
|
-
* @param other - custom logger context
|
|
69
|
-
* @returns - new logger with context
|
|
70
|
-
*/
|
|
71
|
-
forMethod(name: string, ctx?: BaseServiceMethodContext, other?: LoggerContext) {
|
|
72
|
-
const context: LoggerContext = {
|
|
73
|
-
action: name,
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
if (ctx) {
|
|
77
|
-
Object.assign(context, {
|
|
78
|
-
requestId: ctx.requestId,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if (!ctx.user) {
|
|
82
|
-
throw new Error('User is required for strict method context. Use guards: @UseGuards(JwtAuthGuard) or @UseGuards(GraphQLJwtAuthGuard)');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
Object.assign(context, {
|
|
86
|
-
requestId: ctx.requestId,
|
|
87
|
-
userId: ctx.user.id,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (other) {
|
|
92
|
-
Object.assign(context, other);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return this.child(context);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Set data to logger context
|
|
100
|
-
* @param key - context data key (ex: 'firstName')
|
|
101
|
-
* @param value - context data value (ex: 'John')
|
|
102
|
-
*/
|
|
103
|
-
setContext(key: string, value: unknown): void;
|
|
104
|
-
setContext(context: LoggerContext): void;
|
|
105
|
-
setContext(...args: unknown[]): void {
|
|
106
|
-
if (
|
|
107
|
-
args.length === 2 &&
|
|
108
|
-
typeof args[0] === 'string' &&
|
|
109
|
-
typeof args[1] !== 'undefined'
|
|
110
|
-
) {
|
|
111
|
-
const key = args[0];
|
|
112
|
-
const value = args[1];
|
|
113
|
-
this.context[key] = value;
|
|
114
|
-
} else {
|
|
115
|
-
this.context = {
|
|
116
|
-
...this.context,
|
|
117
|
-
...(args[0] as LoggerContext),
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Get logger context data
|
|
124
|
-
* @returns - logger context
|
|
125
|
-
*/
|
|
126
|
-
getContext(): LoggerContext {
|
|
127
|
-
return structuredClone(this.context);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Write a 'log' level log.
|
|
132
|
-
*/
|
|
133
|
-
log(entry: LogEntry): void;
|
|
134
|
-
log(level: WinstonLogLevel, message: string, context?: LoggerContext): void;
|
|
135
|
-
log(...args: unknown[]): void {
|
|
136
|
-
if (isObject(args[0])) {
|
|
137
|
-
this.winstonLogger.log({
|
|
138
|
-
...this.context,
|
|
139
|
-
...(args[0] as LogEntry),
|
|
140
|
-
});
|
|
141
|
-
} else {
|
|
142
|
-
this.winstonLogger.log({
|
|
143
|
-
level: args[0] as WinstonLogLevel,
|
|
144
|
-
message: args[1] as string,
|
|
145
|
-
...this.context,
|
|
146
|
-
...(args[2] as LoggerContext),
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Write a 'info' level log.
|
|
153
|
-
*/
|
|
154
|
-
info(message: string, context?: LoggerContext): void;
|
|
155
|
-
info(infoObject: InfoObject): void;
|
|
156
|
-
info(...args: unknown[]): void {
|
|
157
|
-
if (typeof args[0] === 'string') {
|
|
158
|
-
this.winstonLogger.info({
|
|
159
|
-
message: args[0],
|
|
160
|
-
...this.context,
|
|
161
|
-
...(args[1] as LoggerContext),
|
|
162
|
-
});
|
|
163
|
-
} else {
|
|
164
|
-
this.winstonLogger.info({
|
|
165
|
-
...this.context,
|
|
166
|
-
...(args[0] as InfoObject),
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Write an 'error' level log.
|
|
173
|
-
*/
|
|
174
|
-
error(message: string, context?: LoggerContext): void;
|
|
175
|
-
error(message: string, error: Error, context?: LoggerContext): void;
|
|
176
|
-
error(error: NestError): void;
|
|
177
|
-
error(error: Error, context?: LoggerContext): void;
|
|
178
|
-
error(...args: unknown[]): void {
|
|
179
|
-
if (args[0] instanceof NestError) {
|
|
180
|
-
const err = args[0];
|
|
181
|
-
this.winstonLogger.error({
|
|
182
|
-
...this.context,
|
|
183
|
-
...(args[1] as LoggerContext),
|
|
184
|
-
...this.destructureError(err),
|
|
185
|
-
});
|
|
186
|
-
} else if (args[0] instanceof Error) {
|
|
187
|
-
const err = args[0];
|
|
188
|
-
this.winstonLogger.error({
|
|
189
|
-
...this.context,
|
|
190
|
-
...(args[1] as LoggerContext),
|
|
191
|
-
...this.destructureError(err),
|
|
192
|
-
});
|
|
193
|
-
} else if (typeof args[0] === 'string' && args[1] instanceof Error) {
|
|
194
|
-
const message = args[0];
|
|
195
|
-
const err = args[1];
|
|
196
|
-
this.winstonLogger.error({
|
|
197
|
-
message: message,
|
|
198
|
-
...this.context,
|
|
199
|
-
...(args[2] as LoggerContext),
|
|
200
|
-
error: this.destructureError(err),
|
|
201
|
-
});
|
|
202
|
-
} else if (typeof args[0] === 'string') {
|
|
203
|
-
const message = args[0];
|
|
204
|
-
this.winstonLogger.error({
|
|
205
|
-
message,
|
|
206
|
-
...this.context,
|
|
207
|
-
...(args[1] as LoggerContext),
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Creates plain object from error
|
|
214
|
-
* @see {@link DestructuredError}
|
|
215
|
-
*/
|
|
216
|
-
protected destructureError(error: unknown): DestructuredError {
|
|
217
|
-
const errorObject: DestructuredError = {
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
if (error instanceof NestError) {
|
|
221
|
-
errorObject.message = error.message;
|
|
222
|
-
errorObject.stack = error.stack;
|
|
223
|
-
errorObject.context = error.context;
|
|
224
|
-
errorObject.statusCode = error.httpStatusCode;
|
|
225
|
-
errorObject.errorKey = error.key;
|
|
226
|
-
if (error.cause instanceof Error) {
|
|
227
|
-
errorObject.cause = this.destructureError(error.cause);
|
|
228
|
-
}
|
|
229
|
-
} else if (error instanceof Error) {
|
|
230
|
-
errorObject.message = error.message;
|
|
231
|
-
errorObject.stack = error.stack;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (error instanceof HttpException) {
|
|
235
|
-
errorObject.statusCode = error.getStatus();
|
|
236
|
-
|
|
237
|
-
const response = error.getResponse() as HttpExceptionResponse;
|
|
238
|
-
|
|
239
|
-
if (isObject(response)) {
|
|
240
|
-
if (response.key) {
|
|
241
|
-
errorObject.errorKey = response.key;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (response.context) {
|
|
245
|
-
if (!errorObject.context) {
|
|
246
|
-
errorObject.context = {
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
errorObject.context = {
|
|
251
|
-
...errorObject.context,
|
|
252
|
-
...response.context,
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if (response.error) {
|
|
257
|
-
errorObject.cause = this.destructureError(response.error);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return errorObject;
|
|
262
|
-
} else if (error instanceof GraphQLError) {
|
|
263
|
-
const ctx = JSON.parse(JSON.stringify({
|
|
264
|
-
locations: error.locations,
|
|
265
|
-
positions: error.positions,
|
|
266
|
-
source: error.source,
|
|
267
|
-
}));
|
|
268
|
-
|
|
269
|
-
errorObject.context = {
|
|
270
|
-
...errorObject.context,
|
|
271
|
-
...ctx,
|
|
272
|
-
};
|
|
273
|
-
// } else if (error instanceof ApolloError) {
|
|
274
|
-
// const messages = getApolloErrorMessages(error);
|
|
275
|
-
// errorObject.message = messages.join('\n');
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
return errorObject;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Write a 'warn' level log.
|
|
283
|
-
*/
|
|
284
|
-
warn(message: string, context?: LoggerContext): void;
|
|
285
|
-
warn(infoObject: InfoObject): void;
|
|
286
|
-
warn(...args: unknown[]): void {
|
|
287
|
-
if (typeof args[0] === 'string') {
|
|
288
|
-
this.winstonLogger.warn({
|
|
289
|
-
message: args[0],
|
|
290
|
-
...this.context,
|
|
291
|
-
...(args[1] as LoggerContext),
|
|
292
|
-
});
|
|
293
|
-
} else {
|
|
294
|
-
this.winstonLogger.warn({
|
|
295
|
-
...this.context,
|
|
296
|
-
...(args[0] as InfoObject),
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Write a 'debug' level log.
|
|
303
|
-
*/
|
|
304
|
-
debug(message: string, context?: LoggerContext): void;
|
|
305
|
-
debug(infoObject: InfoObject): void;
|
|
306
|
-
debug(...args: unknown[]): void {
|
|
307
|
-
if (typeof args[0] === 'string') {
|
|
308
|
-
this.winstonLogger.debug({
|
|
309
|
-
message: args[0],
|
|
310
|
-
...this.context,
|
|
311
|
-
...(args[1] as LoggerContext),
|
|
312
|
-
});
|
|
313
|
-
} else {
|
|
314
|
-
this.winstonLogger.debug({
|
|
315
|
-
...this.context,
|
|
316
|
-
...(args[0] as InfoObject),
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Write a 'verbose' level log.
|
|
323
|
-
*/
|
|
324
|
-
verbose(message: string, context?: LoggerContext): void;
|
|
325
|
-
verbose(infoObject: InfoObject): void;
|
|
326
|
-
verbose(...args: unknown[]): void {
|
|
327
|
-
if (typeof args[0] === 'string') {
|
|
328
|
-
this.winstonLogger.verbose({
|
|
329
|
-
message: args[0],
|
|
330
|
-
...this.context,
|
|
331
|
-
...(args[1] as LoggerContext),
|
|
332
|
-
});
|
|
333
|
-
} else {
|
|
334
|
-
this.winstonLogger.verbose({
|
|
335
|
-
...this.context,
|
|
336
|
-
...(args[0] as InfoObject),
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
;
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
ConsoleLogger, Inject, Injectable, LogLevel as NestLogLevel,
|
|
5
|
-
} from '@nestjs/common';
|
|
6
|
-
import { isFunction, isPlainObject } from '@nestjs/common/utils/shared.utils';
|
|
7
|
-
|
|
8
|
-
import { NESTKIT_LOGGER_PROVIDER } from '../logger.constants.js';
|
|
9
|
-
import type { WinstonLogLevel } from '../logger.interfaces.js';
|
|
10
|
-
import { Logger } from './logger.js';
|
|
11
|
-
|
|
12
|
-
@Injectable()
|
|
13
|
-
export class NestSystemLogger extends ConsoleLogger {
|
|
14
|
-
@Inject(NESTKIT_LOGGER_PROVIDER) private readonly logger!: Logger;
|
|
15
|
-
|
|
16
|
-
protected printMessages(messages: unknown[], context?: string | undefined, logLevel?: NestLogLevel | undefined): void {
|
|
17
|
-
for (const message of messages) {
|
|
18
|
-
this.logger.log(this.getWinstonLogLevel(logLevel), this.stringifyMessage(message), {
|
|
19
|
-
scope: context,
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
protected stringifyMessage(message: unknown): string {
|
|
25
|
-
// If the message is a function, call it and re-resolve its value.
|
|
26
|
-
if (isFunction(message)) {
|
|
27
|
-
return this.stringifyMessage(message());
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (isPlainObject(message) || Array.isArray(message)) {
|
|
31
|
-
return JSON.stringify(
|
|
32
|
-
message,
|
|
33
|
-
(key, value) =>
|
|
34
|
-
typeof value === 'bigint' ? value.toString() : value,
|
|
35
|
-
2,
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return String(message);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private getWinstonLogLevel(level?: NestLogLevel): WinstonLogLevel {
|
|
43
|
-
switch (level) {
|
|
44
|
-
case 'log':
|
|
45
|
-
return 'info';
|
|
46
|
-
|
|
47
|
-
case 'verbose':
|
|
48
|
-
return 'verbose';
|
|
49
|
-
|
|
50
|
-
case 'debug':
|
|
51
|
-
return 'debug';
|
|
52
|
-
|
|
53
|
-
case 'warn':
|
|
54
|
-
return 'warn';
|
|
55
|
-
|
|
56
|
-
case 'error':
|
|
57
|
-
return 'error';
|
|
58
|
-
|
|
59
|
-
default:
|
|
60
|
-
return 'info';
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import type { LogLevel, LogMessage } from 'typeorm';
|
|
2
|
-
import { AbstractLogger } from 'typeorm/logger/AbstractLogger.js';
|
|
3
|
-
import type { LoggerOptions } from 'typeorm/logger/LoggerOptions.js';
|
|
4
|
-
|
|
5
|
-
import { type LoggerContext } from '../logger.interfaces.js';
|
|
6
|
-
import { Logger } from './logger.js';
|
|
7
|
-
|
|
8
|
-
export class TypeOrmLoggerOptions {
|
|
9
|
-
enabled?: boolean;
|
|
10
|
-
logger!: Logger;
|
|
11
|
-
levels?: LoggerOptions;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class TypeOrmLogger extends AbstractLogger {
|
|
15
|
-
logger: Logger;
|
|
16
|
-
|
|
17
|
-
constructor(opts: TypeOrmLoggerOptions) {
|
|
18
|
-
super(opts.enabled && opts.levels ? opts.levels : false);
|
|
19
|
-
|
|
20
|
-
this.logger = opts.logger;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Write log to specific output.
|
|
25
|
-
*/
|
|
26
|
-
protected writeLog(
|
|
27
|
-
level: LogLevel, // "query" | "schema" | "error" | "warn" | "info" | "log" | "migration";
|
|
28
|
-
logMessage: LogMessage | LogMessage[],
|
|
29
|
-
) {
|
|
30
|
-
const messages = this.prepareLogMessages(logMessage, {
|
|
31
|
-
highlightSql: false,
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
for (const message of messages) {
|
|
35
|
-
switch (message.type ?? level) {
|
|
36
|
-
case 'log':
|
|
37
|
-
case 'query':
|
|
38
|
-
case 'schema-build':
|
|
39
|
-
this.logger.debug(String(message.message), this.getCtx(message));
|
|
40
|
-
break;
|
|
41
|
-
|
|
42
|
-
case 'migration':
|
|
43
|
-
case 'info':
|
|
44
|
-
if (message.prefix) {
|
|
45
|
-
this.logger.info(`${message.prefix} ${message.message}`, this.getCtx(message));
|
|
46
|
-
} else {
|
|
47
|
-
this.logger.info(String(message.message), this.getCtx(message));
|
|
48
|
-
}
|
|
49
|
-
break;
|
|
50
|
-
|
|
51
|
-
case 'warn':
|
|
52
|
-
case 'query-slow':
|
|
53
|
-
if (message.prefix) {
|
|
54
|
-
this.logger.warn(`${message.prefix} ${message.message}`, this.getCtx(message));
|
|
55
|
-
} else {
|
|
56
|
-
this.logger.warn(String(message.message), this.getCtx(message));
|
|
57
|
-
}
|
|
58
|
-
break;
|
|
59
|
-
|
|
60
|
-
case 'error':
|
|
61
|
-
case 'query-error':
|
|
62
|
-
if (message.prefix) {
|
|
63
|
-
this.logger.error(`[${message.prefix}] ${message.message}`, this.getCtx(message));
|
|
64
|
-
} else {
|
|
65
|
-
this.logger.error(String(message.message), this.getCtx(message));
|
|
66
|
-
}
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private getCtx(message: LogMessage): LoggerContext {
|
|
73
|
-
const ctx = {
|
|
74
|
-
...message.additionalInfo,
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
if (Array.isArray(ctx.parameters)) {
|
|
78
|
-
ctx.parameters = message.parameters;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return ctx;
|
|
82
|
-
}
|
|
83
|
-
}
|
package/src/logger/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
;
|
|
2
|
-
export * from './classes/nest-system-logger.js';
|
|
3
|
-
|
|
4
|
-
export * from './classes/typeorm-logger.js';
|
|
5
|
-
|
|
6
|
-
export * from './classes/logger.js';
|
|
7
|
-
|
|
8
|
-
export * from './classes/logger-factory.js';
|
|
9
|
-
|
|
10
|
-
export * from './logger.constants.js';
|
|
11
|
-
|
|
12
|
-
export * from './logger.interfaces.js';
|
|
13
|
-
|
|
14
|
-
export * from './logger.module.js';
|
|
15
|
-
|
|
16
|
-
export * from './logger.providers.js';
|
|
17
|
-
|
|
18
|
-
export * from './winston.tools.js';
|
|
19
|
-
|
|
20
|
-
export * from '../configs/logger.config.js';
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Injection token for Winston logger module configuration options
|
|
3
|
-
*/
|
|
4
|
-
export const NESTKIT_LOGGER_MODULE_OPTIONS = 'NestKitLoggerModuleOptions';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Injection token for the Winston logger instance provider
|
|
8
|
-
*/
|
|
9
|
-
export const NESTKIT_WINSTON_LOGGER_PROVIDER = 'NestKitLogger';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Injection token for the main NestKit logger provider
|
|
13
|
-
*/
|
|
14
|
-
export const NESTKIT_LOGGER_PROVIDER = 'NestKitLogger';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Injection token for the NestJS system logger provider
|
|
18
|
-
*/
|
|
19
|
-
export const NESTKIT_SYSTEM_LOGGER_PROVIDER = 'NestKitSystemLogger';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Injection token for the logger factory provider
|
|
23
|
-
*/
|
|
24
|
-
export const NESTKIT_LOGGER_FACTORY_PROVIDER = 'NestKitLoggerFactory';
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { Type } from '@nestjs/common';
|
|
2
|
-
import { ModuleMetadata } from '@nestjs/common/interfaces';
|
|
3
|
-
import { LoggerOptions } from 'winston';
|
|
4
|
-
|
|
5
|
-
export type LoggerModuleOptions = LoggerOptions;
|
|
6
|
-
export type WinstonLogLevel = 'error' | 'warn' | 'info' | 'http' | 'verbose' | 'debug' | 'silly';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Logger logger context with specific data for logging
|
|
10
|
-
*/
|
|
11
|
-
export interface LoggerContext {
|
|
12
|
-
/**
|
|
13
|
-
* Scope where the message was logged
|
|
14
|
-
* @example 'VideoService'
|
|
15
|
-
*/
|
|
16
|
-
scope?: string;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Action where the message was logged
|
|
20
|
-
* @example 'createOutput'
|
|
21
|
-
*/
|
|
22
|
-
action?: string;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Request ID
|
|
26
|
-
* Special marker to identify request in logs
|
|
27
|
-
*/
|
|
28
|
-
requestId?: string;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Additional data
|
|
32
|
-
*/
|
|
33
|
-
[key: string]: unknown;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface InfoObject extends LoggerContext {
|
|
37
|
-
message: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface LoggerModuleOptionsFactory {
|
|
41
|
-
createLoggerModuleOptions():
|
|
42
|
-
| Promise<LoggerModuleOptions>
|
|
43
|
-
| LoggerModuleOptions;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface LoggerModuleAsyncOptions
|
|
47
|
-
extends Pick<ModuleMetadata, 'imports'> {
|
|
48
|
-
useFactory?: (
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
50
|
-
...args: any[]
|
|
51
|
-
) => Promise<LoggerModuleOptions> | LoggerModuleOptions;
|
|
52
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
-
inject?: any[];
|
|
54
|
-
useClass?: Type<LoggerModuleOptionsFactory>;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Error object that is destructured and logged by Logger
|
|
59
|
-
*/
|
|
60
|
-
export interface DestructuredError {
|
|
61
|
-
/**
|
|
62
|
-
* Error message text
|
|
63
|
-
*/
|
|
64
|
-
message?: string;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Error stack from Error object (`Error.stack`)
|
|
68
|
-
*/
|
|
69
|
-
stack?: string;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Error key from 'enum'
|
|
73
|
-
* @example 'NST_SA_EMAIL_NOT_UNIQUE'
|
|
74
|
-
*/
|
|
75
|
-
errorKey?: string;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* HTTP status code
|
|
79
|
-
* @example 400
|
|
80
|
-
*/
|
|
81
|
-
statusCode?: number;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Child error.
|
|
85
|
-
*/
|
|
86
|
-
cause?: DestructuredError;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Error context (additional data)
|
|
90
|
-
*/
|
|
91
|
-
context?: LoggerContext;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Response body if error is HTTPError
|
|
95
|
-
*/
|
|
96
|
-
body?: unknown;
|
|
97
|
-
}
|
|
98
|
-
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DynamicModule, Global, LoggerService, Module,
|
|
3
|
-
} from '@nestjs/common';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
LoggerModuleAsyncOptions,
|
|
7
|
-
LoggerModuleOptions,
|
|
8
|
-
} from './logger.interfaces.js';
|
|
9
|
-
import {
|
|
10
|
-
createLoggerAsyncProviders,
|
|
11
|
-
createLoggerProviders,
|
|
12
|
-
createNestLogger,
|
|
13
|
-
} from './logger.providers.js';
|
|
14
|
-
|
|
15
|
-
@Global()
|
|
16
|
-
@Module({
|
|
17
|
-
})
|
|
18
|
-
export class LoggerModule {
|
|
19
|
-
public static forRoot(options: LoggerModuleOptions): DynamicModule {
|
|
20
|
-
const providers = createLoggerProviders(options);
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
module: LoggerModule,
|
|
24
|
-
providers: providers,
|
|
25
|
-
exports: providers,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public static forRootAsync(
|
|
30
|
-
options: LoggerModuleAsyncOptions,
|
|
31
|
-
): DynamicModule {
|
|
32
|
-
const providers = createLoggerAsyncProviders(options);
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
module: LoggerModule,
|
|
36
|
-
imports: options.imports,
|
|
37
|
-
providers: providers,
|
|
38
|
-
exports: providers,
|
|
39
|
-
} as DynamicModule;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public static createLogger(options: LoggerModuleOptions): LoggerService {
|
|
43
|
-
return createNestLogger(options);
|
|
44
|
-
}
|
|
45
|
-
}
|