Created
November 5, 2024 06:32
-
-
Save Goganoid/83d7084c6d7cd8f56e2b212528e7aeca to your computer and use it in GitHub Desktop.
Nestjs logger decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Logger, LoggerService } from '@nestjs/common'; | |
| export type Transform = (data: any) => any; | |
| export interface Options { | |
| logger?: LoggerService; | |
| prefix?: string; | |
| transform?: Transform; | |
| timestamp?: boolean; | |
| } | |
| const DEFAULT_OPTIONS: Options = { | |
| prefix: 'Function', | |
| timestamp: true, | |
| }; | |
| export const Log = (options = DEFAULT_OPTIONS) => { | |
| return ( | |
| target: any, | |
| propertyName: string, | |
| descriptor: TypedPropertyDescriptor<any>, | |
| ): void => { | |
| const logger = options?.logger ?? new Logger(target?.constructor?.name); | |
| const original = descriptor?.value; | |
| descriptor.value = new Proxy(original, { | |
| apply: async function (target, thisArg, args: unknown[]) { | |
| const currentTime = Date.now(); | |
| logger.debug?.( | |
| `${options?.prefix} "${propertyName}" invoke -> ${JSON.stringify( | |
| options?.transform ? options?.transform(args) : args, | |
| )}`, | |
| ); | |
| try { | |
| const data = await target.apply(thisArg, args); | |
| const executeTime = options?.timestamp | |
| ? `${Date.now() - currentTime}ms.` | |
| : ''; | |
| logger.debug?.( | |
| `${options?.prefix} "${propertyName}" result -> ${JSON.stringify( | |
| options?.transform ? options?.transform(data) : data, | |
| )} ${executeTime}`, | |
| ); | |
| return data; | |
| } catch (e) { | |
| logger.error(`${options?.prefix} "${propertyName}" failed`, e); | |
| throw e; | |
| } | |
| }, | |
| }); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment