feat: add options to skip overrideBuild logs

This commit is contained in:
p-sw 2024-11-29 21:20:17 +09:00
parent c4dce4dbc3
commit 94aa138461

View File

@ -59,7 +59,7 @@ export function LoggedInjectable(
) { ) {
if (options && options.verbose) if (options && options.verbose)
logger.log(`LoggedFunction applied to ${method}`); logger.log(`LoggedFunction applied to ${method}`);
LoggedFunction(target.prototype, method, { LoggedFunction()(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
} }
@ -121,6 +121,12 @@ interface FunctionMetadata {
loggedParams?: LoggedParamReflectData[]; loggedParams?: LoggedParamReflectData[];
} }
interface OverrideBuildOptions {
skipCallLog: boolean;
skipReturnLog: boolean;
skipErrorLog: boolean;
}
function overrideBuild<F extends Array<any>, R>( function overrideBuild<F extends Array<any>, R>(
originalFunction: (...args: F) => R, originalFunction: (...args: F) => R,
baseLogger: Logger, baseLogger: Logger,
@ -129,7 +135,8 @@ function overrideBuild<F extends Array<any>, R>(
returnsData: ReturnsReflectData[] | string | true, returnsData: ReturnsReflectData[] | string | true,
route?: { route?: {
fullRoute: string; fullRoute: string;
} },
options?: Partial<OverrideBuildOptions>
): (...args: F) => R { ): (...args: F) => R {
return function (...args: F): R { return function (...args: F): R {
let injectedLogger: Logger = baseLogger; let injectedLogger: Logger = baseLogger;
@ -146,6 +153,7 @@ function overrideBuild<F extends Array<any>, R>(
injectedLogger = args[metadatas.scopedLoggerInjectableParam]; injectedLogger = args[metadatas.scopedLoggerInjectableParam];
} }
if (!options?.skipCallLog) {
injectedLogger.log( injectedLogger.log(
`${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key `${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key
} ${metadatas.loggedParams && metadatas.loggedParams.length > 0 } ${metadatas.loggedParams && metadatas.loggedParams.length > 0
@ -162,9 +170,11 @@ function overrideBuild<F extends Array<any>, R>(
: "" : ""
}` }`
); );
}
try { try {
const r: R = originalFunction.call(this, ...args); const r: R = originalFunction.call(this, ...args);
if (!options?.skipReturnLog) {
if ( if (
originalFunction.constructor.name === 'AsyncFunction' || originalFunction.constructor.name === 'AsyncFunction' ||
(r && typeof r === 'object' && typeof r['then'] === 'function') (r && typeof r === 'object' && typeof r['then'] === 'function')
@ -223,20 +233,28 @@ function overrideBuild<F extends Array<any>, R>(
); );
return r; return r;
} }
} else {
return r;
}
} catch (e) { } catch (e) {
if (!options?.skipErrorLog) {
injectedLogger.error( injectedLogger.error(
`WHILE ${route ? `HTTP ${route.fullRoute} (${key})` : key} ERROR ${e}` `WHILE ${route ? `HTTP ${route.fullRoute} (${key})` : key} ERROR ${e}`
); );
}
throw e; throw e;
} }
}; };
} }
export function LoggedFunction<F extends Array<any>, R>( export function LoggedFunction<F extends Array<any>, R>(
options?: Partial<OverrideBuildOptions>
) {
return (
_target: any, _target: any,
key: string, key: string,
descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>> descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>>
) { ) => {
loggerInit(_target); loggerInit(_target);
const logger: Logger = _target.logger; const logger: Logger = _target.logger;
@ -287,7 +305,9 @@ export function LoggedFunction<F extends Array<any>, R>(
scopeKeys, scopeKeys,
}, },
key, key,
returnsData returnsData,
undefined,
options,
); );
_target[key] = overrideFunction; _target[key] = overrideFunction;
@ -298,8 +318,9 @@ export function LoggedFunction<F extends Array<any>, R>(
Reflect.defineMetadata(k, v, descriptor.value); Reflect.defineMetadata(k, v, descriptor.value);
}); });
} }
}
export function LoggedRoute<F extends Array<any>, R>(route?: string) { export function LoggedRoute<F extends Array<any>, R>(route?: string, options?: Partial<OverrideBuildOptions>) {
return ( return (
_target: any, _target: any,
key: string, key: string,
@ -364,7 +385,8 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
returnsData, returnsData,
{ {
fullRoute, fullRoute,
} },
options,
); );
_target[key] = overrideFunction; _target[key] = overrideFunction;