v3.0.0 build
This commit is contained in:
parent
8cd91db037
commit
04e5b82c7d
@ -17,4 +17,4 @@ yarn add nestlogged
|
|||||||
|
|
||||||
## More Info
|
## More Info
|
||||||
|
|
||||||
[Github Wiki](https://github.com/Worplo/nestlogged/wiki)
|
[Wiki](https://nestlogged.worplo.com)
|
2
dist/README.md
vendored
2
dist/README.md
vendored
@ -17,4 +17,4 @@ yarn add nestlogged
|
|||||||
|
|
||||||
## More Info
|
## More Info
|
||||||
|
|
||||||
[Github Wiki](https://github.com/Worplo/nestlogged/wiki)
|
[Wiki](https://nestlogged.worplo.com)
|
10
dist/lib/logged.d.ts
vendored
10
dist/lib/logged.d.ts
vendored
@ -7,5 +7,11 @@ export declare function LoggedController(prefix: string | string[]): (target: an
|
|||||||
export declare function LoggedController(options: ControllerOptions & {
|
export declare function LoggedController(options: ControllerOptions & {
|
||||||
verbose?: boolean;
|
verbose?: boolean;
|
||||||
}): (target: any) => void;
|
}): (target: any) => void;
|
||||||
export declare function LoggedFunction<F extends Array<any>, R>(_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>>): void;
|
interface OverrideBuildOptions {
|
||||||
export declare function LoggedRoute<F extends Array<any>, R>(route?: string): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R>) => void;
|
skipCallLog: boolean;
|
||||||
|
skipReturnLog: boolean;
|
||||||
|
skipErrorLog: boolean;
|
||||||
|
}
|
||||||
|
export declare function LoggedFunction<F extends Array<any>, R>(options?: Partial<OverrideBuildOptions>): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>>) => void;
|
||||||
|
export declare function LoggedRoute<F extends Array<any>, R>(route?: string, options?: Partial<OverrideBuildOptions>): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R>) => void;
|
||||||
|
export {};
|
||||||
|
178
dist/lib/logged.js
vendored
178
dist/lib/logged.js
vendored
@ -4,7 +4,6 @@ exports.LoggedRoute = exports.LoggedFunction = exports.LoggedController = export
|
|||||||
const common_1 = require("@nestjs/common");
|
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 reflected_2 = require("./reflected");
|
|
||||||
const functions_1 = require("./functions");
|
const functions_1 = require("./functions");
|
||||||
const RevRequestMethod = [
|
const RevRequestMethod = [
|
||||||
"GET",
|
"GET",
|
||||||
@ -39,7 +38,7 @@ function LoggedInjectable(options) {
|
|||||||
typeof target.prototype[method] === "function") {
|
typeof target.prototype[method] === "function") {
|
||||||
if (options && options.verbose)
|
if (options && options.verbose)
|
||||||
logger.log(`LoggedFunction applied to ${method}`);
|
logger.log(`LoggedFunction applied to ${method}`);
|
||||||
LoggedFunction(target.prototype, method, {
|
LoggedFunction()(target.prototype, method, {
|
||||||
value: target.prototype[method],
|
value: target.prototype[method],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -73,7 +72,23 @@ function LoggedController(param) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
exports.LoggedController = LoggedController;
|
exports.LoggedController = LoggedController;
|
||||||
function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData, route) {
|
const defaultOverrideBuildOptions = {
|
||||||
|
skipCallLog: false,
|
||||||
|
skipReturnLog: false,
|
||||||
|
skipErrorLog: false,
|
||||||
|
};
|
||||||
|
class LoggedMetadata {
|
||||||
|
constructor(options) {
|
||||||
|
this.options = options ?? defaultOverrideBuildOptions;
|
||||||
|
}
|
||||||
|
updateOption(options) {
|
||||||
|
this.options = {
|
||||||
|
...this.options,
|
||||||
|
...options
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData, logged, route) {
|
||||||
return function (...args) {
|
return function (...args) {
|
||||||
let injectedLogger = baseLogger;
|
let injectedLogger = baseLogger;
|
||||||
if (typeof metadatas.scopedLoggerInjectableParam !== "undefined") {
|
if (typeof metadatas.scopedLoggerInjectableParam !== "undefined") {
|
||||||
@ -86,20 +101,47 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData
|
|||||||
}
|
}
|
||||||
injectedLogger = args[metadatas.scopedLoggerInjectableParam];
|
injectedLogger = args[metadatas.scopedLoggerInjectableParam];
|
||||||
}
|
}
|
||||||
injectedLogger.log(`${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key} ${metadatas.loggedParams && metadatas.loggedParams.length > 0
|
if (!logged.options.skipCallLog) {
|
||||||
? "WITH " +
|
injectedLogger.log(`${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key} ${metadatas.loggedParams && metadatas.loggedParams.length > 0
|
||||||
metadatas.loggedParams.map(({ name, index, include, exclude }) => name +
|
? "WITH " +
|
||||||
"=" +
|
metadatas.loggedParams.map(({ name, index, include, exclude }) => name +
|
||||||
(0, functions_1.imObjectContainedLogSync)(args[index], {
|
"=" +
|
||||||
include,
|
(0, functions_1.imObjectContainedLogSync)(args[index], {
|
||||||
exclude,
|
include,
|
||||||
})).join(", ")
|
exclude,
|
||||||
: ""}`);
|
})).join(", ")
|
||||||
|
: ""}`);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const r = originalFunction.call(this, ...args);
|
const r = originalFunction.call(this, ...args);
|
||||||
if (originalFunction.constructor.name === 'AsyncFunction' ||
|
if (!logged.options.skipReturnLog) {
|
||||||
(r && typeof r === 'object' && typeof r['then'] === 'function')) {
|
if (originalFunction.constructor.name === 'AsyncFunction' ||
|
||||||
return r['then']((r) => {
|
(r && typeof r === 'object' && typeof r['then'] === 'function')) {
|
||||||
|
return r['then']((r) => {
|
||||||
|
const resultLogged = Array.isArray(returnsData)
|
||||||
|
? typeof r === "object" && r !== null
|
||||||
|
? "WITH " +
|
||||||
|
returnsData.map(({ name, path }) => {
|
||||||
|
const value = (0, functions_1.getItemByPathSync)(r, path);
|
||||||
|
return value !== undefined ? `${name}=${value}` : "";
|
||||||
|
})
|
||||||
|
.filter((v) => v.length > 0)
|
||||||
|
.join(", ")
|
||||||
|
: ""
|
||||||
|
: typeof returnsData === 'string'
|
||||||
|
? "WITH " + returnsData + "=" + typeof r === "object" ? JSON.stringify(r) : r
|
||||||
|
: returnsData
|
||||||
|
? typeof r === "object"
|
||||||
|
? "WITH " + JSON.stringify(r)
|
||||||
|
: "WITH " + r
|
||||||
|
: "";
|
||||||
|
injectedLogger.log(route
|
||||||
|
? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}`
|
||||||
|
: `RETURNED ${key} ${resultLogged}`);
|
||||||
|
return r;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
const resultLogged = Array.isArray(returnsData)
|
const resultLogged = Array.isArray(returnsData)
|
||||||
? typeof r === "object" && r !== null
|
? typeof r === "object" && r !== null
|
||||||
? "WITH " +
|
? "WITH " +
|
||||||
@ -121,68 +163,60 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData
|
|||||||
? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}`
|
? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}`
|
||||||
: `RETURNED ${key} ${resultLogged}`);
|
: `RETURNED ${key} ${resultLogged}`);
|
||||||
return r;
|
return r;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const resultLogged = Array.isArray(returnsData)
|
|
||||||
? typeof r === "object" && r !== null
|
|
||||||
? "WITH " +
|
|
||||||
returnsData.map(({ name, path }) => {
|
|
||||||
const value = (0, functions_1.getItemByPathSync)(r, path);
|
|
||||||
return value !== undefined ? `${name}=${value}` : "";
|
|
||||||
})
|
|
||||||
.filter((v) => v.length > 0)
|
|
||||||
.join(", ")
|
|
||||||
: ""
|
|
||||||
: typeof returnsData === 'string'
|
|
||||||
? "WITH " + returnsData + "=" + typeof r === "object" ? JSON.stringify(r) : r
|
|
||||||
: returnsData
|
|
||||||
? typeof r === "object"
|
|
||||||
? "WITH " + JSON.stringify(r)
|
|
||||||
: "WITH " + r
|
|
||||||
: "";
|
|
||||||
injectedLogger.log(route
|
|
||||||
? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}`
|
|
||||||
: `RETURNED ${key} ${resultLogged}`);
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
injectedLogger.error(`WHILE ${route ? `HTTP ${route.fullRoute} (${key})` : key} ERROR ${e}`);
|
if (!logged.options.skipErrorLog) {
|
||||||
|
injectedLogger.error(`WHILE ${route ? `HTTP ${route.fullRoute} (${key})` : key} ERROR ${e}`);
|
||||||
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
function LoggedFunction(_target, key, descriptor) {
|
function LoggedFunction(options) {
|
||||||
loggerInit(_target);
|
return (_target, key, descriptor) => {
|
||||||
const logger = _target.logger;
|
loggerInit(_target);
|
||||||
const fn = descriptor.value;
|
const logger = _target.logger;
|
||||||
if (!fn || typeof fn !== "function") {
|
const fn = descriptor.value;
|
||||||
logger.warn(`LoggedFunction decorator applied to non-function property: ${key}`);
|
if (!fn || typeof fn !== "function") {
|
||||||
return;
|
logger.warn(`LoggedFunction decorator applied to non-function property: ${key}`);
|
||||||
}
|
return;
|
||||||
const all = Reflect.getMetadataKeys(fn).map((k) => [
|
}
|
||||||
k,
|
const logMetadata = Reflect.getOwnMetadata(reflected_1.nestLoggedMetadata, _target, key);
|
||||||
Reflect.getMetadata(k, fn),
|
if (logMetadata) {
|
||||||
]);
|
// already applied, override instead
|
||||||
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key);
|
logMetadata.updateOption(options);
|
||||||
const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key);
|
return;
|
||||||
const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key);
|
}
|
||||||
const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn);
|
const newMetadata = new LoggedMetadata(options);
|
||||||
const overrideFunction = overrideBuild(fn, logger, {
|
const all = Reflect.getMetadataKeys(fn).map((k) => [
|
||||||
scopedLoggerInjectableParam,
|
k,
|
||||||
loggedParams,
|
Reflect.getMetadata(k, fn),
|
||||||
scopeKeys,
|
]);
|
||||||
}, key, returnsData);
|
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key);
|
||||||
_target[key] = overrideFunction;
|
const loggedParams = Reflect.getOwnMetadata(reflected_1.loggedParam, _target, key);
|
||||||
descriptor.value = overrideFunction;
|
const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key);
|
||||||
all.forEach(([k, v]) => {
|
const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn);
|
||||||
Reflect.defineMetadata(k, v, _target[key]);
|
const overrideFunction = overrideBuild(fn, logger, {
|
||||||
Reflect.defineMetadata(k, v, descriptor.value);
|
scopedLoggerInjectableParam,
|
||||||
});
|
loggedParams,
|
||||||
|
scopeKeys,
|
||||||
|
}, key, returnsData, newMetadata, undefined);
|
||||||
|
_target[key] = overrideFunction;
|
||||||
|
descriptor.value = overrideFunction;
|
||||||
|
Reflect.defineMetadata(reflected_1.nestLoggedMetadata, newMetadata, _target, key);
|
||||||
|
all.forEach(([k, v]) => {
|
||||||
|
Reflect.defineMetadata(k, v, _target[key]);
|
||||||
|
Reflect.defineMetadata(k, v, descriptor.value);
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
exports.LoggedFunction = LoggedFunction;
|
exports.LoggedFunction = LoggedFunction;
|
||||||
function LoggedRoute(route) {
|
function LoggedRoute(route, options) {
|
||||||
return (_target, key, descriptor) => {
|
return (_target, key, descriptor) => {
|
||||||
loggerInit(_target);
|
loggerInit(_target);
|
||||||
const logger = _target.logger;
|
const logger = _target.logger;
|
||||||
@ -191,6 +225,13 @@ function LoggedRoute(route) {
|
|||||||
logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`);
|
logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const logMetadata = Reflect.getOwnMetadata(reflected_1.nestLoggedMetadata, _target, key);
|
||||||
|
if (logMetadata) {
|
||||||
|
// already applied, override instead
|
||||||
|
logMetadata.updateOption(options);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const newMetadata = new LoggedMetadata(options);
|
||||||
const all = Reflect.getMetadataKeys(fn).map((k) => [
|
const all = Reflect.getMetadataKeys(fn).map((k) => [
|
||||||
k,
|
k,
|
||||||
Reflect.getMetadata(k, fn),
|
Reflect.getMetadata(k, fn),
|
||||||
@ -198,19 +239,20 @@ function LoggedRoute(route) {
|
|||||||
const httpPath = Reflect.getMetadata("path", fn);
|
const httpPath = Reflect.getMetadata("path", fn);
|
||||||
const httpMethod = Reflect.getMetadata("method", fn);
|
const httpMethod = Reflect.getMetadata("method", fn);
|
||||||
const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${RevRequestMethod[httpMethod]}]`;
|
const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${RevRequestMethod[httpMethod]}]`;
|
||||||
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key);
|
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key);
|
||||||
const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key);
|
const loggedParams = Reflect.getOwnMetadata(reflected_1.loggedParam, _target, key);
|
||||||
const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key);
|
const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key);
|
||||||
const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn);
|
const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn);
|
||||||
const overrideFunction = overrideBuild(fn, logger, {
|
const overrideFunction = overrideBuild(fn, logger, {
|
||||||
scopedLoggerInjectableParam,
|
scopedLoggerInjectableParam,
|
||||||
loggedParams,
|
loggedParams,
|
||||||
scopeKeys,
|
scopeKeys,
|
||||||
}, key, returnsData, {
|
}, key, returnsData, newMetadata, {
|
||||||
fullRoute,
|
fullRoute,
|
||||||
});
|
});
|
||||||
_target[key] = overrideFunction;
|
_target[key] = overrideFunction;
|
||||||
descriptor.value = overrideFunction;
|
descriptor.value = overrideFunction;
|
||||||
|
Reflect.defineMetadata(reflected_1.nestLoggedMetadata, newMetadata, _target, key);
|
||||||
all.forEach(([k, v]) => {
|
all.forEach(([k, v]) => {
|
||||||
Reflect.defineMetadata(k, v, _target[key]);
|
Reflect.defineMetadata(k, v, _target[key]);
|
||||||
Reflect.defineMetadata(k, v, descriptor.value);
|
Reflect.defineMetadata(k, v, descriptor.value);
|
||||||
|
1
dist/lib/reflected.d.ts
vendored
1
dist/lib/reflected.d.ts
vendored
@ -25,6 +25,7 @@ export declare const scopedLogger: unique symbol;
|
|||||||
export declare const loggedParam: unique symbol;
|
export declare const loggedParam: unique symbol;
|
||||||
export declare const scopeKey: unique symbol;
|
export declare const scopeKey: unique symbol;
|
||||||
export declare const returns: unique symbol;
|
export declare const returns: unique symbol;
|
||||||
|
export declare const nestLoggedMetadata: unique symbol;
|
||||||
export declare function InjectLogger(target: any, propertyKey: string | symbol, parameterIndex: number): void;
|
export declare function InjectLogger(target: any, propertyKey: string | symbol, parameterIndex: number): void;
|
||||||
type ParameterDecoratorType = (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
|
type ParameterDecoratorType = (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
|
||||||
type LoggedParamReturns = (name: string, options?: IncludeExcludePath) => ParameterDecoratorType;
|
type LoggedParamReturns = (name: string, options?: IncludeExcludePath) => ParameterDecoratorType;
|
||||||
|
3
dist/lib/reflected.js
vendored
3
dist/lib/reflected.js
vendored
@ -1,6 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.Returns = exports.LoggedHeaders = exports.LoggedBody = exports.LoggedQuery = exports.LoggedParam = exports.Logged = exports.InjectLogger = exports.returns = exports.scopeKey = exports.loggedParam = exports.scopedLogger = void 0;
|
exports.Returns = exports.LoggedHeaders = exports.LoggedBody = exports.LoggedQuery = exports.LoggedParam = exports.Logged = exports.InjectLogger = exports.nestLoggedMetadata = exports.returns = exports.scopeKey = exports.loggedParam = exports.scopedLogger = void 0;
|
||||||
const route_paramtypes_enum_1 = require("@nestjs/common/enums/route-paramtypes.enum");
|
const route_paramtypes_enum_1 = require("@nestjs/common/enums/route-paramtypes.enum");
|
||||||
const common_1 = require("@nestjs/common");
|
const common_1 = require("@nestjs/common");
|
||||||
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
||||||
@ -22,6 +22,7 @@ exports.scopedLogger = Symbol("nlogdec-scopedLogger");
|
|||||||
exports.loggedParam = Symbol("nlogdec-loggedParam");
|
exports.loggedParam = Symbol("nlogdec-loggedParam");
|
||||||
exports.scopeKey = Symbol("nlogdec-scopeKey");
|
exports.scopeKey = Symbol("nlogdec-scopeKey");
|
||||||
exports.returns = Symbol("nlogdec-returns");
|
exports.returns = Symbol("nlogdec-returns");
|
||||||
|
exports.nestLoggedMetadata = Symbol("nlogdec-metadata");
|
||||||
function InjectLogger(target, propertyKey, parameterIndex) {
|
function InjectLogger(target, propertyKey, parameterIndex) {
|
||||||
Reflect.defineMetadata(exports.scopedLogger, parameterIndex, target, propertyKey);
|
Reflect.defineMetadata(exports.scopedLogger, parameterIndex, target, propertyKey);
|
||||||
}
|
}
|
||||||
|
2
dist/package.json
vendored
2
dist/package.json
vendored
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nestlogged",
|
"name": "nestlogged",
|
||||||
"version": "2.2.8",
|
"version": "3.0.0",
|
||||||
"description": "A NestJS Logger Decorator Library",
|
"description": "A NestJS Logger Decorator Library",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"repository": "https://github.com/worplo/nestlogged",
|
"repository": "https://github.com/worplo/nestlogged",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user