From fc205ad5532ef95a708b14d133090900aec81762 Mon Sep 17 00:00:00 2001 From: p-sw Date: Fri, 29 Nov 2024 21:48:37 +0900 Subject: [PATCH] feat: make override option when LoggedFunction call is nested --- src/logged.ts | 73 +++++++++++++++++++++++++++++++++++++++++++----- src/reflected.ts | 2 ++ 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/logged.ts b/src/logged.ts index 571a3a8..ec9e6a1 100644 --- a/src/logged.ts +++ b/src/logged.ts @@ -12,8 +12,10 @@ import { ScopeKeyReflectData, returns, scopeKey, + nestLoggedMetadata, + loggedParam, + scopedLogger } from "./reflected"; -import { loggedParam, scopedLogger } from "./reflected"; import { imObjectContainedLogSync, getItemByPathSync } from "./functions"; import { RequestMethod } from "@nestjs/common"; @@ -127,16 +129,37 @@ interface OverrideBuildOptions { skipErrorLog: boolean; } +const defaultOverrideBuildOptions: OverrideBuildOptions = { + skipCallLog: false, + skipReturnLog: false, + skipErrorLog: false, +} + +class LoggedMetadata { + options: Partial + + constructor(options?: Partial) { + this.options = options ?? defaultOverrideBuildOptions + } + + updateOption(options: Partial) { + this.options = { + ...this.options, + ...options + } + } +} + function overrideBuild, R>( originalFunction: (...args: F) => R, baseLogger: Logger, metadatas: FunctionMetadata, key: string, returnsData: ReturnsReflectData[] | string | true, + logged: LoggedMetadata, route?: { fullRoute: string; }, - options?: Partial ): (...args: F) => R { return function (...args: F): R { let injectedLogger: Logger = baseLogger; @@ -153,7 +176,7 @@ function overrideBuild, R>( injectedLogger = args[metadatas.scopedLoggerInjectableParam]; } - if (!options?.skipCallLog) { + if (!logged.options.skipCallLog) { injectedLogger.log( `${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key } ${metadatas.loggedParams && metadatas.loggedParams.length > 0 @@ -174,7 +197,7 @@ function overrideBuild, R>( try { const r: R = originalFunction.call(this, ...args); - if (!options?.skipReturnLog) { + if (!logged.options.skipReturnLog) { if ( originalFunction.constructor.name === 'AsyncFunction' || (r && typeof r === 'object' && typeof r['then'] === 'function') @@ -237,7 +260,7 @@ function overrideBuild, R>( return r; } } catch (e) { - if (!options?.skipErrorLog) { + if (!logged.options.skipErrorLog) { injectedLogger.error( `WHILE ${route ? `HTTP ${route.fullRoute} (${key})` : key} ERROR ${e}` ); @@ -268,6 +291,18 @@ export function LoggedFunction, R>( return; } + const logMetadata: LoggedMetadata | undefined = Reflect.getOwnMetadata( + nestLoggedMetadata, + _target, + key + ) + if (logMetadata) { + // already applied, override instead + logMetadata.updateOption(options) + return + } + const newMetadata = new LoggedMetadata(options); + const all = Reflect.getMetadataKeys(fn).map((k) => [ k, Reflect.getMetadata(k, fn), @@ -306,13 +341,19 @@ export function LoggedFunction, R>( }, key, returnsData, + newMetadata, undefined, - options, ); _target[key] = overrideFunction; descriptor.value = overrideFunction; + Reflect.defineMetadata( + nestLoggedMetadata, + newMetadata, + _target, + key + ) all.forEach(([k, v]) => { Reflect.defineMetadata(k, v, _target[key]); Reflect.defineMetadata(k, v, descriptor.value); @@ -339,6 +380,18 @@ export function LoggedRoute, R>(route?: string, options?: P return; } + const logMetadata: LoggedMetadata | undefined = Reflect.getOwnMetadata( + nestLoggedMetadata, + _target, + key + ) + if (logMetadata) { + // already applied, override instead + logMetadata.updateOption(options) + return + } + const newMetadata = new LoggedMetadata(options); + const all = Reflect.getMetadataKeys(fn).map((k) => [ k, Reflect.getMetadata(k, fn), @@ -383,15 +436,21 @@ export function LoggedRoute, R>(route?: string, options?: P }, key, returnsData, + newMetadata, { fullRoute, }, - options, ); _target[key] = overrideFunction; descriptor.value = overrideFunction; + Reflect.defineMetadata( + nestLoggedMetadata, + newMetadata, + _target, + key + ) all.forEach(([k, v]) => { Reflect.defineMetadata(k, v, _target[key]); Reflect.defineMetadata(k, v, descriptor.value); diff --git a/src/reflected.ts b/src/reflected.ts index 8b3216d..0ad7431 100644 --- a/src/reflected.ts +++ b/src/reflected.ts @@ -76,6 +76,8 @@ export const loggedParam = Symbol("nlogdec-loggedParam"); export const scopeKey = Symbol("nlogdec-scopeKey"); export const returns = Symbol("nlogdec-returns"); +export const nestLoggedMetadata = Symbol("nlogdec-metadata"); + export function InjectLogger( target: any, propertyKey: string | symbol,