style: prettified
This commit is contained in:
parent
185e22cb84
commit
a8f7cb4f95
@ -1,14 +1,14 @@
|
|||||||
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,
|
||||||
@ -20,7 +20,7 @@ function loggerInit(_target: any) {
|
|||||||
export function LoggedFunction<F extends Array<any>, R>(
|
export function LoggedFunction<F extends Array<any>, R>(
|
||||||
_target: any,
|
_target: any,
|
||||||
key: string,
|
key: string,
|
||||||
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>,
|
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>
|
||||||
) {
|
) {
|
||||||
loggerInit(_target);
|
loggerInit(_target);
|
||||||
|
|
||||||
@ -34,50 +34,52 @@ export function LoggedFunction<F extends Array<any>, R>(
|
|||||||
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, key);
|
||||||
} else if (typeof scopedLoggerInjectableParam !== 'undefined') {
|
} else if (typeof scopedLoggerInjectableParam !== "undefined") {
|
||||||
args[scopedLoggerInjectableParam] = new ScopedLogger(
|
args[scopedLoggerInjectableParam] = new ScopedLogger(
|
||||||
args[scopedLoggerInjectableParam],
|
args[scopedLoggerInjectableParam],
|
||||||
key,
|
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} ${
|
`CALL ${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 +
|
||||||
|
"=" +
|
||||||
|
(await objectContainedLogged(args[index], {
|
||||||
include,
|
include,
|
||||||
exclude,
|
exclude,
|
||||||
})),
|
}))
|
||||||
),
|
|
||||||
)
|
)
|
||||||
).join(', ')
|
)
|
||||||
: ''
|
).join(", ")
|
||||||
}`,
|
: ""
|
||||||
|
}`
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -95,7 +97,7 @@ export function LoggedRoute<F extends Array<any>, R>(route: string) {
|
|||||||
return (
|
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);
|
||||||
|
|
||||||
@ -110,45 +112,47 @@ export function LoggedRoute<F extends Array<any>, R>(route: string) {
|
|||||||
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, fullRoute);
|
args[scopedLoggerInjectableParam] = new ScopedLogger(logger, fullRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
||||||
`HIT HTTP ${fullRoute} (${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 +
|
||||||
|
"=" +
|
||||||
|
(await objectContainedLogged(args[index], {
|
||||||
include,
|
include,
|
||||||
exclude,
|
exclude,
|
||||||
})),
|
}))
|
||||||
),
|
|
||||||
)
|
)
|
||||||
).join(', ')
|
)
|
||||||
: ''
|
).join(", ")
|
||||||
}`,
|
: ""
|
||||||
|
}`
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -160,5 +164,5 @@ export function LoggedRoute<F extends Array<any>, R>(route: string) {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user