Skip to content

Commit

Permalink
refactor(lib): [createLogger] reporters
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Feb 1, 2025
1 parent 87fed82 commit 0f3f929
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 128 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/logger.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('unit:logger', () => {
expect(subject).to.have.property('format').satisfy(isObjectPlain)
expect(subject).to.have.property('level', logLevels.info)
expect(subject).to.have.property('levels', logLevels)
expect(subject).to.have.property('reporters').be.an('array').that.is.empty
expect(subject).to.have.property('reporters').be.instanceof(Set).and.empty
expect(subject).to.have.property('stderr', process.stderr)
expect(subject).to.have.property('stdout', process.stdout)
expect(subject).to.have.property('types').with.keys(types)
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/__tests__/log-function.spec-d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type TestSubject from '#interfaces/log-function'
import type { InputLogObject } from '@flex-development/log'

describe('unit-d:interfaces/LogFunction', () => {
it('should match [this: void]', () => {
expectTypeOf<TestSubject>().thisParameter.toEqualTypeOf<void>()
it('should match [this: unknown]', () => {
expectTypeOf<TestSubject>().thisParameter.toEqualTypeOf<unknown>()
})

describe('parameters', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/__tests__/log-functions.spec-d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('unit-d:interfaces/LogFunctions', () => {
describe('inspect', () => {
type Subject = TestSubject['inspect']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
it('should match [this: unknown]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<unknown>()
})

describe('parameters', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/__tests__/logger-options.spec-d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import type {
LogFormatOptions,
LogLevelOption,
LogType,
ReportersOption,
WriteStream
} from '@flex-development/log'
import type { Reporter } from '@flex-development/log/reporters'
import type { Nilable } from '@flex-development/tutils'

describe('unit-d:interfaces/LoggerOptions', () => {
Expand All @@ -33,10 +33,10 @@ describe('unit-d:interfaces/LoggerOptions', () => {
.toEqualTypeOf<Nilable<LogLevelOption>>()
})

it('should match [reporters?: Set<Reporter> | readonly Reporter[] | null | undefined]', () => {
it('should match [reporters?: ReportersOption | null | undefined]', () => {
expectTypeOf<TestSubject>()
.toHaveProperty('reporters')
.toEqualTypeOf<Nilable<Set<Reporter> | readonly Reporter[]>>()
.toEqualTypeOf<Nilable<ReportersOption>>()
})

it('should match [stderr?: WriteStream | null | undefined]', () => {
Expand Down
12 changes: 8 additions & 4 deletions src/interfaces/__tests__/logger.spec-d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ describe('unit-d:interfaces/Logger', () => {
expectTypeOf<TestSubject>().toHaveProperty('levels').toEqualTypeOf<T>()
})

it('should match [reporters: Reporter[]]', () => {
expectTypeOf<TestSubject>()
.toHaveProperty('reporters')
.toEqualTypeOf<Reporter[]>()
it('should match [readonly reporters: Set<Reporter>]', () => {
// Arrange
type K = ReadonlyKeys<TestSubject>
type T = Set<Reporter>

// Expect
expectTypeOf<K>().extract<'reporters'>().not.toBeNever()
expectTypeOf<TestSubject>().toHaveProperty('reporters').toEqualTypeOf<T>()
})

it('should match [stderr: WriteStream]', () => {
Expand Down
12 changes: 2 additions & 10 deletions src/interfaces/log-function.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,22 @@ interface LogFunction {
/**
* @see {@linkcode InputLogObject}
*
* @this {void}
*
* @param {InputLogObject | string} message
* The message to write
* @param {unknown[]} args
* Message arguments
* @return {undefined | void}
*/
(
this: void,
message: InputLogObject | string,
...args: unknown[]
): undefined | void
(message: InputLogObject | string, ...args: unknown[]): undefined | void

/**
* @this {void}
*
* @param {unknown} message
* The message to write
* @param {unknown[]} args
* Message arguments
* @return {undefined | void}
*/
(this: void, message: unknown, ...args: unknown[]): undefined | void
(message: unknown, ...args: unknown[]): undefined | void
}

export type { LogFunction as default }
3 changes: 0 additions & 3 deletions src/interfaces/log-functions.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ interface LogFunctions extends LogTypeFunctions {
*
* @see {@linkcode InspectOptions}
*
* @this {void}
*
* @param {unknown} value
* The thing to inspect
* @param {InspectOptions | null | undefined} [options]
* Inspect options
* @return {undefined}
*/
inspect(
this: void,
value: unknown,
options?: InspectOptions | null | undefined
): undefined
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/logger-options.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
LogFormatOptions,
LogLevelOption,
LogType,
ReportersOption,
WriteStream
} from '@flex-development/log'
import type { Reporter } from '@flex-development/log/reporters'

/**
* Logger configuration options.
Expand Down Expand Up @@ -50,13 +50,13 @@ interface LoggerOptions {
level?: LogLevelOption | null | undefined

/**
* List of reporter instances used to handle and output log messages.
* Reporter instances used to handle and output log messages.
*
* @see {@linkcode Reporter}
* @see {@linkcode ReportersOption}
*
* @default []
*/
reporters?: Set<Reporter> | readonly Reporter[] | null | undefined
reporters?: ReportersOption | null | undefined

/**
* The writeable stream for standard error output.
Expand Down
10 changes: 6 additions & 4 deletions src/interfaces/logger.mts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ interface Logger extends LogFunctions {
format: LogFormatOptions

/**
* Get the minimum log level to output.
* Get the current log level.
*
* @see {@linkcode LogLevel}
*
Expand All @@ -83,12 +83,12 @@ interface Logger extends LogFunctions {
get level(): LogLevel

/**
* Set the minimum log level to output.
* Set the maximum log level to output.
*
* @see {@linkcode LogLevelOption}
*
* @param {LogLevelOption | null | undefined} level
* New log level
* Maximum log level (inclusive)
*/
set level(level: LogLevelOption | null | undefined)

Expand All @@ -105,8 +105,10 @@ interface Logger extends LogFunctions {
* List of reporter instances used to handle and output log messages.
*
* @see {@linkcode Reporter}
*
* @readonly
*/
reporters: Reporter[]
readonly reporters: Set<Reporter>

/**
* The writeable stream for standard error output.
Expand Down
8 changes: 4 additions & 4 deletions src/internal/__tests__/normalize-level.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import testSubject from '#internal/normalize-level'
import type { Logger } from '@flex-development/log'

describe('unit:internal/normalizeLevel', () => {
let self: Pick<Logger, 'levels' | 'types'>
let logger: Pick<Logger, 'levels' | 'types'>

beforeAll(() => {
self = {
logger = {
levels: logLevels,
types: {
[logTypes.debug]: { level: logLevels.debug },
Expand All @@ -30,7 +30,7 @@ describe('unit:internal/normalizeLevel', () => {
}
})

it.each<Parameters<typeof testSubject>>([
it.each<[number | string | null | undefined]>([
[-13],
[logLevels.silent],
[logLevels.error],
Expand All @@ -54,6 +54,6 @@ describe('unit:internal/normalizeLevel', () => {
[logTypes.warn],
[null]
])('should return normalized log level (%j)', level => {
expect(testSubject.call(self, level)).toMatchSnapshot()
expect(testSubject(logger, level)).toMatchSnapshot()
})
})
10 changes: 5 additions & 5 deletions src/internal/normalize-level.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ import { clamp } from '@flex-development/tutils'
*
* @internal
*
* @this {Pick<Logger, 'levels' | 'types'>}
*
* @param {Pick<Logger, 'levels' | 'types'>} logger
* Logger object
* @param {number | string | null | undefined} level
* Log level or type
* @return {LogLevel}
* Normalized log level
*/
function normalizeLevel(
this: Pick<Logger, 'levels' | 'types'>,
logger: Pick<Logger, 'levels' | 'types'>,
level: number | string | null | undefined
): LogLevel {
/**
* Log level map.
*
* @const {LogLevelMap} levels
*/
const levels: LogLevelMap = this.levels
const levels: LogLevelMap = logger.levels

if (typeof level === 'number') {
return clamp(level, levels.silent, levels.verbose) as LogLevel
Expand All @@ -43,7 +43,7 @@ function normalizeLevel(
*
* @const {Record<LogType, InputLogObject>} types
*/
const types: Record<LogType, InputLogObject> = this.types
const types: Record<LogType, InputLogObject> = logger.types

// `level` is a known log type
if (level in types) return types[level as LogType].level ?? levels.info
Expand Down
2 changes: 1 addition & 1 deletion src/lib/__tests__/create-logger.functional.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('functional:lib/createLogger', () => {

beforeEach(() => {
reporter = new MockReporter()
subject = testSubject({ reporters: [reporter] })
subject = testSubject({ reporters: reporter })
subject.unicode = true
})

Expand Down
Loading

0 comments on commit 0f3f929

Please sign in to comment.