From 85fc7d65029111401757f032e0690ca1a032829e Mon Sep 17 00:00:00 2001 From: Andrew Stacy Date: Fri, 13 Sep 2024 15:26:56 -0400 Subject: [PATCH] fix: resolves bug where middleware was being mistakenly duplicated --- src/configuration.ts | 32 ++++++++++++++++++-------------- src/log.ts | 4 ++-- test/seal.test.ts | 23 ++++++++++++++++++++++- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/configuration.ts b/src/configuration.ts index 6d0f083..7819e58 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -29,6 +29,10 @@ export class Configuration implements IConfiguration { this.glblCfg = globalThis.$adzeGlobal?.configuration; } + public updateConfiguration(cfg: UserConfiguration): void { + this.logCfg = cfg; + } + public get activeLevel(): Level | number { return this.glblCfg?.activeLevel ?? this.logCfg.activeLevel ?? dfltCfg.activeLevel; } @@ -147,20 +151,20 @@ export class Configuration implements IConfiguration { public exportValues(): UserConfiguration { return { - activeLevel: this.activeLevel, - cache: this.cache, - cacheSize: this.cacheSize, - dump: this.dump, - meta: this.meta, - silent: this.silent, - showTimestamp: this.showTimestamp, - withEmoji: this.withEmoji, - format: this.format, - levels: this.levels, - middleware: this.middleware, - filters: this.filters, - timestampFormatter: this.timestampFormatter, - formatters: this.formatters, + activeLevel: this.logCfg.activeLevel, + cache: this.logCfg.cache, + cacheSize: this.logCfg.cacheSize, + dump: this.logCfg.dump, + meta: this.logCfg.meta, + silent: this.logCfg.silent, + showTimestamp: this.logCfg.showTimestamp, + withEmoji: this.logCfg.withEmoji, + format: this.logCfg.format, + levels: this.logCfg.levels, + middleware: this.logCfg.middleware, + filters: this.logCfg.filters, + timestampFormatter: this.logCfg.timestampFormatter, + formatters: this.logCfg.formatters, }; } } diff --git a/src/log.ts b/src/log.ts index 0033eb6..2e0be13 100644 --- a/src/log.ts +++ b/src/log.ts @@ -435,8 +435,8 @@ export default class Log { * sealed.log('Another log.'); // -> prints "#sealed [sealed-label] Another log." * ``` */ - public seal(cfg?: UserConfiguration) { - this._cfg = new Configuration({ ...this._cfg.exportValues(), ...cfg }); + public seal(_cfg?: UserConfiguration) { + if (_cfg) this._cfg.updateConfiguration(_cfg); return SealedLog(Log, this._cfg, this.modifierData, this.modifierQueue); } diff --git a/test/seal.test.ts b/test/seal.test.ts index 163a261..af07e54 100644 --- a/test/seal.test.ts +++ b/test/seal.test.ts @@ -1,5 +1,6 @@ import { afterEach, describe, expect, test, vi } from 'vitest'; -import adze, { teardown } from '../src'; +import adze, { Middleware, setup, teardown } from '../src'; +import Log from '../src/log'; /** * @vitest-environment happy-dom @@ -28,4 +29,24 @@ describe('Configuration', () => { 'This is a log from a twice sealed logger.' ); }); + + test('the same number of middlewares are inherited after sealing', () => { + console.log = vi.fn(); + + const store = setup({ + middleware: [{} as Middleware], + }); + + store.addListener('*', (log: Log) => { + expect(log.configuration.middleware?.length).toBe(2); + }); + + const logger1 = adze.ns('l1').seal(); + const logger2 = logger1.ns('l2').seal({ + middleware: [{} as Middleware], + }); + const logger3 = logger2.ns('l3').seal(); + + logger3.log('This is a log from a sealed logger.'); + }); });