diff --git a/packages/logger/src/logger/ogma.ts b/packages/logger/src/logger/ogma.ts index 007d44eeb..1b42d394d 100644 --- a/packages/logger/src/logger/ogma.ts +++ b/packages/logger/src/logger/ogma.ts @@ -11,6 +11,15 @@ interface JSONLog { pid: number; application?: string; hostname: string; + requestId?: string; +} + +interface PrintMessageOptions { + level: LogLevel; + formattedLevel: string; + application?: string; + context?: string; + requestId?: string; } function isNil(val: any): boolean { @@ -56,26 +65,15 @@ export class Ogma { this.hostname = hostname(); } - private printMessage( - level: LogLevel, - formattedLevel: string, - message: any, - application?: string, - context?: string, - ): void { - if (level < LogLevel[this.options.logLevel]) { + private printMessage(message: any, options: PrintMessageOptions): void { + if (options.level < LogLevel[this.options.logLevel]) { return; } let logString = ''; if (this.options.json) { - logString = this.formatJSON(level, message, application, context); + logString = this.formatJSON(message, options); } else { - logString = this.formatStream( - formattedLevel, - message, - application, - context, - ); + logString = this.formatStream(message, options); } this.options.stream.write(`${logString}\n`); } @@ -114,10 +112,8 @@ export class Ogma { } private formatJSON( - level: LogLevel, message: any, - application?: string, - context?: string, + { application, requestId, context, level }: PrintMessageOptions, ): string { let json: Partial = { time: this.getTimestamp(), @@ -128,6 +124,7 @@ export class Ogma { } json.pid = this.pid; json.hostname = this.hostname; + json.requestId = requestId; context = context || this.options.context; if (context) { json.context = context; @@ -143,10 +140,8 @@ export class Ogma { } private formatStream( - formattedLevel: string, message: any, - application?: string, - context?: string, + { application, requestId, formattedLevel, context }: PrintMessageOptions, ): string { if (typeof message === 'object') { message = '\n' + JSON.stringify(message, this.circularReplacer(), 2); @@ -169,17 +164,23 @@ export class Ogma { this.options.stream, )} ` : ''; - const hostname = `${colorize( + requestId = + requestId && typeof requestId === 'string' + ? `${colorize( + requestId, + Color.WHITE, + this.options.color, + this.options.stream, + )} ` + : ''; + const hostname = colorize( this.wrapInBrackets(this.hostname), Color.MAGENTA, this.options.color, this.options.stream, - )}`; - return `${this.wrapInBrackets( - this.getTimestamp(), - )} ${hostname} ${application}${ - this.pid - } ${context}${formattedLevel}| ${message}`; + ); + const timestamp = this.wrapInBrackets(this.getTimestamp()); + return `${timestamp} ${hostname} ${application}${this.pid} ${requestId}${context}${formattedLevel}| ${message}`; } private getTimestamp(): string { @@ -192,15 +193,21 @@ export class Ogma { * @param message the message to print out. Can also be a JSON object * @param context the context to add the the log. Can be used to override class setting * @param application the application name to add to the log. Can be used to override class setting + * @param requestId id of request */ - public silly(message: any, context?: string, application?: string): void { - this.printMessage( - LogLevel.SILLY, - this.toColor(LogLevel.SILLY, Color.MAGENTA), - message, + public silly( + message: any, + context?: string, + application?: string, + requestId?: string, + ): void { + this.printMessage(message, { + level: LogLevel.SILLY, + formattedLevel: this.toColor(LogLevel.SILLY, Color.MAGENTA), application, context, - ); + requestId, + }); } /** @@ -209,15 +216,21 @@ export class Ogma { * @param message the message to print out. Can also be a JSON object * @param context the context to add the the log. Can be used to override class setting * @param application the application name to add to the log. Can be used to override class setting + * @param requestId id of request */ - public verbose(message: any, context?: string, application?: string): void { - this.printMessage( - LogLevel.VERBOSE, - this.toColor(LogLevel.VERBOSE, Color.GREEN), - message, + public verbose( + message: any, + context?: string, + application?: string, + requestId?: string, + ): void { + this.printMessage(message, { + level: LogLevel.VERBOSE, + formattedLevel: this.toColor(LogLevel.VERBOSE, Color.GREEN), application, context, - ); + requestId, + }); } /** @@ -226,15 +239,21 @@ export class Ogma { * @param message the message to print out. Can also be a JSON object * @param context the context to add the the log. Can be used to override class setting * @param application the application name to add to the log. Can be used to override class setting + * @param requestId id of request */ - public debug(message: any, context?: string, application?: string): void { - this.printMessage( - LogLevel.DEBUG, - this.toColor(LogLevel.DEBUG, Color.BLUE), - message, + public debug( + message: any, + context?: string, + application?: string, + requestId?: string, + ): void { + this.printMessage(message, { + level: LogLevel.DEBUG, + formattedLevel: this.toColor(LogLevel.DEBUG, Color.BLUE), application, context, - ); + requestId, + }); } /** @@ -243,15 +262,21 @@ export class Ogma { * @param message the message to print out. Can also be a JSON object * @param context the context to add the the log. Can be used to override class setting * @param application the application name to add to the log. Can be used to override class setting + * @param requestId id of request */ - public info(message: any, context?: string, application?: string): void { - this.printMessage( - LogLevel.INFO, - this.toColor(LogLevel.INFO, Color.CYAN), - message, + public info( + message: any, + context?: string, + application?: string, + requestId?: string, + ): void { + this.printMessage(message, { + level: LogLevel.INFO, + formattedLevel: this.toColor(LogLevel.INFO, Color.CYAN), application, context, - ); + requestId, + }); } /** @@ -259,15 +284,21 @@ export class Ogma { * @param message the message to print out. Can also be a JSON object * @param context the context to add the the log. Can be used to override class setting * @param application the application name to add to the log. Can be used to override class setting + * @param requestId id of request */ - public warn(message: any, context?: string, application?: string): void { - this.printMessage( - LogLevel.WARN, - this.toColor(LogLevel.WARN, Color.YELLOW), - message, + public warn( + message: any, + context?: string, + application?: string, + requestId?: string, + ): void { + this.printMessage(message, { + level: LogLevel.WARN, + formattedLevel: this.toColor(LogLevel.WARN, Color.YELLOW), application, context, - ); + requestId, + }); } /** @@ -276,15 +307,21 @@ export class Ogma { * @param message the message to print out. Can also be a JSON object * @param context the context to add the the log. Can be used to override class setting * @param application the application name to add to the log. Can be used to override class setting + * @param requestId id of request */ - public error(message: any, context?: string, application?: string): void { - this.printMessage( - LogLevel.ERROR, - this.toColor(LogLevel.ERROR, Color.RED), - message, + public error( + message: any, + context?: string, + application?: string, + requestId?: string, + ): void { + this.printMessage(message, { + level: LogLevel.ERROR, + formattedLevel: this.toColor(LogLevel.ERROR, Color.RED), application, context, - ); + requestId, + }); } /** @@ -293,15 +330,21 @@ export class Ogma { * @param message the message to print out. Can also be a JSON object * @param context the context to add the the log. Can be used to override class setting * @param application the application name to add to the log. Can be used to override class setting + * @param requestId id of request */ - public fatal(message: any, context?: string, application?: string): void { - this.printMessage( - LogLevel.FATAL, - this.toColor(LogLevel.FATAL, Color.RED), - message, + public fatal( + message: any, + context?: string, + application?: string, + requestId?: string, + ): void { + this.printMessage(message, { + level: LogLevel.FATAL, + formattedLevel: this.toColor(LogLevel.FATAL, Color.RED), application, context, - ); + requestId, + }); } /** @@ -309,14 +352,18 @@ export class Ogma { * * @param error The error that is to be printed. The name, message, and stack trace will be printed * at the Error, Warn, and Verbose log level respectively + * @param context string context of log + * @param application string name of appliction + * @param requestId string id of an request */ public printError( error: Error, context?: string, application?: string, + requestId?: string, ): void { - this.error(error.name, context, application); - this.warn(error.message, context, application); - this.verbose('\n' + error.stack, context, application); + this.error(error.name, context, application, requestId); + this.warn(error.message, context, application, requestId); + this.verbose('\n' + error.stack, context, application, requestId); } } diff --git a/packages/nestjs-module/package-lock.json b/packages/nestjs-module/package-lock.json new file mode 100644 index 000000000..6a5e20a6e --- /dev/null +++ b/packages/nestjs-module/package-lock.json @@ -0,0 +1,397 @@ +{ + "name": "@ogma/nestjs-module", + "version": "0.2.2", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@golevelup/nestjs-modules": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@golevelup/nestjs-modules/-/nestjs-modules-0.4.1.tgz", + "integrity": "sha512-v2GYGtJ8moTfhehavNFBwnHQ+QcgYHssxfqbJZcY4CUqB97E2sWmnAmtgscHDTvHqABOCaq2xvwfw4YTwZU2Ug==", + "requires": { + "shortid": "^2.2.14" + } + }, + "@nestjs/common": { + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-7.4.2.tgz", + "integrity": "sha512-gHfoUPPufBDLUdkBdhC60rgMwyiOKIFVmxCnFNHsMH1mrbt1DcXQD+5nefZm0XaLY20QeBoqZjJDc4KdvE444w==", + "dev": true, + "requires": { + "axios": "0.19.2", + "cli-color": "2.0.0", + "iterare": "1.2.1", + "tslib": "2.0.0", + "uuid": "8.3.0" + } + }, + "@nestjs/core": { + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-7.4.2.tgz", + "integrity": "sha512-zLWVY+qj5RRIuDaL2J6a7f/Tn8wPMYOUHDZhXdRZFqonuY96dH9yc6DU9cWyX7fnmCIAxqdu5B7qd/S7VPHxow==", + "dev": true, + "requires": { + "@nuxtjs/opencollective": "0.2.2", + "fast-safe-stringify": "2.0.7", + "iterare": "1.2.1", + "object-hash": "2.0.3", + "path-to-regexp": "3.2.0", + "tslib": "2.0.0", + "uuid": "8.3.0" + } + }, + "@nestjs/testing": { + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-7.4.2.tgz", + "integrity": "sha512-wnb0ApdTrJ03sg/1YiNNjjDIdf/f0izOea/5Bx9goKEJQPf8IW5AlV4yK9FB1uotED0mcjzp0awqzbfHfipP7w==", + "dev": true, + "requires": { + "optional": "0.1.4", + "tslib": "2.0.0" + } + }, + "@nuxtjs/opencollective": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@nuxtjs/opencollective/-/opencollective-0.2.2.tgz", + "integrity": "sha512-69gFVDs7mJfNjv9Zs5DFVD+pvBW+k1TaHSOqUWqAyTTfLcKI/EMYQgvEvziRd+zAFtUOoye6MfWh0qvinGISPw==", + "dev": true, + "requires": { + "chalk": "^2.4.1", + "consola": "^2.3.0", + "node-fetch": "^2.3.0" + } + }, + "@ogma/logger": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@ogma/logger/-/logger-0.2.2.tgz", + "integrity": "sha512-yn18E2ONUMv0n9y6P9TCx+hbLutp5n1K1omQVw5eNmrdbEaUt75auPiWtRn0vqN902mXEfxZpipHphgJvu9EEg==" + }, + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", + "dev": true + }, + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, + "requires": { + "color-convert": "^1.9.0" + } + }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "dev": true, + "requires": { + "follow-redirects": "1.5.10" + } + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "cli-color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/cli-color/-/cli-color-2.0.0.tgz", + "integrity": "sha512-a0VZ8LeraW0jTuCkuAGMNufareGHhyZU9z8OGsW0gXd1hZGi1SRuNRXdbGkraBBKnhyUhyebFWnRbp+dIn0f0A==", + "dev": true, + "requires": { + "ansi-regex": "^2.1.1", + "d": "^1.0.1", + "es5-ext": "^0.10.51", + "es6-iterator": "^2.0.3", + "memoizee": "^0.4.14", + "timers-ext": "^0.1.7" + } + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dev": true, + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", + "dev": true + }, + "consola": { + "version": "2.15.0", + "resolved": "https://registry.npmjs.org/consola/-/consola-2.15.0.tgz", + "integrity": "sha512-vlcSGgdYS26mPf7qNi+dCisbhiyDnrN1zaRbw3CSuc2wGOMEGGPsp46PdRG5gqXwgtJfjxDkxRNAgRPr1B77vQ==", + "dev": true + }, + "d": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz", + "integrity": "sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==", + "dev": true, + "requires": { + "es5-ext": "^0.10.50", + "type": "^1.0.1" + } + }, + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "es5-ext": { + "version": "0.10.53", + "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.53.tgz", + "integrity": "sha512-Xs2Stw6NiNHWypzRTY1MtaG/uJlwCk8kH81920ma8mvN8Xq1gsfhZvpkImLQArw8AHnv8MT2I45J3c0R8slE+Q==", + "dev": true, + "requires": { + "es6-iterator": "~2.0.3", + "es6-symbol": "~3.1.3", + "next-tick": "~1.0.0" + } + }, + "es6-iterator": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", + "integrity": "sha1-p96IkUGgWpSwhUQDstCg+/qY87c=", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "^0.10.35", + "es6-symbol": "^3.1.1" + } + }, + "es6-symbol": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.3.tgz", + "integrity": "sha512-NJ6Yn3FuDinBaBRWl/q5X/s4koRHBrgKAu+yGI6JCBeiu3qrcbJhwT2GeR/EXVfylRk8dpQVJoLEFhK+Mu31NA==", + "dev": true, + "requires": { + "d": "^1.0.1", + "ext": "^1.1.2" + } + }, + "es6-weak-map": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-weak-map/-/es6-weak-map-2.0.3.tgz", + "integrity": "sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA==", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "^0.10.46", + "es6-iterator": "^2.0.3", + "es6-symbol": "^3.1.1" + } + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "event-emitter": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", + "integrity": "sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, + "ext": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/ext/-/ext-1.4.0.tgz", + "integrity": "sha512-Key5NIsUxdqKg3vIsdw9dSuXpPCQ297y6wBjL30edxwPgt2E44WcWBZey/ZvUc6sERLTxKdyCu4gZFmUbk1Q7A==", + "dev": true, + "requires": { + "type": "^2.0.0" + }, + "dependencies": { + "type": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/type/-/type-2.1.0.tgz", + "integrity": "sha512-G9absDWvhAWCV2gmF1zKud3OyC61nZDwWvBL2DApaVFogI07CprggiQAOOjvp2NRjYWFzPyu7vwtDrQFq8jeSA==", + "dev": true + } + } + }, + "fast-safe-stringify": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz", + "integrity": "sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA==", + "dev": true + }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "dev": true, + "requires": { + "debug": "=3.1.0" + } + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true + }, + "is-promise": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.2.tgz", + "integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==", + "dev": true + }, + "iterare": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/iterare/-/iterare-1.2.1.tgz", + "integrity": "sha512-RKYVTCjAnRthyJes037NX/IiqeidgN1xc3j1RjFfECFp28A1GVwK9nA+i0rJPaHqSZwygLzRnFlzUuHFoWWy+Q==", + "dev": true + }, + "lru-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/lru-queue/-/lru-queue-0.1.0.tgz", + "integrity": "sha1-Jzi9nw089PhEkMVzbEhpmsYyzaM=", + "dev": true, + "requires": { + "es5-ext": "~0.10.2" + } + }, + "memoizee": { + "version": "0.4.14", + "resolved": "https://registry.npmjs.org/memoizee/-/memoizee-0.4.14.tgz", + "integrity": "sha512-/SWFvWegAIYAO4NQMpcX+gcra0yEZu4OntmUdrBaWrJncxOqAziGFlHxc7yjKVK2uu3lpPW27P27wkR82wA8mg==", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "^0.10.45", + "es6-weak-map": "^2.0.2", + "event-emitter": "^0.3.5", + "is-promise": "^2.1", + "lru-queue": "0.1", + "next-tick": "1", + "timers-ext": "^0.1.5" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "nanoid": { + "version": "2.1.11", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.11.tgz", + "integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==" + }, + "next-tick": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", + "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=", + "dev": true + }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==", + "dev": true + }, + "object-hash": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-2.0.3.tgz", + "integrity": "sha512-JPKn0GMu+Fa3zt3Bmr66JhokJU5BaNBIh4ZeTlaCBzrBsOeXzwcKKAK1tbLiPKgvwmPXsDvvLHoWh5Bm7ofIYg==", + "dev": true + }, + "optional": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/optional/-/optional-0.1.4.tgz", + "integrity": "sha512-gtvrrCfkE08wKcgXaVwQVgwEQ8vel2dc5DDBn9RLQZ3YtmtkBss6A2HY6BnJH4N/4Ku97Ri/SF8sNWE2225WJw==", + "dev": true + }, + "path-to-regexp": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-3.2.0.tgz", + "integrity": "sha512-jczvQbCUS7XmS7o+y1aEO9OBVFeZBQ1MDSEqmO7xSoPgOPoowY/SxLpZ6Vh97/8qHZOteiCKb7gkG9gA2ZUxJA==", + "dev": true + }, + "rxjs": { + "version": "6.6.2", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.2.tgz", + "integrity": "sha512-BHdBMVoWC2sL26w//BCu3YzKT4s2jip/WhwsGEDmeKYBhKDZeYezVUnHatYB7L85v5xs0BAQmg6BEYJEKxBabg==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + }, + "dependencies": { + "tslib": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", + "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==", + "dev": true + } + } + }, + "shortid": { + "version": "2.2.15", + "resolved": "https://registry.npmjs.org/shortid/-/shortid-2.2.15.tgz", + "integrity": "sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==", + "requires": { + "nanoid": "^2.1.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + }, + "timers-ext": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/timers-ext/-/timers-ext-0.1.7.tgz", + "integrity": "sha512-b85NUNzTSdodShTIbky6ZF02e8STtVVfD+fu4aXXShEELpozH+bCpJLYMPZbsABN2wDH7fJpqIoXxJpzbf0NqQ==", + "dev": true, + "requires": { + "es5-ext": "~0.10.46", + "next-tick": "1" + } + }, + "tslib": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.0.0.tgz", + "integrity": "sha512-lTqkx847PI7xEDYJntxZH89L2/aXInsyF2luSafe/+0fHOMjlBNXdH6th7f70qxLDhul7KZK0zC8V5ZIyHl0/g==", + "dev": true + }, + "type": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz", + "integrity": "sha512-+5nt5AAniqsCnu2cEQQdpzCAh33kVx8n0VoFidKpB1dVVLAN/F+bgVOqOJqOnEnrhp222clB5p3vUlD+1QAnfg==", + "dev": true + }, + "uuid": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.0.tgz", + "integrity": "sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ==", + "dev": true + } + } +} diff --git a/packages/nestjs-module/package.json b/packages/nestjs-module/package.json index b442a1e09..b880d7866 100644 --- a/packages/nestjs-module/package.json +++ b/packages/nestjs-module/package.json @@ -38,6 +38,7 @@ "peerDependencies": { "@nestjs/common": "^7.0.0", "@nestjs/core": "^7.0.0", + "@nestjs/microservices": "^7.0.0", "reflect-metadata": "^0.1.0", "rxjs": "^6.0.0" }, diff --git a/packages/nestjs-module/src/decorators/ogma-logger.decorator.ts b/packages/nestjs-module/src/decorators/ogma-logger.decorator.ts index 573781e0e..a4d3227bd 100644 --- a/packages/nestjs-module/src/decorators/ogma-logger.decorator.ts +++ b/packages/nestjs-module/src/decorators/ogma-logger.decorator.ts @@ -1,6 +1,18 @@ import { Inject } from '@nestjs/common'; -import { createProviderToken } from '../ogma.provider'; +import { + createProviderToken, + createRequestScopedProviderToken, +} from '../ogma.provider'; import { Type } from '../interfaces'; export const OgmaLogger = (topic: string | (() => any) | Type) => Inject(createProviderToken(typeof topic === 'function' ? topic.name : topic)); + +export const OgmaLoggerRequestScoped = ( + topic: string | (() => any) | Type, +) => + Inject( + createRequestScopedProviderToken( + typeof topic === 'function' ? topic.name : topic, + ), + ); diff --git a/packages/nestjs-module/src/interceptor/ogma.interceptor.ts b/packages/nestjs-module/src/interceptor/ogma.interceptor.ts index c63a74ab6..fd8f817ff 100644 --- a/packages/nestjs-module/src/interceptor/ogma.interceptor.ts +++ b/packages/nestjs-module/src/interceptor/ogma.interceptor.ts @@ -36,6 +36,10 @@ export class OgmaInterceptor implements NestInterceptor { const startTime = Date.now(); const options = { ...this.options, json: this.json, color: this.color }; let logObject: string | LogObject; + + const requestId = this.generateRequestId(); + this.delegate.setRequestId(context, requestId); + return next.handle().pipe( tap( (data) => { @@ -46,7 +50,7 @@ export class OgmaInterceptor implements NestInterceptor { startTime, options, ); - this.log(logObject, context); + this.log(logObject, context, requestId); } }, (err) => { @@ -57,7 +61,7 @@ export class OgmaInterceptor implements NestInterceptor { startTime, options, ); - this.log(logObject, context); + this.log(logObject, context, requestId); } }, ), @@ -86,10 +90,21 @@ export class OgmaInterceptor implements NestInterceptor { } } - public log(logObject: string | LogObject, context: ExecutionContext): void { + public log( + logObject: string | LogObject, + context: ExecutionContext, + requestId?: string, + ): void { this.service.info( logObject, `${context.getClass().name}#${context.getHandler().name}`, + requestId, ); } + + public generateRequestId(): string { + const time = Date.now().toString(); + const randomNumbers = Math.floor(Math.random() * (1000 - 100) + 100); + return time + randomNumbers.toString(); + } } diff --git a/packages/nestjs-module/src/interceptor/providers/abstract-interceptor.service.ts b/packages/nestjs-module/src/interceptor/providers/abstract-interceptor.service.ts index fb00825a6..8126ce451 100644 --- a/packages/nestjs-module/src/interceptor/providers/abstract-interceptor.service.ts +++ b/packages/nestjs-module/src/interceptor/providers/abstract-interceptor.service.ts @@ -61,6 +61,8 @@ export abstract class AbstractInterceptorService implements InterceptorService { abstract getCallPoint(context: ExecutionContext): string; + abstract setRequestId(context: ExecutionContext, requestId: string): void; + protected wrapInColor(status: number): string { let statusString: string; if (this.isBetween(status, 100, 300)) { diff --git a/packages/nestjs-module/src/interceptor/providers/delegator.service.ts b/packages/nestjs-module/src/interceptor/providers/delegator.service.ts index 93a1d9d30..fe89eb663 100644 --- a/packages/nestjs-module/src/interceptor/providers/delegator.service.ts +++ b/packages/nestjs-module/src/interceptor/providers/delegator.service.ts @@ -15,6 +15,23 @@ export class DelegatorService { private readonly gqlParser: GqlInterceptorService, ) {} + setRequestId(context: ExecutionContext, requestId: string): void { + switch (context.getType()) { + case 'rpc': + this.rpcParser.setRequestId(context, requestId); + break; + case 'http': + this.httpParser.setRequestId(context, requestId); + break; + case 'graphql': + this.gqlParser.setRequestId(context, requestId); + break; + case 'ws': + this.wsParser.setRequestId(context, requestId); + break; + } + } + getContextSuccessString( data: any, context: ExecutionContext, diff --git a/packages/nestjs-module/src/interceptor/providers/noop-interceptor.service.ts b/packages/nestjs-module/src/interceptor/providers/noop-interceptor.service.ts index 5802b885e..94507167d 100644 --- a/packages/nestjs-module/src/interceptor/providers/noop-interceptor.service.ts +++ b/packages/nestjs-module/src/interceptor/providers/noop-interceptor.service.ts @@ -22,4 +22,8 @@ export class NoopInterceptorService extends AbstractInterceptorService { getProtocol(): string { return 'protocol'; } + + setRequestId(): void { + return; + } } diff --git a/packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts b/packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts new file mode 100644 index 000000000..e75d34d4a --- /dev/null +++ b/packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts @@ -0,0 +1,3 @@ +export interface OgmaProviederOptions { + addRequestId: boolean; +} diff --git a/packages/nestjs-module/src/ogma.constants.ts b/packages/nestjs-module/src/ogma.constants.ts index 785177c54..d8444e9fb 100644 --- a/packages/nestjs-module/src/ogma.constants.ts +++ b/packages/nestjs-module/src/ogma.constants.ts @@ -9,6 +9,7 @@ export const OGMA_INTERCEPTOR_OPTIONS = 'OGMA_INTERCEPTOR_OPTIONS'; export const OGMA_SERVICE_OPTIONS = 'OGMA_SERVICE_OPTIONS'; export const OGMA_INTERCEPTOR_SKIP = 'OGMA_INTERCEPTOR_SKIP'; export const OGMA_SERVICE_TOKEN = 'OGMA_SERVICE'; +export const OGMA_REQUEST_SCOPED_SERVICE_TOKEN = 'OGMA_REQUEST_SCOPED_SERVICE'; export const OGMA_INTERCEPTOR_PROVIDERS = [OGMA_INTERCEPTOR_OPTIONS, Reflector]; export const OgmaInterceptorProviderError = diff --git a/packages/nestjs-module/src/ogma.module.ts b/packages/nestjs-module/src/ogma.module.ts index d495d68c3..d54fa3927 100644 --- a/packages/nestjs-module/src/ogma.module.ts +++ b/packages/nestjs-module/src/ogma.module.ts @@ -1,8 +1,12 @@ import { AsyncModuleConfig } from '@golevelup/nestjs-modules'; import { DynamicModule, Module, Provider } from '@nestjs/common'; import { OgmaModuleOptions, Type } from './interfaces'; -import { createLoggerProviders } from './ogma.provider'; +import { + createLoggerProviders, + createRequestScopedLoggerProviders, +} from './ogma.provider'; import { OgmaCoreModule } from './ogma-core.module'; +import { OgmaProviederOptions } from './interfaces/ogma-provieder-options.interface'; @Module({ /* imports: [OgmaCoreModule.Deferred], @@ -24,9 +28,14 @@ export class OgmaModule { * Original options from the `forRoot` or `forRootAsync` options are merged with new options * * @param context string context for the OgmaService to use in logging + * @param options object options in creation of OgmaService + * @param options.addRequestId boolean if logger should add requestId to each log */ - static forFeature(context: string | (() => any) | Type): DynamicModule { - const providers: Provider[] = createLoggerProviders(context); + static forFeature( + context: string | (() => any) | Type, + options: OgmaProviederOptions = { addRequestId: false }, + ): DynamicModule { + const providers: Provider[] = this.createProviders(context, options); return { imports: [OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0)], module: OgmaModule, @@ -34,4 +43,15 @@ export class OgmaModule { exports: providers, }; } + + private static createProviders( + context: string | (() => any) | Type, + options: OgmaProviederOptions = { addRequestId: false }, + ) { + if (options.addRequestId) { + return createRequestScopedLoggerProviders(context); + } + + return createLoggerProviders(context); + } } diff --git a/packages/nestjs-module/src/ogma.provider.ts b/packages/nestjs-module/src/ogma.provider.ts index 72e87a4fa..54bc82324 100644 --- a/packages/nestjs-module/src/ogma.provider.ts +++ b/packages/nestjs-module/src/ogma.provider.ts @@ -3,12 +3,14 @@ import { ExecutionContext, NestInterceptor, Provider, + Scope, } from '@nestjs/common'; -import { Reflector } from '@nestjs/core'; +import { ModuleRef, Reflector } from '@nestjs/core'; import { Ogma, OgmaOptions } from '@ogma/logger'; import { Observable } from 'rxjs'; import { OGMA_INSTANCE, + OGMA_REQUEST_SCOPED_SERVICE_TOKEN, OGMA_SERVICE_TOKEN, OgmaInterceptorProviderError, } from './ogma.constants'; @@ -24,6 +26,8 @@ import { Type, } from './interfaces'; import { OgmaInterceptor } from './interceptor/ogma.interceptor'; +import { CONTEXT } from '@nestjs/microservices'; +import { RequestContextHost } from '@nestjs/microservices/context/request-context-host'; export class NoopInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable { @@ -89,6 +93,10 @@ export function createProviderToken(topic: string): string { return OGMA_SERVICE_TOKEN + ':' + topic; } +export function createRequestScopedProviderToken(topic: string): string { + return OGMA_REQUEST_SCOPED_SERVICE_TOKEN + ':' + topic; +} + export function createLoggerProviders( topic: string | (() => any) | Type, ): Provider[] { @@ -105,6 +113,26 @@ export function createLoggerProviders( ]; } +export function createRequestScopedLoggerProviders( + topic: string | (() => any) | Type, +): Provider[] { + topic = typeof topic === 'function' ? topic.name : topic; + const token = createRequestScopedProviderToken(topic); + return [ + { + inject: [OGMA_INSTANCE, CONTEXT], + provide: token, + scope: Scope.REQUEST, + useFactory: ( + ogmaInstance: Ogma, + requestContext: RequestContextHost, + ): OgmaService => { + return new OgmaService(ogmaInstance, topic as string, requestContext); + }, + }, + ]; +} + export const interceptorProviderFactory = ( type: 'http' | 'gql' | 'ws' | 'rpc', backupClass: Type, diff --git a/packages/nestjs-module/src/ogma.service.ts b/packages/nestjs-module/src/ogma.service.ts index 7f18ff712..f08eccce4 100644 --- a/packages/nestjs-module/src/ogma.service.ts +++ b/packages/nestjs-module/src/ogma.service.ts @@ -1,6 +1,8 @@ import { Injectable, LoggerService, Optional } from '@nestjs/common'; import { Ogma } from '@ogma/logger'; import { InjectOgma, InjectOgmaContext } from './decorators'; +import { RequestContextHost } from '@nestjs/microservices/context/request-context-host'; + @Injectable() export class OgmaService implements LoggerService { private readonly context?: string; @@ -23,91 +25,118 @@ export class OgmaService implements LoggerService { constructor( @InjectOgma() ogma?: Ogma, @Optional() @InjectOgmaContext() context?: string, + @Optional() private readonly requestContext?: RequestContextHost, ) { this.context = context || ''; this.ogma = ogma ?? new Ogma(); } + private getRequestId( + requestContext: RequestContextHost | Record, + ): string | undefined { + if ( + requestContext instanceof RequestContextHost || + typeof requestContext.getContext !== 'undefined' + ) { + return requestContext.getContext().requestId; + } + return requestContext.requestId; + } + /** * use Ogma to log at the INFO level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public info(message: any, context?: string): void { - this.printMessage(message, 'info', context); + public info(message: any, context?: string, requestId?: string): void { + this.printMessage(message, 'info', context, requestId); } /** * use Ogma to log at the ERROR level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public error(message: any, context?: string): void { - this.printMessage(message, 'error', context); + public error(message: any, context?: string, requestId?: string): void { + this.printMessage(message, 'error', context, requestId); } /** * use Ogma to log at the WARN level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public warn(message: any, context?: string): void { - this.printMessage(message, 'warn', context); + public warn(message: any, context?: string, requestId?: string): void { + this.printMessage(message, 'warn', context, requestId); } /** * use Ogma to log at the DBEUG level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public debug(message: any, context?: string): void { - this.printMessage(message, 'debug', context); + public debug(message: any, context?: string, requestId?: string): void { + this.printMessage(message, 'debug', context, requestId); } /** * use Ogma to log at the FATAL level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public fatal(message: any, context?: string): void { - this.printMessage(message, 'fatal', context); + public fatal(message: any, context?: string, requestId?: string): void { + this.printMessage(message, 'fatal', context, requestId); } /** * use Ogma to log at the SILLY level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public silly(message: any, context?: string): void { - this.printMessage(message, 'silly', context); + public silly(message: any, context?: string, requestId?: string): void { + this.printMessage(message, 'silly', context, requestId); } /** * use Ogma to log at the VERBOSE level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public verbose(message: any, context?: string): void { - this.printMessage(message, 'verbose', context); + public verbose(message: any, context?: string, requestId?: string): void { + this.printMessage(message, 'verbose', context, requestId); } /** * A predefined method for printing errors to the Ogma instance * @param error The error to print. Should be an Error or Exception object * @param context Optional context if you want to change what the original context was + * @param requestId Optional id of an request */ - public printError(error: Error, context?: string): void { - this.printMessage('', 'error', context); - context = context ?? this.context; - this.ogma.printError(error, context); + public printError(error: Error, context?: string, requestId?: string): void { + this.printMessage('', 'error', context, requestId); + if (this.requestContext && this.context) { + requestId = this.getRequestId(this.requestContext); + } + this.ogma.printError(error, context, undefined, requestId); } private printMessage( message: any, levelString: Exclude, context?: string, + requestId?: string, ): void { context = context ?? this.context; - this.ogma[levelString](message, context); + if (this.requestContext && this.context) { + requestId = this.getRequestId(this.requestContext); + } + this.ogma[levelString](message, context, undefined, requestId); } } diff --git a/packages/platform-express/src/express-interceptor.service.ts b/packages/platform-express/src/express-interceptor.service.ts index 024fddf97..b82232813 100644 --- a/packages/platform-express/src/express-interceptor.service.ts +++ b/packages/platform-express/src/express-interceptor.service.ts @@ -45,6 +45,11 @@ export class ExpressParser extends AbstractInterceptorService { return `HTTP/${this.getHttpMajor(req)}.${this.getHttpMinor(req)}`; } + setRequestId(context: ExecutionContext, requestId: string): void { + const req = this.getRequest(context) as any; + req.requestId = requestId; + } + getRequest(context: ExecutionContext): Request { return context.switchToHttp().getRequest(); } diff --git a/packages/platform-fastify/src/fastify-interceptor.service.ts b/packages/platform-fastify/src/fastify-interceptor.service.ts index f1ce76289..6f198502f 100644 --- a/packages/platform-fastify/src/fastify-interceptor.service.ts +++ b/packages/platform-fastify/src/fastify-interceptor.service.ts @@ -20,6 +20,11 @@ export class FastifyParser extends AbstractInterceptorService { return req.raw.method || 'GET'; } + setRequestId(context: ExecutionContext, requestId: string) { + const req = this.getRequest(context) as any; + req.requestId = requestId; + } + getProtocol(context: ExecutionContext): string { const req = this.getRequest(context); return `HTTP/${req.raw.httpVersionMajor}.${req.raw.httpVersionMinor}`; diff --git a/packages/platform-grpc/package-lock.json b/packages/platform-grpc/package-lock.json new file mode 100644 index 000000000..f8abbeaef --- /dev/null +++ b/packages/platform-grpc/package-lock.json @@ -0,0 +1,93 @@ +{ + "name": "@ogma/platform-grpc", + "version": "0.2.2", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + }, + "rimraf": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", + "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", + "requires": { + "glob": "^7.1.3" + } + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + } + } +} diff --git a/packages/platform-grpc/src/grpc-interceptor.service.ts b/packages/platform-grpc/src/grpc-interceptor.service.ts index a50926a56..bd9499b2c 100644 --- a/packages/platform-grpc/src/grpc-interceptor.service.ts +++ b/packages/platform-grpc/src/grpc-interceptor.service.ts @@ -30,7 +30,16 @@ export class GrpcParser extends AbstractInterceptorService { return inColor ? this.wrapInColor(status) : status.toString(); } + setRequestId(context: ExecutionContext, requestId): void { + const grpcContext = this.getGrpcContext(context); + grpcContext.requestId = requestId; + } + private getData(context: ExecutionContext) { return context.switchToRpc().getData(); } + + private getGrpcContext(context: ExecutionContext) { + return context.switchToRpc().getContext(); + } } diff --git a/packages/platform-kafka/src/kafka-interceptor.service.ts b/packages/platform-kafka/src/kafka-interceptor.service.ts index 6381660a5..f4919bf42 100644 --- a/packages/platform-kafka/src/kafka-interceptor.service.ts +++ b/packages/platform-kafka/src/kafka-interceptor.service.ts @@ -30,6 +30,11 @@ export class KafkaParser extends AbstractInterceptorService { return inColor ? this.wrapInColor(status) : status.toString(); } + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getClient(context: ExecutionContext): KafkaContext { return context.switchToRpc().getContext(); } diff --git a/packages/platform-mqtt/src/mqtt-interceptor.service.ts b/packages/platform-mqtt/src/mqtt-interceptor.service.ts index 0984cbf14..827e737a8 100644 --- a/packages/platform-mqtt/src/mqtt-interceptor.service.ts +++ b/packages/platform-mqtt/src/mqtt-interceptor.service.ts @@ -35,6 +35,11 @@ export class MqttParser extends AbstractInterceptorService { return 'mqtt'; } + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getClient(context: ExecutionContext): MqttContext { return context.switchToRpc().getContext(); } diff --git a/packages/platform-nats/src/nats-interceptor.service.ts b/packages/platform-nats/src/nats-interceptor.service.ts index ba5f328ff..fe5fc657e 100644 --- a/packages/platform-nats/src/nats-interceptor.service.ts +++ b/packages/platform-nats/src/nats-interceptor.service.ts @@ -30,6 +30,11 @@ export class NatsParser extends AbstractInterceptorService { return inColor ? this.wrapInColor(status) : status.toString(); } + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getClient(context: ExecutionContext) { return context.switchToRpc().getContext(); } diff --git a/packages/platform-rabbitmq/src/rabbitmq-interceptor.service.ts b/packages/platform-rabbitmq/src/rabbitmq-interceptor.service.ts index 504d1f293..cb4e48410 100644 --- a/packages/platform-rabbitmq/src/rabbitmq-interceptor.service.ts +++ b/packages/platform-rabbitmq/src/rabbitmq-interceptor.service.ts @@ -30,6 +30,11 @@ export class RabbitMqParser extends AbstractInterceptorService { return inColor ? this.wrapInColor(status) : status.toString(); } + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getClient(context: ExecutionContext): RmqContext { return context.switchToRpc().getContext(); } diff --git a/packages/platform-redis/src/redis-interceptor.service.ts b/packages/platform-redis/src/redis-interceptor.service.ts index 8132ba8cc..fb039b54e 100644 --- a/packages/platform-redis/src/redis-interceptor.service.ts +++ b/packages/platform-redis/src/redis-interceptor.service.ts @@ -1,6 +1,7 @@ import { ExecutionContext, Injectable } from '@nestjs/common'; import { PATTERN_METADATA } from '@nestjs/microservices/constants'; import { AbstractInterceptorService } from '@ogma/nestjs-module'; +import { RedisContext } from '@nestjs/microservices'; @Injectable() export class RedisParser extends AbstractInterceptorService { @@ -32,7 +33,16 @@ export class RedisParser extends AbstractInterceptorService { return inColor ? this.wrapInColor(status) : status.toString(); } + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getData(context: ExecutionContext): any { return context.switchToRpc().getData(); } + + private getClient(context: ExecutionContext): any { + return context.switchToRpc().getContext(); + } } diff --git a/packages/platform-socket.io/src/socket-io-interceptor.service.ts b/packages/platform-socket.io/src/socket-io-interceptor.service.ts index 87d3a7e20..3150d6fee 100644 --- a/packages/platform-socket.io/src/socket-io-interceptor.service.ts +++ b/packages/platform-socket.io/src/socket-io-interceptor.service.ts @@ -31,6 +31,11 @@ export class SocketIOParser extends AbstractInterceptorService { return inColor ? this.wrapInColor(status) : status.toString(); } + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getClient(context: ExecutionContext): Socket { return context.switchToWs().getClient(); } diff --git a/packages/platform-tcp/src/tcp-interceptor.service.ts b/packages/platform-tcp/src/tcp-interceptor.service.ts index 9333fc697..fbaa16727 100644 --- a/packages/platform-tcp/src/tcp-interceptor.service.ts +++ b/packages/platform-tcp/src/tcp-interceptor.service.ts @@ -35,6 +35,11 @@ export class TcpParser extends AbstractInterceptorService { return client.getSocketRef().socket.remoteFamily; } + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getClient(context: ExecutionContext): TcpContext { return context.switchToRpc().getContext(); } diff --git a/packages/platform-ws/src/ws-interceptor.service.ts b/packages/platform-ws/src/ws-interceptor.service.ts index 5b0f8a171..a7c570b12 100644 --- a/packages/platform-ws/src/ws-interceptor.service.ts +++ b/packages/platform-ws/src/ws-interceptor.service.ts @@ -29,6 +29,12 @@ export class WsParser extends AbstractInterceptorService { const status = error ? 500 : 200; return inColor ? this.wrapInColor(status) : status.toString(); } + + setRequestId(context: ExecutionContext, requestId: string): void { + const client = this.getClient(context) as any; + client.requestId = requestId; + } + private getClient(context: ExecutionContext): WebSocket { return context.switchToWs().getClient(); } diff --git a/tsconfig.json b/tsconfig.json index b3b314058..e5745757c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,8 @@ "strict": false, "noUnusedLocals": false, "lib": ["ES7"], - "esModuleInterop": true + "esModuleInterop": true, + "skipLibCheck": true }, "exclude": ["node_modules", "lib"] }