From b6e828a00303bfd799fc7e5a6db92413e52f3b84 Mon Sep 17 00:00:00 2001 From: p-sw Date: Sun, 31 Mar 2024 20:43:35 +0900 Subject: [PATCH] chore: v2.2.8 build --- dist/lib/functions.d.ts | 10 ++++++ dist/lib/functions.js | 67 ++++++++++++++++++++++++++++++++++++++++- dist/lib/logged.js | 10 +++--- dist/lib/logger.d.ts | 8 ++--- dist/lib/logger.js | 17 +++++++---- src/test/index.ts | 7 +++-- 6 files changed, 100 insertions(+), 19 deletions(-) diff --git a/dist/lib/functions.d.ts b/dist/lib/functions.d.ts index adead74..6ff8586 100644 --- a/dist/lib/functions.d.ts +++ b/dist/lib/functions.d.ts @@ -1,7 +1,17 @@ export declare const notIncludedSymbol: unique symbol; +export declare function includeObjectSync(ocv: any, opt: { + paths: string[]; +}): any; +export declare function excludeObjectSync(ocv: any, opt: { + paths: string[]; +}): any; export declare function includeOrExcludeObjectSync(ocv: any, paths: string[], currentPath: string[], include: boolean): any; export declare function objectContainedLoggedSync(ocv: any, options?: { include?: string[]; exclude: string[]; }): string; +export declare function imObjectContainedLogSync(ocv: any, options?: { + include?: string[]; + exclude?: string[]; +}): string; export declare function getItemByPathSync(obj: object, path: string | string[]): any; diff --git a/dist/lib/functions.js b/dist/lib/functions.js index 8431df1..931e2a3 100644 --- a/dist/lib/functions.js +++ b/dist/lib/functions.js @@ -1,7 +1,55 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.getItemByPathSync = exports.objectContainedLoggedSync = exports.includeOrExcludeObjectSync = exports.notIncludedSymbol = void 0; +exports.getItemByPathSync = exports.imObjectContainedLogSync = exports.objectContainedLoggedSync = exports.includeOrExcludeObjectSync = exports.excludeObjectSync = exports.includeObjectSync = exports.notIncludedSymbol = void 0; exports.notIncludedSymbol = Symbol("notIncluded"); +function includeObjectSync(ocv, opt) { + let current = Array.isArray(ocv) ? [] : typeof ocv === 'object' ? {} : ocv; + opt.paths.forEach((dotpath) => { + let query = ocv; + let objRef = current; + const path = dotpath.split('.'); + for (const [index, key] of Object.entries(path)) { + query = query[key]; + if (query !== undefined && objRef[key] === undefined) { + if (typeof query === 'object') { + if (Array.isArray(query)) { + objRef[key] = []; + } + else { + objRef[key] = {}; + } + } + } + if (typeof query !== 'object' || index === (path.length - 1).toString()) { + objRef[key] = query; + break; + } + objRef = objRef[key]; + } + }); + return current; +} +exports.includeObjectSync = includeObjectSync; +function excludeObjectSync(ocv, opt) { + const copied = typeof ocv === 'object' ? Array.isArray(ocv) ? [...ocv] : { ...ocv } : ocv; + opt.paths.forEach((dotpath) => { + let objRef = copied; + const path = dotpath.split('.'); + const lastIndex = (path.length - 1).toString(); + for (const [index, key] of Object.entries(path)) { + if (index === lastIndex) { + delete objRef[key]; + break; + } + objRef = objRef[key]; + if (typeof objRef !== 'object') { + break; + } + } + }); + return copied; +} +exports.excludeObjectSync = excludeObjectSync; function includeOrExcludeObjectSync(ocv, paths, currentPath = [], include // or exclude ) { if (Array.isArray(ocv)) { @@ -40,6 +88,23 @@ function objectContainedLoggedSync(ocv, options) { } } exports.objectContainedLoggedSync = objectContainedLoggedSync; +function imObjectContainedLogSync(ocv, options) { + if (options && typeof ocv === 'object' && ocv !== null) { + if (options.include && options.include.length > 0) { + return JSON.stringify(includeObjectSync(ocv, { paths: options.include })); + } + if (options.exclude && options.exclude.length > 0) { + return JSON.stringify(excludeObjectSync(ocv, { paths: options.exclude })); + } + } + if (typeof ocv === "object" && ocv !== null) { + return JSON.stringify(ocv); + } + else { + return `${ocv}`; + } +} +exports.imObjectContainedLogSync = imObjectContainedLogSync; function getItemByPathSync(obj, path) { const paths = Array.isArray(path) ? path : path.split("."); return Object.keys(obj).includes(paths[0]) diff --git a/dist/lib/logged.js b/dist/lib/logged.js index 26419ae..1536596 100644 --- a/dist/lib/logged.js +++ b/dist/lib/logged.js @@ -79,10 +79,10 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData if (typeof metadatas.scopedLoggerInjectableParam !== "undefined") { if (args.length <= metadatas.scopedLoggerInjectableParam || !(args[metadatas.scopedLoggerInjectableParam] instanceof logger_1.ScopedLogger)) { - args[metadatas.scopedLoggerInjectableParam] = new logger_1.ScopedLogger(baseLogger, key, true, true); + args[metadatas.scopedLoggerInjectableParam] = logger_1.ScopedLogger.fromRoot(baseLogger, key); } else { - args[metadatas.scopedLoggerInjectableParam] = new logger_1.ScopedLogger(args[metadatas.scopedLoggerInjectableParam], key, false); + args[metadatas.scopedLoggerInjectableParam] = logger_1.ScopedLogger.fromSuper(baseLogger, args[metadatas.scopedLoggerInjectableParam], key); } injectedLogger = args[metadatas.scopedLoggerInjectableParam]; } @@ -90,7 +90,7 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData ? "WITH " + metadatas.loggedParams.map(({ name, index, include, exclude }) => name + "=" + - (0, functions_1.objectContainedLoggedSync)(args[index], { + (0, functions_1.imObjectContainedLogSync)(args[index], { include, exclude, })).join(", ") @@ -101,7 +101,7 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData (r && typeof r === 'object' && typeof r['then'] === 'function')) { return r['then']((r) => { const resultLogged = Array.isArray(returnsData) - ? typeof r === "object" + ? typeof r === "object" && r !== null ? "WITH " + returnsData.map(({ name, path }) => { const value = (0, functions_1.getItemByPathSync)(r, path); @@ -125,7 +125,7 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData } else { const resultLogged = Array.isArray(returnsData) - ? typeof r === "object" + ? typeof r === "object" && r !== null ? "WITH " + returnsData.map(({ name, path }) => { const value = (0, functions_1.getItemByPathSync)(r, path); diff --git a/dist/lib/logger.d.ts b/dist/lib/logger.d.ts index 9b7dad3..e92ed3f 100644 --- a/dist/lib/logger.d.ts +++ b/dist/lib/logger.d.ts @@ -2,10 +2,8 @@ import { Logger } from "@nestjs/common"; export declare class ScopedLogger extends Logger { private logger; private scope; - private root; - private createScopeId; - private readonly scopeId?; - constructor(logger: Logger, scope: string, root?: boolean, createScopeId?: boolean); + private scopeId; + constructor(logger: Logger, scope: string[], scopeId?: string); private scopedLog; debug: (message: string) => void; log: (message: string) => void; @@ -13,4 +11,6 @@ export declare class ScopedLogger extends Logger { verbose: (message: string) => void; error: (message: string) => void; fatal: (message: string) => void; + static fromSuper(baseLogger: Logger, logger: ScopedLogger, scope: string): ScopedLogger; + static fromRoot(logger: Logger, scope: string): ScopedLogger; } diff --git a/dist/lib/logger.js b/dist/lib/logger.js index dab56e3..b385f73 100644 --- a/dist/lib/logger.js +++ b/dist/lib/logger.js @@ -5,25 +5,30 @@ const common_1 = require("@nestjs/common"); const hyperid = require("hyperid"); const createId = hyperid({ fixedLength: true }); class ScopedLogger extends common_1.Logger { - constructor(logger, scope, root = false, createScopeId = false) { + constructor(logger, scope, scopeId = createId()) { super(); this.logger = logger; this.scope = scope; - this.root = root; - this.createScopeId = createScopeId; + 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"); - if (this.createScopeId) - this.scopeId = createId(); } scopedLog(method) { return (message) => { - this.logger[method](`${this.root ? "" : "-> "}${this.scope}${this.scopeId ? `(${this.scopeId})` : ""}: ${message}`); + this.logger[method](`${this.scopeId ? `(ID ${this.scopeId}) | ` : ""}${this.scope.join(" -> ")}: ${message}`); }; } + static fromSuper(baseLogger, logger, scope) { + return new ScopedLogger(baseLogger, [...logger.scope, scope], logger.scopeId); + } + ; + static fromRoot(logger, scope) { + return new ScopedLogger(logger, [scope]); + } + ; } exports.ScopedLogger = ScopedLogger; diff --git a/src/test/index.ts b/src/test/index.ts index 8290658..93c55b8 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -273,6 +273,7 @@ class LoggedMethodsClass { @InjectLogger logger?: ScopedLogger ) { logger.log(userId); + return { http: { result: "success", @@ -371,12 +372,12 @@ class LoggedMethodsClass { -const service = new TestService(); +// const service = new TestService(); /** * Choose Class to Test */ -const tester = new LoggedClass(service); +// const tester = new LoggedClass(service); // const tester = new LoggedMethodsClass(service); /** @@ -398,7 +399,7 @@ const tester = new LoggedClass(service); // void tester.testLoggerRootLogging(); // tester.testSyncLoggerRootLogging(); // tester.testSyncLogging(); -void tester.testService(); +// void tester.testService(); /** * Then run `yarn test`