Skip to content

Commit

Permalink
feat: improved global instance
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Nov 4, 2023
1 parent 27f8851 commit 344dd2e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
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 @@ export class Bunyamin<Logger extends BunyanLikeLogger = BunyanLikeLogger> {
this.#fields = undefined;
this.#shared = {
...config,
threadGroups: config.threadGroups ?? [],
messageStack: new MessageStack({
noBeginMessage: config.noBeginMessage,
}),
Expand All @@ -42,13 +44,21 @@ export class Bunyamin<Logger extends BunyanLikeLogger = BunyanLikeLogger> {
}
}

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 './uniteTraceEvents';
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';

0 comments on commit 344dd2e

Please sign in to comment.