diff --git a/packages/nestlogged/src/logged/class.ts b/packages/nestlogged/src/logged/class.ts index 1b1561e..5364051 100644 --- a/packages/nestlogged/src/logged/class.ts +++ b/packages/nestlogged/src/logged/class.ts @@ -4,15 +4,13 @@ import { ControllerOptions, ScopeOptions, } from '@nestjs/common'; -import { loggerInit, RevRequestMethod } from './utils'; +import { RevRequestMethod } from './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); @@ -44,8 +42,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); diff --git a/packages/nestlogged/src/logged/methods/function.ts b/packages/nestlogged/src/logged/methods/function.ts index 3805302..367f7d9 100644 --- a/packages/nestlogged/src/logged/methods/function.ts +++ b/packages/nestlogged/src/logged/methods/function.ts @@ -1,5 +1,4 @@ -import { Logger } from '@nestjs/common'; -import { OverrideBuildOptions, loggerInit } from '../utils'; +import { OverrideBuildOptions } from '../utils'; import { LoggedMetadata, nestLoggedMetadata } from '../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/src/logged/methods/guard.ts b/packages/nestlogged/src/logged/methods/guard.ts index 0cb592b..def32b7 100644 --- a/packages/nestlogged/src/logged/methods/guard.ts +++ b/packages/nestlogged/src/logged/methods/guard.ts @@ -1,5 +1,5 @@ -import { ExecutionContext, Logger } from '@nestjs/common'; -import { OverrideBuildOptions, loggerInit } from '../utils'; +import { ExecutionContext } from '@nestjs/common'; +import { OverrideBuildOptions } from '../utils'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { scopedLogger, returns, ReturnsReflectData } from '../../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/src/logged/methods/interceptor.ts b/packages/nestlogged/src/logged/methods/interceptor.ts index 2a1bf72..e6623bb 100644 --- a/packages/nestlogged/src/logged/methods/interceptor.ts +++ b/packages/nestlogged/src/logged/methods/interceptor.ts @@ -1,6 +1,5 @@ import { OverrideBuildOptions } from '../utils'; import { ExecutionContext, Logger } from '@nestjs/common'; -import { loggerInit } from '../utils'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { scopedLogger, returns, ReturnsReflectData } from '../../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/src/logged/methods/middleware.ts b/packages/nestlogged/src/logged/methods/middleware.ts index 81d3649..4cc9586 100644 --- a/packages/nestlogged/src/logged/methods/middleware.ts +++ b/packages/nestlogged/src/logged/methods/middleware.ts @@ -1,6 +1,4 @@ import { OverrideBuildOptions } from '../utils'; -import { Logger } from '@nestjs/common'; -import { loggerInit } from '../utils'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { scopedLogger, returns, ReturnsReflectData } from '../../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/src/logged/methods/route.ts b/packages/nestlogged/src/logged/methods/route.ts index 952b20f..a243afc 100644 --- a/packages/nestlogged/src/logged/methods/route.ts +++ b/packages/nestlogged/src/logged/methods/route.ts @@ -1,5 +1,5 @@ import { RequestMethod } from '@nestjs/common'; -import { OverrideBuildOptions, loggerInit, RevRequestMethod } from '../utils'; +import { OverrideBuildOptions, RevRequestMethod } from '../utils'; import { LoggedMetadata, nestLoggedMetadata } from '../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/src/logged/override.ts b/packages/nestlogged/src/logged/override.ts index fd057bb..d6e3b3b 100644 --- a/packages/nestlogged/src/logged/override.ts +++ b/packages/nestlogged/src/logged/override.ts @@ -5,6 +5,7 @@ import { BuildType, REQUEST_LOG_ID, createCallLogIdentifyMessage, + loggerInit, } from './utils'; import { objectContainedLogSync, getItemByPathSync } from '../internals/utils'; import { ScopedLogger } from '../logger'; @@ -17,7 +18,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, @@ -27,7 +28,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, @@ -36,7 +37,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, @@ -44,6 +45,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') { diff --git a/packages/nestlogged/src/logged/utils.ts b/packages/nestlogged/src/logged/utils.ts index 01098f2..6e6d2ad 100644 --- a/packages/nestlogged/src/logged/utils.ts +++ b/packages/nestlogged/src/logged/utils.ts @@ -23,6 +23,8 @@ export function loggerInit(_target: any) { value: newTargetLogger, }); } + + return _target.logger as Logger; } export type BuildType =