fix: fix behavior and logging in logged controller and logged route

This commit is contained in:
Shinwoo PARK 2023-12-03 22:04:15 +09:00
parent 484aaef557
commit 81ff8a3719
3 changed files with 91 additions and 48 deletions

View File

@ -1,7 +1,8 @@
import { ControllerOptions, ScopeOptions } from "@nestjs/common"; import { ControllerOptions, ScopeOptions } from "@nestjs/common";
import { RequestMethod } from "@nestjs/common";
export declare function LoggedInjectable(options?: ScopeOptions): (target: any) => void; export declare function LoggedInjectable(options?: ScopeOptions): (target: any) => void;
export declare function LoggedController(): (target: any) => void; export declare function LoggedController(): (target: any) => void;
export declare function LoggedController(prefix: string | string[]): (target: any) => void; export declare function LoggedController(prefix: string | string[]): (target: any) => void;
export declare function LoggedController(options: ControllerOptions): (target: any) => void; export declare function LoggedController(options: ControllerOptions): (target: any) => void;
export declare function LoggedFunction<F extends Array<any>, R>(_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>): void; export declare function LoggedFunction<F extends Array<any>, R>(_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>): void;
export declare function LoggedRoute<F extends Array<any>, R>(route?: string): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>) => void; export declare function LoggedRoute<F extends Array<any>, R>(route?: string): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>) => [string, RequestMethod];

25
dist/lib/logged.js vendored
View File

@ -5,6 +5,17 @@ const common_1 = require("@nestjs/common");
const logger_1 = require("./logger"); const logger_1 = require("./logger");
const reflected_1 = require("./reflected"); const reflected_1 = require("./reflected");
const functions_1 = require("./functions"); const functions_1 = require("./functions");
const RevRequestMethod = [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"ALL",
"OPTIONS",
"HEAD",
"SEARCH",
];
function loggerInit(_target) { function loggerInit(_target) {
if (!Object.getOwnPropertyNames(_target).includes("logger")) { if (!Object.getOwnPropertyNames(_target).includes("logger")) {
const newTargetLogger = new common_1.Logger(_target.constructor.name); const newTargetLogger = new common_1.Logger(_target.constructor.name);
@ -40,15 +51,17 @@ function LoggedController(param) {
loggerInit(target.prototype); 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);
logger.log(JSON.stringify(methods));
methods.forEach((method) => { methods.forEach((method) => {
logger.log(method);
if (method !== "constructor" && if (method !== "constructor" &&
typeof target.prototype[method] === "function") { typeof target.prototype[method] === "function") {
logger.log(`LoggedRoute applied to ${method}`); const path = Reflect.getMetadata("path", target.prototype[method]);
const httpMethod = Reflect.getMetadata("method", target.prototype[method]);
logger.log(`LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`);
LoggedRoute()(target.prototype, method, { LoggedRoute()(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
Reflect.defineMetadata("path", path, target.prototype[method]);
Reflect.defineMetadata("method", httpMethod, target.prototype[method]);
} }
}); });
(0, common_1.Controller)(param)(target); (0, common_1.Controller)(param)(target);
@ -102,15 +115,16 @@ function LoggedRoute(route) {
return (_target, key, descriptor) => { return (_target, key, descriptor) => {
loggerInit(_target); loggerInit(_target);
const logger = _target.logger; const logger = _target.logger;
let fullRoute = `${_target.constructor.name}/`;
const fn = descriptor.value; const fn = descriptor.value;
const httpPath = Reflect.getMetadata("path", fn);
const httpMethod = Reflect.getMetadata("method", fn);
const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${RevRequestMethod[httpMethod]}]`;
if (!fn || typeof fn !== "function") { if (!fn || typeof fn !== "function") {
logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`); logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`);
return; return;
} }
_target[key] = async function (...args) { _target[key] = async function (...args) {
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key); const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key);
fullRoute += route || Reflect.getMetadata("path", fn);
if (typeof scopedLoggerInjectableParam !== "undefined" && if (typeof scopedLoggerInjectableParam !== "undefined" &&
(args.length <= scopedLoggerInjectableParam || (args.length <= scopedLoggerInjectableParam ||
!(args[scopedLoggerInjectableParam] instanceof logger_1.ScopedLogger))) { !(args[scopedLoggerInjectableParam] instanceof logger_1.ScopedLogger))) {
@ -139,6 +153,7 @@ function LoggedRoute(route) {
throw e; throw e;
} }
}; };
return [httpPath, httpMethod];
}; };
} }
exports.LoggedRoute = LoggedRoute; exports.LoggedRoute = LoggedRoute;

View File

@ -9,6 +9,19 @@ import { ScopedLogger } from "./logger";
import { LoggedParamReflectData } from "./reflected"; import { LoggedParamReflectData } from "./reflected";
import { loggedParam, scopedLogger } from "./reflected"; import { loggedParam, scopedLogger } from "./reflected";
import objectContainedLogged from "./functions"; import objectContainedLogged from "./functions";
import { RequestMethod } from "@nestjs/common";
const RevRequestMethod = [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"ALL",
"OPTIONS",
"HEAD",
"SEARCH",
];
function loggerInit(_target: any) { function loggerInit(_target: any) {
if (!Object.getOwnPropertyNames(_target).includes("logger")) { if (!Object.getOwnPropertyNames(_target).includes("logger")) {
@ -63,18 +76,24 @@ export function LoggedController(param?: any): (target: any) => void {
const methods = Object.getOwnPropertyNames(target.prototype); const methods = Object.getOwnPropertyNames(target.prototype);
logger.log(JSON.stringify(methods))
methods.forEach((method) => { methods.forEach((method) => {
logger.log(method)
if ( if (
method !== "constructor" && method !== "constructor" &&
typeof target.prototype[method] === "function" typeof target.prototype[method] === "function"
) { ) {
logger.log(`LoggedRoute applied to ${method}`); const path = Reflect.getMetadata("path", target.prototype[method]);
const httpMethod = Reflect.getMetadata(
"method",
target.prototype[method]
);
logger.log(
`LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`
);
LoggedRoute()(target.prototype, method, { LoggedRoute()(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
Reflect.defineMetadata("path", path, target.prototype[method]);
Reflect.defineMetadata("method", httpMethod, target.prototype[method]);
} }
}); });
@ -100,7 +119,7 @@ export function LoggedFunction<F extends Array<any>, R>(
return; return;
} }
_target[key] = async function(...args: F) { _target[key] = async function (...args: F) {
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata( const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
scopedLogger, scopedLogger,
_target, _target,
@ -132,22 +151,23 @@ export function LoggedFunction<F extends Array<any>, R>(
); );
injectedLogger.log( injectedLogger.log(
`CALL ${key} ${loggedParams && loggedParams.length > 0 `CALL ${key} ${
? "WITH " + loggedParams && loggedParams.length > 0
( ? "WITH " +
await Promise.all( (
loggedParams.map( await Promise.all(
async ({ name, index, include, exclude }) => loggedParams.map(
name + async ({ name, index, include, exclude }) =>
"=" + name +
(await objectContainedLogged(args[index], { "=" +
include, (await objectContainedLogged(args[index], {
exclude, include,
})) exclude,
) }))
) )
).join(", ") )
: "" ).join(", ")
: ""
}` }`
); );
@ -167,14 +187,20 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
_target: any, _target: any,
key: string, key: string,
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>> descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>
) => { ): [string, RequestMethod] => {
loggerInit(_target); loggerInit(_target);
const logger = _target.logger; const logger = _target.logger;
let fullRoute = `${_target.constructor.name}/`;
const fn = descriptor.value; const fn = descriptor.value;
const httpPath: string = Reflect.getMetadata("path", fn);
const httpMethod: RequestMethod = Reflect.getMetadata("method", fn);
const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${
RevRequestMethod[httpMethod]
}]`;
if (!fn || typeof fn !== "function") { if (!fn || typeof fn !== "function") {
logger.warn( logger.warn(
`LoggedRoute decorator applied to non-function property: ${key}` `LoggedRoute decorator applied to non-function property: ${key}`
@ -182,15 +208,13 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
return; return;
} }
_target[key] = async function(...args: F) { _target[key] = async function (...args: F) {
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata( const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
scopedLogger, scopedLogger,
_target, _target,
key key
); );
fullRoute += route || Reflect.getMetadata("path", fn);
if ( if (
typeof scopedLoggerInjectableParam !== "undefined" && typeof scopedLoggerInjectableParam !== "undefined" &&
(args.length <= scopedLoggerInjectableParam || (args.length <= scopedLoggerInjectableParam ||
@ -211,22 +235,23 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
); );
injectedLogger.log( injectedLogger.log(
`HIT HTTP ${fullRoute} (${key}) ${loggedParams && loggedParams.length > 0 `HIT HTTP ${fullRoute} (${key}) ${
? "WITH " + loggedParams && loggedParams.length > 0
( ? "WITH " +
await Promise.all( (
loggedParams.map( await Promise.all(
async ({ name, index, include, exclude }) => loggedParams.map(
name + async ({ name, index, include, exclude }) =>
"=" + name +
(await objectContainedLogged(args[index], { "=" +
include, (await objectContainedLogged(args[index], {
exclude, include,
})) exclude,
) }))
) )
).join(", ") )
: "" ).join(", ")
: ""
}` }`
); );
@ -239,5 +264,7 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
throw e; throw e;
} }
}; };
return [httpPath, httpMethod];
}; };
} }