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