style: prettified
This commit is contained in:
parent
185e22cb84
commit
a8f7cb4f95
284
src/logged.ts
284
src/logged.ts
@ -1,164 +1,168 @@
|
|||||||
import {Logger} from "@nestjs/common";
|
import { Logger } from "@nestjs/common";
|
||||||
import {ScopedLogger} from "./logger";
|
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";
|
||||||
|
|
||||||
function loggerInit(_target: any) {
|
function loggerInit(_target: any) {
|
||||||
if (!Object.getOwnPropertyNames(_target).includes('logger')) {
|
if (!Object.getOwnPropertyNames(_target).includes("logger")) {
|
||||||
const newTargetLogger = new Logger(_target.constructor.name);
|
const newTargetLogger = new Logger(_target.constructor.name);
|
||||||
newTargetLogger.log('Logger Initialized.');
|
newTargetLogger.log("Logger Initialized.");
|
||||||
Object.defineProperty(_target, 'logger', {
|
Object.defineProperty(_target, "logger", {
|
||||||
writable: false,
|
writable: false,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
configurable: false,
|
configurable: false,
|
||||||
value: newTargetLogger,
|
value: newTargetLogger,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LoggedFunction<F extends Array<any>, R>(
|
export function LoggedFunction<F extends Array<any>, R>(
|
||||||
|
_target: any,
|
||||||
|
key: string,
|
||||||
|
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>
|
||||||
|
) {
|
||||||
|
loggerInit(_target);
|
||||||
|
|
||||||
|
const logger = _target.logger;
|
||||||
|
|
||||||
|
const fn = descriptor.value;
|
||||||
|
|
||||||
|
if (!fn) return;
|
||||||
|
|
||||||
|
descriptor.value = async function (...args: F) {
|
||||||
|
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
|
||||||
|
scopedLogger,
|
||||||
|
_target,
|
||||||
|
key
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof scopedLoggerInjectableParam !== "undefined" &&
|
||||||
|
(args.length <= scopedLoggerInjectableParam ||
|
||||||
|
!(args[scopedLoggerInjectableParam] instanceof ScopedLogger))
|
||||||
|
) {
|
||||||
|
args[scopedLoggerInjectableParam] = new ScopedLogger(logger, key);
|
||||||
|
} else if (typeof scopedLoggerInjectableParam !== "undefined") {
|
||||||
|
args[scopedLoggerInjectableParam] = new ScopedLogger(
|
||||||
|
args[scopedLoggerInjectableParam],
|
||||||
|
key
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const injectedLogger =
|
||||||
|
typeof scopedLoggerInjectableParam !== "undefined"
|
||||||
|
? args[scopedLoggerInjectableParam]
|
||||||
|
: logger;
|
||||||
|
|
||||||
|
const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata(
|
||||||
|
loggedParam,
|
||||||
|
_target,
|
||||||
|
key
|
||||||
|
);
|
||||||
|
|
||||||
|
injectedLogger.log(
|
||||||
|
`CALL ${key} ${
|
||||||
|
loggedParams && loggedParams.length > 0
|
||||||
|
? "WITH " +
|
||||||
|
(
|
||||||
|
await Promise.all(
|
||||||
|
loggedParams.map(
|
||||||
|
async ({ name, index, include, exclude }) =>
|
||||||
|
name +
|
||||||
|
"=" +
|
||||||
|
(await objectContainedLogged(args[index], {
|
||||||
|
include,
|
||||||
|
exclude,
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).join(", ")
|
||||||
|
: ""
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const r: R = await fn.call(this, ...args);
|
||||||
|
injectedLogger.log(`RETURNED ${key}`);
|
||||||
|
return r;
|
||||||
|
} catch (e) {
|
||||||
|
injectedLogger.error(`WHILE ${key} ERROR ${e}`);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function LoggedRoute<F extends Array<any>, R>(route: string) {
|
||||||
|
return (
|
||||||
_target: any,
|
_target: any,
|
||||||
key: string,
|
key: string,
|
||||||
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>,
|
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>
|
||||||
) {
|
) => {
|
||||||
loggerInit(_target);
|
loggerInit(_target);
|
||||||
|
|
||||||
const logger = _target.logger;
|
const logger = _target.logger;
|
||||||
|
|
||||||
|
const fullRoute = `${_target.constructor.name}/${route}`;
|
||||||
const fn = descriptor.value;
|
const fn = descriptor.value;
|
||||||
|
|
||||||
if (!fn) return;
|
if (!fn) return;
|
||||||
|
|
||||||
descriptor.value = async function (...args: F) {
|
descriptor.value = async function (...args: F) {
|
||||||
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
|
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
|
||||||
scopedLogger,
|
scopedLogger,
|
||||||
_target,
|
_target,
|
||||||
key,
|
key
|
||||||
);
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof scopedLoggerInjectableParam !== 'undefined' &&
|
typeof scopedLoggerInjectableParam !== "undefined" &&
|
||||||
(args.length <= scopedLoggerInjectableParam ||
|
(args.length <= scopedLoggerInjectableParam ||
|
||||||
!(args[scopedLoggerInjectableParam] instanceof ScopedLogger))
|
!(args[scopedLoggerInjectableParam] instanceof ScopedLogger))
|
||||||
) {
|
) {
|
||||||
args[scopedLoggerInjectableParam] = new ScopedLogger(logger, key);
|
args[scopedLoggerInjectableParam] = new ScopedLogger(logger, fullRoute);
|
||||||
} else if (typeof scopedLoggerInjectableParam !== 'undefined') {
|
}
|
||||||
args[scopedLoggerInjectableParam] = new ScopedLogger(
|
|
||||||
args[scopedLoggerInjectableParam],
|
|
||||||
key,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const injectedLogger=
|
const injectedLogger =
|
||||||
typeof scopedLoggerInjectableParam !== 'undefined'
|
typeof scopedLoggerInjectableParam !== "undefined"
|
||||||
? args[scopedLoggerInjectableParam]
|
? args[scopedLoggerInjectableParam]
|
||||||
: logger;
|
: logger;
|
||||||
|
|
||||||
const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata(
|
const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata(
|
||||||
loggedParam,
|
loggedParam,
|
||||||
_target,
|
_target,
|
||||||
key,
|
key
|
||||||
)
|
);
|
||||||
|
|
||||||
injectedLogger.log(
|
injectedLogger.log(
|
||||||
`CALL ${key} ${
|
`HIT HTTP ${fullRoute} (${key}) ${
|
||||||
loggedParams && loggedParams.length > 0
|
loggedParams && loggedParams.length > 0
|
||||||
? 'WITH ' +
|
? "WITH " +
|
||||||
(
|
(
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
loggedParams.map(
|
loggedParams.map(
|
||||||
async ({ name, index, include, exclude }) =>
|
async ({ name, index, include, exclude }) =>
|
||||||
name + '=' + (await objectContainedLogged(args[index], {
|
name +
|
||||||
include,
|
"=" +
|
||||||
exclude,
|
(await objectContainedLogged(args[index], {
|
||||||
})),
|
include,
|
||||||
),
|
exclude,
|
||||||
)
|
}))
|
||||||
).join(', ')
|
)
|
||||||
: ''
|
)
|
||||||
}`,
|
).join(", ")
|
||||||
);
|
: ""
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const r: R = await fn.call(this, ...args);
|
const r: R = await fn.call(this, ...args);
|
||||||
injectedLogger.log(`RETURNED ${key}`);
|
injectedLogger.log(`RETURNED RESPONSE ${fullRoute} (${key})`);
|
||||||
return r;
|
return r;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
injectedLogger.error(`WHILE ${key} ERROR ${e}`);
|
injectedLogger.error(`WHILE HTTP ${fullRoute} (${key}) ERROR ${e}`);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LoggedRoute<F extends Array<any>, R>(route: string) {
|
|
||||||
return (
|
|
||||||
_target: any,
|
|
||||||
key: string,
|
|
||||||
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>,
|
|
||||||
) => {
|
|
||||||
loggerInit(_target);
|
|
||||||
|
|
||||||
const logger = _target.logger;
|
|
||||||
|
|
||||||
const fullRoute = `${_target.constructor.name}/${route}`;
|
|
||||||
const fn = descriptor.value;
|
|
||||||
|
|
||||||
if (!fn) return;
|
|
||||||
|
|
||||||
descriptor.value = async function (...args: F) {
|
|
||||||
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
|
|
||||||
scopedLogger,
|
|
||||||
_target,
|
|
||||||
key,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
typeof scopedLoggerInjectableParam !== 'undefined' &&
|
|
||||||
(args.length <= scopedLoggerInjectableParam ||
|
|
||||||
!(args[scopedLoggerInjectableParam] instanceof ScopedLogger))
|
|
||||||
) {
|
|
||||||
args[scopedLoggerInjectableParam] = new ScopedLogger(logger, fullRoute);
|
|
||||||
}
|
|
||||||
|
|
||||||
const injectedLogger=
|
|
||||||
typeof scopedLoggerInjectableParam !== 'undefined'
|
|
||||||
? args[scopedLoggerInjectableParam]
|
|
||||||
: logger;
|
|
||||||
|
|
||||||
const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata(
|
|
||||||
loggedParam,
|
|
||||||
_target,
|
|
||||||
key,
|
|
||||||
)
|
|
||||||
|
|
||||||
injectedLogger.log(
|
|
||||||
`HIT HTTP ${fullRoute} (${key}) ${
|
|
||||||
loggedParams && loggedParams.length > 0
|
|
||||||
? 'WITH ' +
|
|
||||||
(
|
|
||||||
await Promise.all(
|
|
||||||
loggedParams.map(
|
|
||||||
async ({ name, index, include, exclude }) =>
|
|
||||||
name + '=' + (await objectContainedLogged(args[index], {
|
|
||||||
include,
|
|
||||||
exclude,
|
|
||||||
})),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
).join(', ')
|
|
||||||
: ''
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const r: R = await fn.call(this, ...args);
|
|
||||||
injectedLogger.log(`RETURNED RESPONSE ${fullRoute} (${key})`);
|
|
||||||
return r;
|
|
||||||
} catch (e) {
|
|
||||||
injectedLogger.error(`WHILE HTTP ${fullRoute} (${key}) ERROR ${e}`);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user