Skip to content

Commit

Permalink
fix: resolves bug where middleware was being mistakenly duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stacy committed Sep 13, 2024
1 parent 8ded8c1 commit 85fc7d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
32 changes: 18 additions & 14 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
};
}
}
4 changes: 2 additions & 2 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ export default class Log<N extends string = string, Msg = unknown> {
* sealed.log('Another log.'); // -> prints "#sealed [sealed-label] Another log."
* ```
*/
public seal<N extends string = string, M = unknown>(cfg?: UserConfiguration) {
this._cfg = new Configuration({ ...this._cfg.exportValues(), ...cfg });
public seal<N extends string = string, M = unknown>(_cfg?: UserConfiguration) {
if (_cfg) this._cfg.updateConfiguration(_cfg);
return SealedLog<N, M>(Log<N, M>, this._cfg, this.modifierData, this.modifierQueue);
}

Expand Down
23 changes: 22 additions & 1 deletion test/seal.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.');
});
});

0 comments on commit 85fc7d6

Please sign in to comment.