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: improved global instance #10

Merged
merged 1 commit into from
Nov 4, 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
3 changes: 3 additions & 0 deletions src/decorator/Bunyamin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ describe('Bunyamin', () => {
const child = bunyamin.child();
expect(child.logger).toBe(bunyamin.logger);
expect(() => (child.logger = new MockLogger())).toThrow();

const immutable = new Bunyamin({ logger, immutable: true });
expect(() => (immutable.logger = new MockLogger())).toThrow();
});

test.each(LEVELS)('bunyamin.%s(message)', (level) => {
Expand Down
12 changes: 11 additions & 1 deletion src/decorator/Bunyamin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { deflateCategories, mergeCategories } from './categories';
import { isActionable, isError, isObject, isPromiseLike } from '../utils';
import type { ThreadGroupConfig } from '../streams';
import type { ThreadID } from '../types';
import type {
BunyaminLogMethod,
Expand Down Expand Up @@ -32,6 +33,7 @@
this.#fields = undefined;
this.#shared = {
...config,
threadGroups: config.threadGroups ?? [],
messageStack: new MessageStack({
noBeginMessage: config.noBeginMessage,
}),
Expand All @@ -42,13 +44,21 @@
}
}

get threadGroups(): ThreadGroupConfig[] {
return this.#shared.threadGroups!;

Check warning on line 48 in src/decorator/Bunyamin.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
}

get logger(): Logger {
return this.#shared.logger;
}

set logger(logger: Logger) {
if (this.#shared.immutable) {
throw new Error('Cannot change a logger of an immutable instance');
}

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

this.#shared.logger = logger;
Expand Down
6 changes: 0 additions & 6 deletions src/decorator/BunyaminGlobal.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/decorator/types/BunyaminConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ThreadGroupConfig } from '../../streams';
import type { BunyaminLogRecordFields } from './BunyaminLogRecordFields';
import type { BunyanLikeLogger } from './BunyanLikeLogger';

Expand All @@ -6,6 +7,14 @@ export type BunyaminConfig<Logger extends BunyanLikeLogger> = {
* Underlying logger to be used.
*/
logger: Logger;
/**
* Forbids changing the logger after constuction.
*/
immutable?: boolean;
/**
* Thread groups to be used for grouping log records.
*/
threadGroups?: ThreadGroupConfig[];
/**
* Fallback message to be used when there was no previous message
* passed with {@link BunyaminLogMethod#begin}.
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@
export * from './wrapLogger';
export * from './is-debug';

export default new Bunyamin<BunyanLikeLogger>({ logger: noopLogger() });
const threadGroups: any[] = [];

Check warning on line 11 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
export const bunyamin = new Bunyamin<BunyanLikeLogger>({ logger: noopLogger(), threadGroups });
export const nobunyamin = new Bunyamin<BunyanLikeLogger>({
logger: noopLogger(),
threadGroups,
immutable: true,
});

export default bunyamin;
1 change: 1 addition & 0 deletions src/streams/bunyan-trace-event/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type { TraceEventStreamOptions } from './options';
export type { ThreadGroupConfig } from './threads';
export * from './BunyanTraceEventStream';
Loading