-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
172 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { deflateCategories, mergeCategories } from './categories'; | ||
import { isActionable, isError, isObject, isPromiseLike } from '../utils'; | ||
import { isSelfDebug } from '../is-debug'; | ||
import type { ThreadGroupConfig } from '../streams'; | ||
import type { ThreadID } from '../types'; | ||
import { isActionable, isError, isObject, isPromiseLike } from '../utils'; | ||
import type { | ||
BunyaminLogMethod, | ||
BunyaminConfig, | ||
|
@@ -10,6 +11,7 @@ import type { | |
BunyanLogLevel, | ||
} from './types'; | ||
import { MessageStack } from './message-stack'; | ||
import { StackTraceError } from './StackTraceError'; | ||
|
||
export class Bunyamin<Logger extends BunyanLikeLogger = BunyanLikeLogger> { | ||
public readonly fatal = this.#setupLogMethod('fatal'); | ||
|
@@ -33,7 +35,7 @@ export class Bunyamin<Logger extends BunyanLikeLogger = BunyanLikeLogger> { | |
this.#fields = undefined; | ||
this.#shared = { | ||
...config, | ||
threadGroups: config.threadGroups ?? [], | ||
loggerPriority: 0, | ||
messageStack: new MessageStack({ | ||
noBeginMessage: config.noBeginMessage, | ||
}), | ||
|
@@ -44,15 +46,20 @@ export class Bunyamin<Logger extends BunyanLikeLogger = BunyanLikeLogger> { | |
} | ||
} | ||
|
||
/** @deprecated */ | ||
Check warning on line 49 in src/decorator/Bunyamin.ts GitHub Actions / Lint
|
||
get threadGroups(): ThreadGroupConfig[] { | ||
return this.#shared.threadGroups!; | ||
return []; | ||
} | ||
|
||
get logger(): Logger { | ||
return this.#shared.logger; | ||
} | ||
|
||
set logger(logger: Logger) { | ||
this.useLogger(logger); | ||
} | ||
|
||
useLogger(logger: Logger, priority = 0): void { | ||
if (this.#shared.immutable) { | ||
throw new Error('Cannot change a logger of an immutable instance'); | ||
} | ||
|
@@ -61,7 +68,23 @@ export class Bunyamin<Logger extends BunyanLikeLogger = BunyanLikeLogger> { | |
throw new Error('Cannot change a logger of a child instance'); | ||
} | ||
|
||
this.#shared.logger = logger; | ||
const { stack } = isSelfDebug() ? new StackTraceError() : StackTraceError.empty(); | ||
const currentPriority = this.#shared.loggerPriority; | ||
if (priority >= currentPriority) { | ||
this.#shared.loggerPriority = priority; | ||
this.#shared.logger = logger; | ||
stack && | ||
this.#shared.logger.trace( | ||
{ cat: 'bunyamin' }, | ||
`bunyamin logger changed (${priority} >= ${currentPriority}), caller was:\n${stack}`, | ||
); | ||
} else { | ||
stack && | ||
this.#shared.logger.trace( | ||
{ cat: 'bunyamin' }, | ||
`bunyamin logger not changed (${priority} < ${currentPriority}), caller was:\n${stack}`, | ||
); | ||
} | ||
} | ||
|
||
child(overrides?: UserFields): Bunyamin<Logger> { | ||
|
@@ -226,5 +249,6 @@ type ResolvedFields = UserFields & { | |
}; | ||
|
||
type SharedBunyaminConfig<Logger extends BunyanLikeLogger> = BunyaminConfig<Logger> & { | ||
loggerPriority: number; | ||
messageStack: MessageStack; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export class StackTraceError extends Error { | ||
constructor() { | ||
super('Providing stack trace below:'); | ||
// eslint-disable-next-line unicorn/custom-error-definition | ||
this.name = 'StackTrace'; | ||
} | ||
|
||
static empty() { | ||
return { | ||
message: '', | ||
stack: '', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { createIsDebug } from './createIsDebug'; | ||
|
||
export const isDebug = createIsDebug(process.env.DEBUG || ''); | ||
|
||
export const isSelfDebug = () => isDebug('bunyamin'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { beforeEach, describe, expect, jest, it } from '@jest/globals'; | ||
import type { ThreadGroups } from './ThreadGroups'; | ||
import { wrapLogger } from '../wrapLogger'; | ||
import type { Bunyamin } from '../decorator'; | ||
|
||
describe('ThreadGroups', () => { | ||
let ThreadGroups: new (logger: Bunyamin) => ThreadGroups; | ||
let threadGroups: ThreadGroups; | ||
let isDebug: jest.Mocked<any>; | ||
let logger: Bunyamin; | ||
|
||
beforeEach(() => { | ||
jest.mock('../is-debug'); | ||
isDebug = jest.requireMock<any>('../is-debug'); | ||
ThreadGroups = jest.requireActual<any>('./ThreadGroups').ThreadGroups; | ||
logger = wrapLogger({ | ||
trace: jest.fn(), | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
fatal: jest.fn(), | ||
}); | ||
}); | ||
|
||
describe('in regular mode', () => { | ||
beforeEach(() => { | ||
isDebug.isSelfDebug.mockReturnValue(false); | ||
threadGroups = new ThreadGroups(logger); | ||
}); | ||
|
||
it('should be empty by default', () => { | ||
expect([...threadGroups]).toEqual([]); | ||
}); | ||
|
||
it('should add a thread group', () => { | ||
const group = { id: 'foo', displayName: 'Foo' }; | ||
threadGroups.add(group); | ||
expect([...threadGroups]).toEqual([group]); | ||
}); | ||
|
||
it('should not call logger.trace', () => { | ||
expect(logger.logger.trace).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('in debug mode', () => { | ||
beforeEach(() => { | ||
isDebug.isSelfDebug.mockReturnValue(true); | ||
threadGroups = new ThreadGroups(logger); | ||
}); | ||
|
||
it('should call logger.trace upon addition', () => { | ||
const group = { id: 'foo', displayName: 'Foo' }; | ||
threadGroups.add(group); | ||
expect(logger.logger.trace).toHaveBeenCalledWith( | ||
{ cat: 'bunyamin' }, | ||
expect.stringContaining(__filename), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Bunyamin } from '../decorator'; | ||
import type { ThreadGroupConfig } from '../streams'; | ||
import { isSelfDebug } from '../is-debug'; | ||
import { StackTraceError } from '../decorator/StackTraceError'; | ||
|
||
export class ThreadGroups { | ||
readonly #bunyamin: Bunyamin; | ||
readonly #debugMode = isSelfDebug(); | ||
readonly #groups = new Map<string, ThreadGroupConfig>(); | ||
|
||
constructor(bunyamin: Bunyamin) { | ||
this.#bunyamin = bunyamin; | ||
this.#groups = new Map(); | ||
} | ||
|
||
add(group: ThreadGroupConfig) { | ||
if (this.#debugMode) { | ||
if (this.#groups.has(group.id)) { | ||
this.#logAddition(group, 'overwritten'); | ||
} else { | ||
this.#logAddition(group, 'added'); | ||
} | ||
} | ||
|
||
this.#groups.set(group.id, group); | ||
return this; | ||
} | ||
|
||
[Symbol.iterator]() { | ||
return this.#groups.values(); | ||
} | ||
|
||
#logAddition(group: ThreadGroupConfig, action: string) { | ||
const { stack } = new StackTraceError(); | ||
this.#bunyamin.trace( | ||
{ cat: 'bunyamin' }, | ||
`thread group ${action}: ${group.id} (${group.displayName})\n\n${stack}`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ThreadGroups'; |