feat(nestlogged): make loggerInit called after nestjs initialization

This commit is contained in:
Shinwoo PARK 2025-03-27 02:52:18 +09:00
parent 6a0c98d742
commit e62be27436
8 changed files with 23 additions and 46 deletions

View File

@ -4,15 +4,13 @@ import {
ControllerOptions, ControllerOptions,
ScopeOptions, ScopeOptions,
} from '@nestjs/common'; } from '@nestjs/common';
import { loggerInit, RevRequestMethod } from './utils'; import { RevRequestMethod } from './utils';
import { LoggedRoute, LoggedFunction } from './methods'; import { LoggedRoute, LoggedFunction } from './methods';
export function LoggedInjectable( export function LoggedInjectable(
options?: ScopeOptions & { verbose?: boolean }, options?: ScopeOptions & { verbose?: boolean },
) { ) {
return (target: any) => { return (target: any) => {
loggerInit(target.prototype);
const logger = target.prototype.logger; const logger = target.prototype.logger;
const methods = Object.getOwnPropertyNames(target.prototype); const methods = Object.getOwnPropertyNames(target.prototype);
@ -44,8 +42,6 @@ export function LoggedController(
export function LoggedController(param?: any): (target: any) => void { export function LoggedController(param?: any): (target: any) => void {
return (target: any) => { return (target: any) => {
loggerInit(target.prototype);
const logger = target.prototype.logger; const logger = target.prototype.logger;
const methods = Object.getOwnPropertyNames(target.prototype); const methods = Object.getOwnPropertyNames(target.prototype);

View File

@ -1,5 +1,4 @@
import { Logger } from '@nestjs/common'; import { OverrideBuildOptions } from '../utils';
import { OverrideBuildOptions, loggerInit } from '../utils';
import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata';
import { import {
loggedParam, loggedParam,
@ -18,14 +17,10 @@ export function LoggedFunction<F extends Array<any>, R>(
key: string, key: string,
descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>>, descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>>,
) => { ) => {
loggerInit(_target);
const logger: Logger = _target.logger;
const fn = descriptor.value; const fn = descriptor.value;
if (!fn || typeof fn !== 'function') { if (!fn || typeof fn !== 'function') {
logger.warn( console.warn(
`LoggedFunction decorator applied to non-function property: ${key}`, `LoggedFunction decorator applied to non-function property: ${key}`,
); );
return; return;
@ -68,7 +63,7 @@ export function LoggedFunction<F extends Array<any>, R>(
const overrideFunction = overrideBuild( const overrideFunction = overrideBuild(
'function', 'function',
fn, fn,
logger, _target,
{ {
scopedLoggerInjectableParam, scopedLoggerInjectableParam,
loggedParams, loggedParams,

View File

@ -1,5 +1,5 @@
import { ExecutionContext, Logger } from '@nestjs/common'; import { ExecutionContext } from '@nestjs/common';
import { OverrideBuildOptions, loggerInit } from '../utils'; import { OverrideBuildOptions } from '../utils';
import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata';
import { scopedLogger, returns, ReturnsReflectData } from '../../reflected'; import { scopedLogger, returns, ReturnsReflectData } from '../../reflected';
import { overrideBuild } from '../override'; import { overrideBuild } from '../override';
@ -14,14 +14,10 @@ export function LoggedGuard<F extends Array<any>, R>(
(context: ExecutionContext, ...args: F) => R (context: ExecutionContext, ...args: F) => R
>, >,
) => { ) => {
loggerInit(_target);
const logger: Logger = _target.logger;
const fn = descriptor.value; const fn = descriptor.value;
if (!fn || typeof fn !== 'function') { if (!fn || typeof fn !== 'function') {
logger.warn( console.warn(
`LoggedGuard decorator applied to non-function property: ${key}`, `LoggedGuard decorator applied to non-function property: ${key}`,
); );
return; return;
@ -58,7 +54,7 @@ export function LoggedGuard<F extends Array<any>, R>(
const overrideFunction = overrideBuild( const overrideFunction = overrideBuild(
'guard', 'guard',
fn, fn,
logger, _target,
{ {
scopedLoggerInjectableParam, scopedLoggerInjectableParam,
loggedParams: [], loggedParams: [],

View File

@ -1,6 +1,5 @@
import { OverrideBuildOptions } from '../utils'; import { OverrideBuildOptions } from '../utils';
import { ExecutionContext, Logger } from '@nestjs/common'; import { ExecutionContext, Logger } from '@nestjs/common';
import { loggerInit } from '../utils';
import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata';
import { scopedLogger, returns, ReturnsReflectData } from '../../reflected'; import { scopedLogger, returns, ReturnsReflectData } from '../../reflected';
import { overrideBuild } from '../override'; import { overrideBuild } from '../override';
@ -15,14 +14,10 @@ export function LoggedInterceptor<F extends Array<any>, R>(
(context: ExecutionContext, ...args: F) => R (context: ExecutionContext, ...args: F) => R
>, >,
) => { ) => {
loggerInit(_target);
const logger: Logger = _target.logger;
const fn = descriptor.value; const fn = descriptor.value;
if (!fn || typeof fn !== 'function') { if (!fn || typeof fn !== 'function') {
logger.warn( console.warn(
`LoggedInterceptor decorator applied to non-function property: ${key}`, `LoggedInterceptor decorator applied to non-function property: ${key}`,
); );
return; return;
@ -59,7 +54,7 @@ export function LoggedInterceptor<F extends Array<any>, R>(
const overrideFunction = overrideBuild( const overrideFunction = overrideBuild(
'interceptor', 'interceptor',
fn, fn,
logger, _target,
{ {
scopedLoggerInjectableParam, scopedLoggerInjectableParam,
loggedParams: [], loggedParams: [],

View File

@ -1,6 +1,4 @@
import { OverrideBuildOptions } from '../utils'; import { OverrideBuildOptions } from '../utils';
import { Logger } from '@nestjs/common';
import { loggerInit } from '../utils';
import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata';
import { scopedLogger, returns, ReturnsReflectData } from '../../reflected'; import { scopedLogger, returns, ReturnsReflectData } from '../../reflected';
import { overrideBuild } from '../override'; import { overrideBuild } from '../override';
@ -15,14 +13,10 @@ export function LoggedMiddleware<F extends Array<any>, R>(
(...args: F) => R (...args: F) => R
>, >,
) => { ) => {
loggerInit(_target);
const logger: Logger = _target.logger;
const fn = descriptor.value; const fn = descriptor.value;
if (!fn || typeof fn !== 'function') { if (!fn || typeof fn !== 'function') {
logger.warn( console.warn(
`LoggedMiddleware decorator applied to non-function property: ${key}`, `LoggedMiddleware decorator applied to non-function property: ${key}`,
); );
return; return;
@ -59,7 +53,7 @@ export function LoggedMiddleware<F extends Array<any>, R>(
const overrideFunction = overrideBuild( const overrideFunction = overrideBuild(
'middleware', 'middleware',
fn, fn,
logger, _target,
{ {
scopedLoggerInjectableParam, scopedLoggerInjectableParam,
loggedParams: [], loggedParams: [],

View File

@ -1,5 +1,5 @@
import { RequestMethod } from '@nestjs/common'; import { RequestMethod } from '@nestjs/common';
import { OverrideBuildOptions, loggerInit, RevRequestMethod } from '../utils'; import { OverrideBuildOptions, RevRequestMethod } from '../utils';
import { LoggedMetadata, nestLoggedMetadata } from '../metadata'; import { LoggedMetadata, nestLoggedMetadata } from '../metadata';
import { import {
loggedParam, loggedParam,
@ -20,14 +20,10 @@ export function LoggedRoute<F extends Array<any>, R>(
key: string, key: string,
descriptor: TypedPropertyDescriptor<(...args: F) => R>, descriptor: TypedPropertyDescriptor<(...args: F) => R>,
) => { ) => {
loggerInit(_target);
const logger = _target.logger;
const fn = descriptor.value; const fn = descriptor.value;
if (!fn || typeof fn !== 'function') { if (!fn || typeof fn !== 'function') {
logger.warn( console.warn(
`LoggedRoute decorator applied to non-function property: ${key}`, `LoggedRoute decorator applied to non-function property: ${key}`,
); );
return; return;
@ -81,7 +77,7 @@ export function LoggedRoute<F extends Array<any>, R>(
const overrideFunction = overrideBuild( const overrideFunction = overrideBuild(
'route', 'route',
fn, fn,
logger, _target,
{ {
scopedLoggerInjectableParam, scopedLoggerInjectableParam,
loggedParams, loggedParams,

View File

@ -5,6 +5,7 @@ import {
BuildType, BuildType,
REQUEST_LOG_ID, REQUEST_LOG_ID,
createCallLogIdentifyMessage, createCallLogIdentifyMessage,
loggerInit,
} from './utils'; } from './utils';
import { objectContainedLogSync, getItemByPathSync } from '../internals/utils'; import { objectContainedLogSync, getItemByPathSync } from '../internals/utils';
import { ScopedLogger } from '../logger'; import { ScopedLogger } from '../logger';
@ -17,7 +18,7 @@ interface FunctionMetadata {
export function overrideBuild<F extends Array<any>, R>( export function overrideBuild<F extends Array<any>, R>(
type: 'route', type: 'route',
originalFunction: (...args: F) => R, originalFunction: (...args: F) => R,
baseLogger: Logger, _target: any,
metadatas: FunctionMetadata, metadatas: FunctionMetadata,
key: string, key: string,
returnsData: ReturnsReflectData[] | string | true, returnsData: ReturnsReflectData[] | string | true,
@ -27,7 +28,7 @@ export function overrideBuild<F extends Array<any>, R>(
export function overrideBuild<F extends Array<any>, R>( export function overrideBuild<F extends Array<any>, R>(
type: 'function' | 'guard' | 'interceptor' | 'middleware', type: 'function' | 'guard' | 'interceptor' | 'middleware',
originalFunction: (...args: F) => R, originalFunction: (...args: F) => R,
baseLogger: Logger, _target: any,
metadatas: FunctionMetadata, metadatas: FunctionMetadata,
key: string, key: string,
returnsData: ReturnsReflectData[] | string | true, returnsData: ReturnsReflectData[] | string | true,
@ -36,7 +37,7 @@ export function overrideBuild<F extends Array<any>, R>(
export function overrideBuild<F extends Array<any>, R>( export function overrideBuild<F extends Array<any>, R>(
type: BuildType, type: BuildType,
originalFunction: (...args: F) => R, originalFunction: (...args: F) => R,
baseLogger: Logger, _target: any,
metadatas: FunctionMetadata, metadatas: FunctionMetadata,
key: string, key: string,
returnsData: ReturnsReflectData[] | string | true, returnsData: ReturnsReflectData[] | string | true,
@ -44,6 +45,8 @@ export function overrideBuild<F extends Array<any>, R>(
route?: string, route?: string,
): (...args: F) => R { ): (...args: F) => R {
return function (...args: F): R { return function (...args: F): R {
const baseLogger: Logger = loggerInit(_target);
// Creating ScopedLogger // Creating ScopedLogger
let injectedLogger: Logger = baseLogger; let injectedLogger: Logger = baseLogger;
if (typeof metadatas.scopedLoggerInjectableParam !== 'undefined') { if (typeof metadatas.scopedLoggerInjectableParam !== 'undefined') {

View File

@ -23,6 +23,8 @@ export function loggerInit(_target: any) {
value: newTargetLogger, value: newTargetLogger,
}); });
} }
return _target.logger as Logger;
} }
export type BuildType = export type BuildType =