Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export global instance as default #7

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,16 +1,18 @@
import type { BunyanLikeLogger } from '../decorator';

const noop: any = () => {

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

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
/* 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();
}
Loading