feat: add ability to log return value

This commit is contained in:
Shinwoo PARK 2023-12-15 01:38:09 +09:00
parent 36da94a623
commit 995fbafe19
2 changed files with 51 additions and 4 deletions

View File

@ -74,3 +74,13 @@ export default async function objectContainedLogged(
return `${ocv}`; return `${ocv}`;
} }
} }
export async function getItemByPath(obj: object, path: string | string[]) {
const paths = Array.isArray(path) ? path : path.split(".");
return Object.keys(obj).includes(paths[0])
? typeof obj[paths[0]] === "object"
? getItemByPath(obj[paths[0]], paths.slice(1))
: obj[paths[0]]
: undefined;
}

View File

@ -8,12 +8,14 @@ import {
import { ScopedLogger } from "./logger"; import { ScopedLogger } from "./logger";
import { import {
LoggedParamReflectData, LoggedParamReflectData,
ReturnsReflectData,
ScopeKeyReflectData, ScopeKeyReflectData,
forceScopeKey, forceScopeKey,
returns,
scopeKey, scopeKey,
} from "./reflected"; } from "./reflected";
import { loggedParam, scopedLogger } from "./reflected"; import { loggedParam, scopedLogger } from "./reflected";
import objectContainedLogged from "./functions"; import objectContainedLogged, { getItemByPath } from "./functions";
import { RequestMethod } from "@nestjs/common"; import { RequestMethod } from "@nestjs/common";
const RevRequestMethod = [ const RevRequestMethod = [
@ -126,6 +128,7 @@ function overrideBuild<F extends Array<any>, R>(
baseLogger: Logger, baseLogger: Logger,
metadatas: FunctionMetadata, metadatas: FunctionMetadata,
key: string, key: string,
returnsData: ReturnsReflectData[] | true,
route?: { route?: {
fullRoute: string; fullRoute: string;
} }
@ -219,10 +222,32 @@ function overrideBuild<F extends Array<any>, R>(
try { try {
const r: R = await originalFunction.call(this, ...args); const r: R = await originalFunction.call(this, ...args);
const resultLogged = Array.isArray(returnsData)
? typeof r === "object"
? "WITH " +
(
await Promise.all(
returnsData.map(async ({ name, path }) => {
const value = await getItemByPath(r, path);
return value !== undefined ? `${name}=${value}` : "";
})
)
)
.filter((v) => v.length > 0)
.join(", ")
: ""
: returnsData
? typeof r === "object"
? "WITH " + JSON.stringify(r)
: "WITH " + r
: "";
injectedLogger.log( injectedLogger.log(
route route
? `RETURNED RESPONSE ${route.fullRoute} (${key})` ? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}`
: `RETURNED ${key}` : `RETURNED ${key} ${resultLogged}`
); );
return r; return r;
} catch (e) { } catch (e) {
@ -277,6 +302,11 @@ export function LoggedFunction<F extends Array<any>, R>(
const shouldScoped: boolean = Reflect.getOwnMetadata(forceScopeKey, fn); const shouldScoped: boolean = Reflect.getOwnMetadata(forceScopeKey, fn);
const returnsData: ReturnsReflectData[] | true = Reflect.getOwnMetadata(
returns,
fn
);
const overrideFunction = overrideBuild( const overrideFunction = overrideBuild(
fn, fn,
logger, logger,
@ -286,7 +316,8 @@ export function LoggedFunction<F extends Array<any>, R>(
scopeKeys, scopeKeys,
shouldScoped, shouldScoped,
}, },
key key,
returnsData
); );
_target[key] = overrideFunction; _target[key] = overrideFunction;
@ -349,6 +380,11 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
const shouldScoped: boolean = Reflect.getOwnMetadata(forceScopeKey, fn); const shouldScoped: boolean = Reflect.getOwnMetadata(forceScopeKey, fn);
const returnsData: ReturnsReflectData[] | true = Reflect.getOwnMetadata(
returns,
fn
);
const overrideFunction = overrideBuild( const overrideFunction = overrideBuild(
fn, fn,
logger, logger,
@ -359,6 +395,7 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
shouldScoped, shouldScoped,
}, },
key, key,
returnsData,
{ {
fullRoute, fullRoute,
} }