feat: 1.2.0 build

This commit is contained in:
Shinwoo PARK 2023-12-03 20:03:23 +09:00
parent cccc446b80
commit 13b858c547
19 changed files with 184 additions and 67 deletions

86
dist/README.md vendored
View File

@ -1,4 +1,4 @@
# NestLogged
# NestLoggedDecorators
This package provides some decorations to make NestJS logging simpler.
It only uses Logger provided by @nestjs/common package and some dependencies required for nestjs.
@ -6,8 +6,8 @@ It only uses Logger provided by @nestjs/common package and some dependencies req
### Route Logging
```ts
import {Controller, Get} from "@nestjs/common";
import {LoggedRoute} from "nlogdec";
import { Controller, Get } from "@nestjs/common";
import { LoggedRoute } from "nlogdec";
@Controller('whatever')
export class WhateverController {
@ -31,15 +31,15 @@ It will automatically log the call and response.
If function throws any exception, it will also catch exception, log that, and throw it again.
```ts
import {BadRequestException, Controller, Get} from "@nestjs/common";
import {LoggedRoute} from "nlogdec";
import { BadRequestException, Controller, Get } from "@nestjs/common";
import { LoggedRoute } from "nlogdec";
@Controller('whatever')
export class WhateverController {
constructor() {}
@Get('/you/like')
@LoggedRoute('/you/like')
@LoggedRoute()
public async whateverYouLikeImpl() {
throw new BadRequestException("I don't like this") // Throwing HTTP exception here
}
@ -53,9 +53,36 @@ export class WhateverController {
Not only HTTP exception, it will also catch all exceptions and log it.
If you want to provide another route instead of path you provided to method decorators like Get, Post, you can give a string to LoggedRoute decorator to replace it.
```ts
import { BadRequestException, Controller, Get } from "@nestjs/common";
import { LoggedRoute } from "nlogdec";
@Controller('whatever')
export class WhateverController {
constructor() {}
@Get('/you/like')
@LoggedRoute('you/like')
public async whateverYouLikeImpl() {
throw new BadRequestException("I don't like this") // Throwing HTTP exception here
}
}
```
```
[Nest] 000000 - 00/00/0000, 00:00:00 AM LOG [WhateverController] HIT HTTP WhateverController/you/like (whateverYouLikeImpl)
[Nest] 000000 - 00/00/0000, 00:00:00 AM LOG [WhateverController] WHILE HTTP WhateverController/you/like (whateverYouLikeImpl) ERROR BadRequestException: I don't like this
```
You feel the change?
Logged path is slightly changed from `WhateverController//you/like` to `WhateverController/you/like`.
### Function Logging
```ts
import {LoggedFunction} from "nlogdec";
import { LoggedFunction } from "nlogdec";
@LoggedFunction // This decorator will do the magic for you
export async function doILikeThis(stuff: "apple" | "banana"): "yes" | "no" {
@ -65,14 +92,14 @@ export async function doILikeThis(stuff: "apple" | "banana"): "yes" | "no" {
LoggedFunction decorator will log function calls and returns for you.
**Note: This decorator is expected to be used with a class method like Service. (we will upgrade that later, so you can use it without class)**
**Note: This decorator is expected to be used with a class method. You can't use this outside of class**
Like `LoggedRoute` decorator, it will automatically catch all exceptions, log it, and throw it again.
### Parameter Logging
```ts
import {LoggedParam, LoggedFunction} from "nlogdec";
import { LoggedParam, LoggedFunction } from "nlogdec";
@LoggedFunction
export async function doILikeThis(
@ -93,6 +120,45 @@ The name of parameter is decided by the first parameter of LoggedParam decorator
This decorator also can be used with `LoggedRoute`.
### Class Logging
You can make all method in injectable classes to logged function.
```ts
import { LoggedInjectable } from "nlogdec";
@LoggedInjectable()
export class InjectableService {
constructor() {}
public async getHello(@LoggedParam('name') name: string = 'world'): Promise<string> {
return `Hello, ${name}!`;
}
}
```
It will make all methods to logged function, so it internally uses LoggedFunction decorator.
You can do same thing with controller.
```ts
import { Get, Query } from "@nestjs/common";
import { LoggedController } from "nlogdec";
@LoggedController('path')
export class Controller {
constructor(private injectableService: InjectableService) {}
@Get('/hello')
public async getHello(@LoggedParam('name') @Query() query: { name: string }): Promise<string> {
return await this.injectableService.getHello(query.name);
}
}
```
It is exactly same using LoggedFunction and LoggedRoute, but it is much simpler because you don't have to write decorator to every method.
But still, if you don't want to log every method, you can use LoggedFunction and LoggedRoute decorator.
### Scoped Logging
You can do scoped logging with `InjectLogger` decorator.
@ -122,7 +188,7 @@ export async function doILikeThis(
Then, in controller:
```ts
import {BadRequestException, Controller, Get, Param} from "@nestjs/common";
import { BadRequestException, Controller, Get, Param } from "@nestjs/common";
import {
LoggedRoute,
InjectLogger,

View File

@ -1 +1 @@
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,eAAwB,CAAA;AAEtD,wBAAsB,sBAAsB,CACxC,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,MAAM,EAAE,EACf,WAAW,EAAE,MAAM,EAAO,EAC1B,OAAO,EAAE,OAAO,OA6CnB;AAED,wBAA8B,qBAAqB,CAC/C,GAAG,EAAE,GAAG,EACR,OAAO,CAAC,EAAE;IAAC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAA;CAAC,GAClD,OAAO,CAAC,MAAM,CAAC,CAmBjB"}
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,eAAwB,CAAC;AAEvD,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,MAAM,EAAE,EACf,WAAW,EAAE,MAAM,EAAO,EAC1B,OAAO,EAAE,OAAO,OA6CjB;AAED,wBAA8B,qBAAqB,CACjD,GAAG,EAAE,GAAG,EACR,OAAO,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAA;CAAE,GAClD,OAAO,CAAC,MAAM,CAAC,CAmBjB"}

13
dist/lib/functions.js vendored
View File

@ -1,18 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.includeOrExcludeObject = exports.notIncludedSymbol = void 0;
exports.notIncludedSymbol = Symbol('notIncluded');
async function includeOrExcludeObject(ocv, paths, currentPath = [], include) {
exports.notIncludedSymbol = Symbol("notIncluded");
async function includeOrExcludeObject(ocv, paths, currentPath = [], include // or exclude
) {
if (Array.isArray(ocv)) {
return (await Promise.all(ocv.map(async (v, i) => await includeOrExcludeObject(v, paths, [...currentPath, i.toString()], include)))).filter((e) => e !== exports.notIncludedSymbol);
}
if (typeof ocv === 'object') {
if (typeof ocv === "object") {
return Object.fromEntries((await Promise.all(Object.entries(ocv).map(async ([key, value]) => [
key,
await includeOrExcludeObject(value, paths, [...currentPath, key], include),
]))).filter((e) => e[1] !== exports.notIncludedSymbol));
}
const isIncluded = paths.includes(currentPath.join('.'));
const isIncluded = paths.includes(currentPath.join("."));
return include
? isIncluded // include mode, path is in list
? ocv
@ -23,7 +24,7 @@ async function includeOrExcludeObject(ocv, paths, currentPath = [], include) {
}
exports.includeOrExcludeObject = includeOrExcludeObject;
async function objectContainedLogged(ocv, options) {
if (options && typeof ocv === 'object') {
if (options && typeof ocv === "object") {
if (options.include && options.include.length > 0) {
return JSON.stringify(await includeOrExcludeObject(ocv, options.include, [], true));
}
@ -31,7 +32,7 @@ async function objectContainedLogged(ocv, options) {
return JSON.stringify(await includeOrExcludeObject(ocv, options.exclude, [], false));
}
}
if (typeof ocv === 'object') {
if (typeof ocv === "object") {
return JSON.stringify(ocv);
}
else {

View File

@ -1 +1 @@
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":";;;AAAa,QAAA,iBAAiB,GAAG,MAAM,CAAC,aAAa,CAAC,CAAA;AAE/C,KAAK,UAAU,sBAAsB,CACxC,GAAQ,EACR,KAAe,EACf,cAAwB,EAAE,EAC1B,OAAgB;IAEhB,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACpB,OAAO,CACH,MAAM,OAAO,CAAC,GAAG,CACb,GAAG,CAAC,GAAG,CACH,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CACX,MAAM,sBAAsB,CACxB,CAAC,EACD,KAAK,EACL,CAAC,GAAG,WAAW,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC,EAC9B,OAAO,CACV,CACR,CACJ,CACJ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,yBAAiB,CAAC,CAAC;KAC5C;IAED,IAAI,OAAO,GA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LoggedRoute, LoggedFunction } from './logged';
export { ScopedLogger } from './logger';
export { LoggedParamReflectData, InjectLogger, LoggedParam } from './reflected';
export { LoggedRoute, LoggedFunction, LoggedController, LoggedInjectable, } from "./logged";
export { ScopedLogger } from "./logger";
export { LoggedParamReflectData, InjectLogger, LoggedParam } from "./reflected";
//# sourceMappingURL=index.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,cAAc,EAAC,MAAM,UAAU,CAAC;AACrD,OAAO,EAAC,YAAY,EAAC,MAAM,UAAU,CAAC;AACtC,OAAO,EAAC,sBAAsB,EAAE,YAAY,EAAE,WAAW,EAAC,MAAM,aAAa,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,sBAAsB,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}

4
dist/lib/index.js vendored
View File

@ -1,9 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggedParam = exports.InjectLogger = exports.ScopedLogger = exports.LoggedFunction = exports.LoggedRoute = void 0;
exports.LoggedParam = exports.InjectLogger = exports.ScopedLogger = exports.LoggedInjectable = exports.LoggedController = exports.LoggedFunction = exports.LoggedRoute = void 0;
var logged_1 = require("./logged");
Object.defineProperty(exports, "LoggedRoute", { enumerable: true, get: function () { return logged_1.LoggedRoute; } });
Object.defineProperty(exports, "LoggedFunction", { enumerable: true, get: function () { return logged_1.LoggedFunction; } });
Object.defineProperty(exports, "LoggedController", { enumerable: true, get: function () { return logged_1.LoggedController; } });
Object.defineProperty(exports, "LoggedInjectable", { enumerable: true, get: function () { return logged_1.LoggedInjectable; } });
var logger_1 = require("./logger");
Object.defineProperty(exports, "ScopedLogger", { enumerable: true, get: function () { return logger_1.ScopedLogger; } });
var reflected_1 = require("./reflected");

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAqD;AAA7C,qGAAA,WAAW,OAAA;AAAE,wGAAA,cAAc,OAAA;AACnC,mCAAsC;AAA9B,sGAAA,YAAY,OAAA;AACpB,yCAA8E;AAA9C,yGAAA,YAAY,OAAA;AAAE,wGAAA,WAAW,OAAA"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAKkB;AAJhB,qGAAA,WAAW,OAAA;AACX,wGAAA,cAAc,OAAA;AACd,0GAAA,gBAAgB,OAAA;AAChB,0GAAA,gBAAgB,OAAA;AAElB,mCAAwC;AAA/B,sGAAA,YAAY,OAAA;AACrB,yCAAgF;AAA/C,yGAAA,YAAY,OAAA;AAAE,wGAAA,WAAW,OAAA"}

View File

@ -1,3 +1,8 @@
import { ControllerOptions, ScopeOptions } from "@nestjs/common";
export declare function LoggedInjectable(options?: ScopeOptions): (target: any) => void;
export declare function LoggedController(): (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 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>>) => void;
//# sourceMappingURL=logged.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"logged.d.ts","sourceRoot":"","sources":["../../src/logged.ts"],"names":[],"mappings":"AAmBA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MAAM,gDACmC,CAAC,KAAK,QAAQ,CAAC,CAAC,WAsEjE"}

91
dist/lib/logged.js vendored
View File

@ -1,15 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggedRoute = exports.LoggedFunction = void 0;
exports.LoggedRoute = exports.LoggedFunction = exports.LoggedController = exports.LoggedInjectable = void 0;
const common_1 = require("@nestjs/common");
const logger_1 = require("./logger");
const reflected_1 = require("./reflected");
const functions_1 = require("./functions");
function loggerInit(_target) {
if (!Object.getOwnPropertyNames(_target).includes('logger')) {
if (!Object.getOwnPropertyNames(_target).includes("logger")) {
const newTargetLogger = new common_1.Logger(_target.constructor.name);
newTargetLogger.log('Logger Initialized.');
Object.defineProperty(_target, 'logger', {
newTargetLogger.log("Logger Initialized.");
Object.defineProperty(_target, "logger", {
writable: false,
enumerable: false,
configurable: false,
@ -17,33 +17,73 @@ function loggerInit(_target) {
});
}
}
function LoggedInjectable(options) {
return (target) => {
target = (0, common_1.Injectable)(options)(target);
loggerInit(target.prototype);
const logger = target.prototype.logger;
const methods = Object.getOwnPropertyNames(target.prototype);
methods.forEach((method) => {
if (method !== "constructor" &&
typeof target.prototype[method] === "function") {
logger.log(`LoggedFunction applied to ${method}`);
LoggedFunction(target.prototype, method, {
value: target.prototype[method],
});
}
});
};
}
exports.LoggedInjectable = LoggedInjectable;
function LoggedController(param) {
return (target) => {
target = (0, common_1.Controller)(param)(target);
loggerInit(target.prototype);
const logger = target.prototype.logger;
const methods = Object.getOwnPropertyNames(target.prototype);
methods.forEach((method) => {
if (method !== "constructor" &&
typeof target.prototype[method] === "function") {
logger.log(`LoggedRoute applied to ${method}`);
LoggedRoute()(target.prototype, method, {
value: target.prototype[method],
});
}
});
};
}
exports.LoggedController = LoggedController;
function LoggedFunction(_target, key, descriptor) {
loggerInit(_target);
const logger = _target.logger;
const fn = descriptor.value;
if (!fn)
if (!fn || typeof fn !== "function") {
logger.warn(`LoggedFunction decorator applied to non-function property: ${key}`);
return;
descriptor.value = async function (...args) {
}
_target[key] = async function (...args) {
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key);
if (typeof scopedLoggerInjectableParam !== 'undefined' &&
if (typeof scopedLoggerInjectableParam !== "undefined" &&
(args.length <= scopedLoggerInjectableParam ||
!(args[scopedLoggerInjectableParam] instanceof logger_1.ScopedLogger))) {
args[scopedLoggerInjectableParam] = new logger_1.ScopedLogger(logger, key);
}
else if (typeof scopedLoggerInjectableParam !== 'undefined') {
else if (typeof scopedLoggerInjectableParam !== "undefined") {
args[scopedLoggerInjectableParam] = new logger_1.ScopedLogger(args[scopedLoggerInjectableParam], key);
}
const injectedLogger = typeof scopedLoggerInjectableParam !== 'undefined'
const injectedLogger = typeof scopedLoggerInjectableParam !== "undefined"
? args[scopedLoggerInjectableParam]
: logger;
const loggedParams = Reflect.getOwnMetadata(reflected_1.loggedParam, _target, key);
injectedLogger.log(`CALL ${key} ${loggedParams && loggedParams.length > 0
? 'WITH ' +
(await Promise.all(loggedParams.map(async ({ name, index, include, exclude }) => name + '=' + (await (0, functions_1.default)(args[index], {
include,
exclude,
}))))).join(', ')
: ''}`);
? "WITH " +
(await Promise.all(loggedParams.map(async ({ name, index, include, exclude }) => name +
"=" +
(await (0, functions_1.default)(args[index], {
include,
exclude,
}))))).join(", ")
: ""}`);
try {
const r = await fn.call(this, ...args);
injectedLogger.log(`RETURNED ${key}`);
@ -60,28 +100,31 @@ function LoggedRoute(route) {
return (_target, key, descriptor) => {
loggerInit(_target);
const logger = _target.logger;
const fullRoute = `${_target.constructor.name}/${route}`;
let fullRoute = `${_target.constructor.name}/`;
const fn = descriptor.value;
if (!fn)
return;
descriptor.value = async function (...args) {
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key);
if (typeof scopedLoggerInjectableParam !== 'undefined' &&
fullRoute += route || Reflect.getMetadata("path", fn);
if (typeof scopedLoggerInjectableParam !== "undefined" &&
(args.length <= scopedLoggerInjectableParam ||
!(args[scopedLoggerInjectableParam] instanceof logger_1.ScopedLogger))) {
args[scopedLoggerInjectableParam] = new logger_1.ScopedLogger(logger, fullRoute);
}
const injectedLogger = typeof scopedLoggerInjectableParam !== 'undefined'
const injectedLogger = typeof scopedLoggerInjectableParam !== "undefined"
? args[scopedLoggerInjectableParam]
: logger;
const loggedParams = Reflect.getOwnMetadata(reflected_1.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 (0, functions_1.default)(args[index], {
include,
exclude,
}))))).join(', ')
: ''}`);
? "WITH " +
(await Promise.all(loggedParams.map(async ({ name, index, include, exclude }) => name +
"=" +
(await (0, functions_1.default)(args[index], {
include,
exclude,
}))))).join(", ")
: ""}`);
try {
const r = await fn.call(this, ...args);
injectedLogger.log(`RETURNED RESPONSE ${fullRoute} (${key})`);

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,gBAAgB,CAAC;AAItC,qBAAa,YAAa,SAAQ,MAAM;IAEhC,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,OAAO,CAAC;gBAFR,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,MAAM;IAK5B,OAAO,CAAC,SAAS;IAUjB,KAAK,YATgB,MAAM,UASK;IAChC,GAAG,YAVkB,MAAM,UAUC;IAC5B,IAAI,YAXiB,MAAM,UAWG;IAC9B,OAAO,YAZc,MAAM,UAYS;IACpC,KAAK,YAbgB,MAAM,UAaK;IAChC,KAAK,YAdgB,MAAM,UAcK;CACnC"}
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAIxC,qBAAa,YAAa,SAAQ,MAAM;IAEpC,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,OAAO,CAAC;gBAFR,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,MAAM;IAK1B,OAAO,CAAC,SAAS;IAQjB,KAAK,YAPc,MAAM,UAOO;IAChC,GAAG,YARgB,MAAM,UAQG;IAC5B,IAAI,YATe,MAAM,UASK;IAC9B,OAAO,YAVY,MAAM,UAUW;IACpC,KAAK,YAXc,MAAM,UAWO;IAChC,KAAK,YAZc,MAAM,UAYO;CACjC"}

14
dist/lib/logger.js vendored
View File

@ -8,16 +8,16 @@ class ScopedLogger extends common_1.Logger {
this.logger = logger;
this.scope = scope;
this.scopeId = scopeId;
this.debug = this.scopedLog('debug');
this.log = this.scopedLog('log');
this.warn = this.scopedLog('warn');
this.verbose = this.scopedLog('verbose');
this.error = this.scopedLog('error');
this.fatal = this.scopedLog('fatal');
this.debug = this.scopedLog("debug");
this.log = this.scopedLog("log");
this.warn = this.scopedLog("warn");
this.verbose = this.scopedLog("verbose");
this.error = this.scopedLog("error");
this.fatal = this.scopedLog("fatal");
}
scopedLog(method) {
return (message) => {
this.logger[method](`-> ${this.scope}${this.scopeId ? `(${this.scopeId})` : ''}: ${message}`);
this.logger[method](`-> ${this.scope}${this.scopeId ? `(${this.scopeId})` : ""}: ${message}`);
};
}
}

View File

@ -1 +1 @@
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":";;;AAAA,2CAAsC;AAItC,MAAa,YAAa,SAAQ,eAAM;IACpC,YACY,MAAc,EACd,KAAa,EACb,OAAgB;QAExB,KAAK,EAAE,CAAC;QAJA,WAAM,GAAN,MAAM,CAAQ;QACd,UAAK,GAAL,KAAK,CAAQ;QACb,YAAO,GAAP,OAAO,CAAS;QAe5B,UAAK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AO,CAAC,CAAC;QAChC,UAAK,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAfhC,CAAC;IAEO,SAAS,CAAC,MAAgB;QAChC,OAAO,CAAC,OAAe,EAAE,EAAE;YACzB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CACjB,MAAM,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,EAAE,CACzE,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;CAQF;AAvBD,oCAuBC"}

View File

@ -1 +1 @@
{"version":3,"file":"reflected.d.ts","sourceRoot":"","sources":["../../src/reflected.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,sBAAsB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED,eAAO,MAAM,YAAY,eAAyB,CAAC;AACnD,eAAO,MAAM,WAAW,eAAwB,CAAC;AAGjD,wBAAgB,YAAY,CACxB,MAAM,EAAE,GAAG,EACX,WAAW,EAAE,MAAM,GAAG,MAAM,EAC5B,cAAc,EAAE,MAAM,QAGzB;AAED,wBAAgB,WAAW,CACvB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;IACN,WAAW,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,CAAC;IACpC,WAAW,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,CAAC;CACvC,YAGW,GAAG,eACE,MAAM,GAAG,MAAM,kBACZ,MAAM,UAyB7B"}
{"version":3,"file":"reflected.d.ts","sourceRoot":"","sources":["../../src/reflected.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,eAAO,MAAM,YAAY,eAAyB,CAAC;AACnD,eAAO,MAAM,WAAW,eAAwB,CAAC;AAEjD,wBAAgB,YAAY,CAC1B,MAAM,EAAE,GAAG,EACX,WAAW,EAAE,MAAM,GAAG,MAAM,EAC5B,cAAc,EAAE,MAAM,QAGvB;AAED,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;IACR,WAAW,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,CAAC;IACpC,WAAW,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,CAAC;CACrC,YAGS,GAAG,eACE,MAAM,GAAG,MAAM,kBACZ,MAAM,UAyBzB"}

View File

@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggedParam = exports.InjectLogger = exports.loggedParam = exports.scopedLogger = void 0;
exports.scopedLogger = Symbol('scopedLogger');
exports.loggedParam = Symbol('loggedParam');
exports.scopedLogger = Symbol("scopedLogger");
exports.loggedParam = Symbol("loggedParam");
function InjectLogger(target, propertyKey, parameterIndex) {
Reflect.defineMetadata(exports.scopedLogger, parameterIndex, target, propertyKey);
}
@ -15,10 +15,10 @@ function LoggedParam(name, options) {
index: parameterIndex,
include: options &&
options.includePath &&
options.includePath.map((v) => (Array.isArray(v) ? v.join('.') : v)),
options.includePath.map((v) => (Array.isArray(v) ? v.join(".") : v)),
exclude: options &&
options.excludePath &&
options.excludePath.map((v) => (Array.isArray(v) ? v.join('.') : v)),
options.excludePath.map((v) => (Array.isArray(v) ? v.join(".") : v)),
});
Reflect.defineMetadata(exports.loggedParam, existingLoggedParams, target, propertyKey);
};

View File

@ -1 +1 @@
{"version":3,"file":"reflected.js","sourceR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WAAW,CACZ,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAnCD,kCAmCC"}

2
dist/package.json vendored
View File

@ -1,6 +1,6 @@
{
"name": "nlogdec",
"version": "1.1.2",
"version": "1.2.0",
"description": "A NestJS Logger Decorator Library",
"main": "lib/index.js",
"repository": "https://github.com/worplo/nestlogged",