Skip to content

Commit

Permalink
feat: export global instance as default
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Nov 3, 2023
1 parent 01cffd6 commit 3a079a8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/decorator/Bunyamin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ describe('Bunyamin', () => {

test('bunyamin.logger', () => {
expect(bunyamin.logger).toBe(logger);

bunyamin.logger = new MockLogger();
expect(bunyamin.logger).not.toBe(logger);

const child = bunyamin.child();
expect(child.logger).toBe(bunyamin.logger);
expect(() => (child.logger = new MockLogger())).toThrow();
});

test.each(LEVELS)('bunyamin.%s(message)', (level) => {
Expand Down
8 changes: 8 additions & 0 deletions src/decorator/Bunyamin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export class Bunyamin<Logger extends BunyanLikeLogger = BunyanLikeLogger> {
return this.#shared.logger;
}

set logger(logger: Logger) {
if (this.#fields) {
throw new Error('Cannot change logger of child instance');
}

this.#shared.logger = logger;
}

child(overrides?: UserFields): Bunyamin<Logger> {
const childContext = this.#mergeFields(this.#fields, this.#transformContext(overrides));
return new Bunyamin(this.#shared, childContext as never);
Expand Down
6 changes: 6 additions & 0 deletions src/decorator/BunyaminGlobal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Bunyamin } from './Bunyamin';
import type { BunyanLikeLogger } from './types';

export type BunyaminGlobal = Bunyamin<BunyanLikeLogger> & {
setLogger: (logger: BunyanLikeLogger) => void;
};
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Bunyamin } from './decorator';
import { noopLogger } from './noopLogger';

export * from './noopLogger';
export * from './traceEventStream';
export * from './uniteTraceEvents';
export * from './wrapLogger';

export default new Bunyamin({ logger: noopLogger() });
20 changes: 11 additions & 9 deletions src/noopLogger/noopLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const noop: any = () => {
/* no-op */
};

export function noopLogger(_options?: any): BunyanLikeLogger {
return {
fatal: noop,
error: noop,
warn: noop,
info: noop,
debug: noop,
trace: noop,
};
export class NoopLogger implements BunyanLikeLogger {
fatal = noop;
error = noop;
warn = noop;
info = noop;
debug = noop;
trace = noop;
}

export function noopLogger(_options?: any) {

Check warning on line 16 in src/noopLogger/noopLogger.ts

View workflow job for this annotation

GitHub Actions / Lint

'_options' is defined but never used

Check warning on line 16 in src/noopLogger/noopLogger.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
return new NoopLogger();
}

0 comments on commit 3a079a8

Please sign in to comment.