From 8b68bb1d63546647f3ace3e722e991374497b251 Mon Sep 17 00:00:00 2001 From: Shinwoo PARK Date: Thu, 27 Mar 2025 02:56:18 +0900 Subject: [PATCH] feat(nestlogged-fastify): make loggerInit called after nestjs initialization --- packages/nestlogged-fastify/src/logged/class.ts | 14 +++----------- .../src/logged/methods/function.ts | 11 +++-------- .../nestlogged-fastify/src/logged/methods/guard.ts | 12 ++++-------- .../src/logged/methods/interceptor.ts | 11 +++-------- .../src/logged/methods/middleware.ts | 10 ++-------- .../nestlogged-fastify/src/logged/methods/route.ts | 10 +++------- packages/nestlogged-fastify/src/logged/override.ts | 9 ++++++--- 7 files changed, 24 insertions(+), 53 deletions(-) diff --git a/packages/nestlogged-fastify/src/logged/class.ts b/packages/nestlogged-fastify/src/logged/class.ts index 9559151..b7e24cd 100644 --- a/packages/nestlogged-fastify/src/logged/class.ts +++ b/packages/nestlogged-fastify/src/logged/class.ts @@ -4,17 +4,13 @@ import { ControllerOptions, ScopeOptions, } from '@nestjs/common'; -import { loggerInit, RevRequestMethod } from 'nestlogged/lib/logged/utils'; +import { RevRequestMethod } from 'nestlogged/lib/logged/utils'; import { LoggedRoute, LoggedFunction } from './methods'; export function LoggedInjectable( options?: ScopeOptions & { verbose?: boolean }, ) { return (target: any) => { - loggerInit(target.prototype); - - const logger = target.prototype.logger; - const methods = Object.getOwnPropertyNames(target.prototype); methods.forEach((method) => { @@ -23,7 +19,7 @@ export function LoggedInjectable( typeof target.prototype[method] === 'function' ) { if (options && options.verbose) - logger.log(`LoggedFunction applied to ${method}`); + console.log(`LoggedFunction applied to ${method}`); LoggedFunction()(target.prototype, method, { value: target.prototype[method], }); @@ -44,10 +40,6 @@ export function LoggedController( export function LoggedController(param?: any): (target: any) => void { return (target: any) => { - loggerInit(target.prototype); - - const logger = target.prototype.logger; - const methods = Object.getOwnPropertyNames(target.prototype); let verbose = @@ -66,7 +58,7 @@ export function LoggedController(param?: any): (target: any) => void { 'method', target.prototype[method], ); - logger.log( + console.log( `LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`, ); } diff --git a/packages/nestlogged-fastify/src/logged/methods/function.ts b/packages/nestlogged-fastify/src/logged/methods/function.ts index cfea25d..1476456 100644 --- a/packages/nestlogged-fastify/src/logged/methods/function.ts +++ b/packages/nestlogged-fastify/src/logged/methods/function.ts @@ -1,5 +1,4 @@ -import { Logger } from '@nestjs/common'; -import { OverrideBuildOptions, loggerInit } from 'nestlogged/lib/logged/utils'; +import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { loggedParam, @@ -18,14 +17,10 @@ export function LoggedFunction, R>( key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise>, ) => { - loggerInit(_target); - - const logger: Logger = _target.logger; - const fn = descriptor.value; if (!fn || typeof fn !== 'function') { - logger.warn( + console.warn( `LoggedFunction decorator applied to non-function property: ${key}`, ); return; @@ -68,7 +63,7 @@ export function LoggedFunction, R>( const overrideFunction = overrideBuild( 'function', fn, - logger, + _target, { scopedLoggerInjectableParam, loggedParams, diff --git a/packages/nestlogged-fastify/src/logged/methods/guard.ts b/packages/nestlogged-fastify/src/logged/methods/guard.ts index d68ba34..ba2a654 100644 --- a/packages/nestlogged-fastify/src/logged/methods/guard.ts +++ b/packages/nestlogged-fastify/src/logged/methods/guard.ts @@ -1,5 +1,5 @@ -import { ExecutionContext, Logger } from '@nestjs/common'; -import { OverrideBuildOptions, loggerInit } from 'nestlogged/lib/logged/utils'; +import { ExecutionContext } from '@nestjs/common'; +import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/reflected'; import { overrideBuild } from '../override'; @@ -14,14 +14,10 @@ export function LoggedGuard, R>( (context: ExecutionContext, ...args: F) => R >, ) => { - loggerInit(_target); - - const logger: Logger = _target.logger; - const fn = descriptor.value; if (!fn || typeof fn !== 'function') { - logger.warn( + console.warn( `LoggedGuard decorator applied to non-function property: ${key}`, ); return; @@ -58,7 +54,7 @@ export function LoggedGuard, R>( const overrideFunction = overrideBuild( 'guard', fn, - logger, + _target, { scopedLoggerInjectableParam, loggedParams: [], diff --git a/packages/nestlogged-fastify/src/logged/methods/interceptor.ts b/packages/nestlogged-fastify/src/logged/methods/interceptor.ts index 39cb5d6..c9e4450 100644 --- a/packages/nestlogged-fastify/src/logged/methods/interceptor.ts +++ b/packages/nestlogged-fastify/src/logged/methods/interceptor.ts @@ -1,6 +1,5 @@ import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils'; -import { ExecutionContext, Logger } from '@nestjs/common'; -import { loggerInit } from 'nestlogged/lib/logged/utils'; +import { ExecutionContext } from '@nestjs/common'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/reflected'; import { overrideBuild } from '../override'; @@ -15,14 +14,10 @@ export function LoggedInterceptor, R>( (context: ExecutionContext, ...args: F) => R >, ) => { - loggerInit(_target); - - const logger: Logger = _target.logger; - const fn = descriptor.value; if (!fn || typeof fn !== 'function') { - logger.warn( + console.warn( `LoggedInterceptor decorator applied to non-function property: ${key}`, ); return; @@ -59,7 +54,7 @@ export function LoggedInterceptor, R>( const overrideFunction = overrideBuild( 'interceptor', fn, - logger, + _target, { scopedLoggerInjectableParam, loggedParams: [], diff --git a/packages/nestlogged-fastify/src/logged/methods/middleware.ts b/packages/nestlogged-fastify/src/logged/methods/middleware.ts index caaff1a..a23ae97 100644 --- a/packages/nestlogged-fastify/src/logged/methods/middleware.ts +++ b/packages/nestlogged-fastify/src/logged/methods/middleware.ts @@ -1,6 +1,4 @@ import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils'; -import { Logger } from '@nestjs/common'; -import { loggerInit } from 'nestlogged/lib/logged/utils'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/reflected'; import { overrideBuild } from '../override'; @@ -15,14 +13,10 @@ export function LoggedMiddleware, R>( (...args: F) => R >, ) => { - loggerInit(_target); - - const logger: Logger = _target.logger; - const fn = descriptor.value; if (!fn || typeof fn !== 'function') { - logger.warn( + console.warn( `LoggedMiddleware decorator applied to non-function property: ${key}`, ); return; @@ -59,7 +53,7 @@ export function LoggedMiddleware, R>( const overrideFunction = overrideBuild( 'middleware', fn, - logger, + _target, { scopedLoggerInjectableParam, loggedParams: [], diff --git a/packages/nestlogged-fastify/src/logged/methods/route.ts b/packages/nestlogged-fastify/src/logged/methods/route.ts index e877141..7cf884e 100644 --- a/packages/nestlogged-fastify/src/logged/methods/route.ts +++ b/packages/nestlogged-fastify/src/logged/methods/route.ts @@ -1,5 +1,5 @@ import { RequestMethod } from '@nestjs/common'; -import { OverrideBuildOptions, loggerInit, RevRequestMethod } from 'nestlogged/lib/logged/utils'; +import { OverrideBuildOptions, RevRequestMethod } from 'nestlogged/lib/logged/utils'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { loggedParam, @@ -20,14 +20,10 @@ export function LoggedRoute, R>( key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R>, ) => { - loggerInit(_target); - - const logger = _target.logger; - const fn = descriptor.value; if (!fn || typeof fn !== 'function') { - logger.warn( + console.warn( `LoggedRoute decorator applied to non-function property: ${key}`, ); return; @@ -81,7 +77,7 @@ export function LoggedRoute, R>( const overrideFunction = overrideBuild( 'route', fn, - logger, + _target, { scopedLoggerInjectableParam, loggedParams, diff --git a/packages/nestlogged-fastify/src/logged/override.ts b/packages/nestlogged-fastify/src/logged/override.ts index c2d9149..8a24701 100644 --- a/packages/nestlogged-fastify/src/logged/override.ts +++ b/packages/nestlogged-fastify/src/logged/override.ts @@ -5,6 +5,7 @@ import { BuildType, REQUEST_LOG_ID, createCallLogIdentifyMessage, + loggerInit, } from 'nestlogged/lib/logged/utils'; import { objectContainedLogSync, getItemByPathSync } from 'nestlogged/lib/internals/utils'; import { ScopedLogger } from 'nestlogged/lib/logger'; @@ -18,7 +19,7 @@ interface FunctionMetadata { export function overrideBuild, R>( type: 'route', originalFunction: (...args: F) => R, - baseLogger: Logger, + _target: any, metadatas: FunctionMetadata, key: string, returnsData: ReturnsReflectData[] | string | true, @@ -28,7 +29,7 @@ export function overrideBuild, R>( export function overrideBuild, R>( type: 'function' | 'guard' | 'interceptor' | 'middleware', originalFunction: (...args: F) => R, - baseLogger: Logger, + _target: any, metadatas: FunctionMetadata, key: string, returnsData: ReturnsReflectData[] | string | true, @@ -37,7 +38,7 @@ export function overrideBuild, R>( export function overrideBuild, R>( type: BuildType, originalFunction: (...args: F) => R, - baseLogger: Logger, + _target: any, metadatas: FunctionMetadata, key: string, returnsData: ReturnsReflectData[] | string | true, @@ -45,6 +46,8 @@ export function overrideBuild, R>( route?: string, ): (...args: F) => R { return function (...args: F): R { + const baseLogger: Logger = loggerInit(_target); + // Creating ScopedLogger let injectedLogger: Logger = baseLogger; if (typeof metadatas.scopedLoggerInjectableParam !== 'undefined') {