feat(nestlogged-fastify): make loggerInit called after nestjs initialization

This commit is contained in:
Shinwoo PARK 2025-03-27 02:56:18 +09:00
parent e62be27436
commit 8b68bb1d63
7 changed files with 24 additions and 53 deletions

View File

@ -4,17 +4,13 @@ import {
ControllerOptions, ControllerOptions,
ScopeOptions, ScopeOptions,
} from '@nestjs/common'; } from '@nestjs/common';
import { loggerInit, RevRequestMethod } from 'nestlogged/lib/logged/utils'; import { RevRequestMethod } from 'nestlogged/lib/logged/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 methods = Object.getOwnPropertyNames(target.prototype); const methods = Object.getOwnPropertyNames(target.prototype);
methods.forEach((method) => { methods.forEach((method) => {
@ -23,7 +19,7 @@ export function LoggedInjectable(
typeof target.prototype[method] === 'function' typeof target.prototype[method] === 'function'
) { ) {
if (options && options.verbose) if (options && options.verbose)
logger.log(`LoggedFunction applied to ${method}`); console.log(`LoggedFunction applied to ${method}`);
LoggedFunction()(target.prototype, method, { LoggedFunction()(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
@ -44,10 +40,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 methods = Object.getOwnPropertyNames(target.prototype); const methods = Object.getOwnPropertyNames(target.prototype);
let verbose = let verbose =
@ -66,7 +58,7 @@ export function LoggedController(param?: any): (target: any) => void {
'method', 'method',
target.prototype[method], target.prototype[method],
); );
logger.log( console.log(
`LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`, `LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`,
); );
} }

View File

@ -1,5 +1,4 @@
import { Logger } from '@nestjs/common'; import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils';
import { OverrideBuildOptions, loggerInit } from 'nestlogged/lib/logged/utils';
import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/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 'nestlogged/lib/logged/utils'; import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils';
import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata';
import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/reflected'; import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/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 'nestlogged/lib/logged/utils'; import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils';
import { ExecutionContext, Logger } from '@nestjs/common'; import { ExecutionContext } from '@nestjs/common';
import { loggerInit } from 'nestlogged/lib/logged/utils';
import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata';
import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/reflected'; import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/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 'nestlogged/lib/logged/utils'; 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 { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata';
import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/reflected'; import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/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 'nestlogged/lib/logged/utils'; import { OverrideBuildOptions, RevRequestMethod } from 'nestlogged/lib/logged/utils';
import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata'; import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/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 'nestlogged/lib/logged/utils'; } from 'nestlogged/lib/logged/utils';
import { objectContainedLogSync, getItemByPathSync } from 'nestlogged/lib/internals/utils'; import { objectContainedLogSync, getItemByPathSync } from 'nestlogged/lib/internals/utils';
import { ScopedLogger } from 'nestlogged/lib/logger'; import { ScopedLogger } from 'nestlogged/lib/logger';
@ -18,7 +19,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,
@ -28,7 +29,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,
@ -37,7 +38,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,
@ -45,6 +46,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') {