Skip to content

Commit

Permalink
chore: fix pino breaking types
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 18, 2023
1 parent 839e82d commit 01957e5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ import type { LoggerConfig, LevelMapping, Bindings, ChildLoggerOptions } from '.
* ```
*/
export class Logger<Config extends LoggerConfig = LoggerConfig> {
pino: PinoLogger<Config>
pino: PinoLogger<keyof Config['customLevels'] & string>

constructor(
protected config: Config,
pino?: PinoLogger<Config>
pino?: PinoLogger<keyof Config['customLevels'] & string>
) {
if (!this.config.enabled) {
this.pino = abstractLogging
Expand Down
11 changes: 7 additions & 4 deletions src/logger_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class LoggerManager<
/**
* Created loggers. Logger instances are cached forever
*/
#loggers: Map<keyof KnownLoggers, Logger<LoggerConfig>> = new Map()
#loggers: Map<keyof KnownLoggers, Logger<KnownLoggers[keyof KnownLoggers]>> = new Map()

constructor(config: LoggerManagerConfig<KnownLoggers>) {
super(config.loggers[config.default])
Expand Down Expand Up @@ -58,7 +58,7 @@ export class LoggerManager<

if (this.#loggers.has(loggerToUse)) {
debug('using logger from cache. name: "%s"', logger)
return this.#loggers.get(loggerToUse)! as Logger<KnownLoggers[K]>
return this.#loggers.get(loggerToUse)! as Logger<KnownLoggers[K]> | Logger<LoggerConfig>
}

const config = this.#config.loggers[loggerToUse]
Expand All @@ -67,14 +67,17 @@ export class LoggerManager<
const loggerInstance = this.createLogger(loggerToUse, config)
this.#loggers.set(loggerToUse, loggerInstance)

return loggerInstance
return loggerInstance as Logger<KnownLoggers[K]> | Logger<LoggerConfig>
}

/**
* Create a logger instance from the config. The created instance
* is not managed by the manager
*/
create(config: LoggerConfig, pino?: PinoLogger) {
create<Config extends LoggerConfig>(
config: Config,
pino?: PinoLogger<keyof Config['customLevels'] & string>
) {
return new Logger(config, pino)
}
}
8 changes: 5 additions & 3 deletions src/pino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ const TimestampFormatters: { [Keyword in TimestampKeywords]: () => string } = {
/**
* Returns an instance of pino logger by adjusting the config options
*/
export function createPino<Config extends LoggerConfig>(options: Config): PinoLogger<Config> {
export function createPino<Config extends LoggerConfig>(
options: Config
): PinoLogger<keyof Config['customLevels'] & string> {
const { desination, timestamp, ...rest } = options
const pinoOptions: LoggerOptions = Object.assign({}, rest)
const pinoOptions: LoggerOptions<any> = Object.assign({}, rest)

/**
* Use pino formatters when a keyword is used
Expand All @@ -43,7 +45,7 @@ export function createPino<Config extends LoggerConfig>(options: Config): PinoLo
pinoOptions.timestamp = TimestampFormatters[timestamp]
}

return (desination ? pino(pinoOptions, desination) : pino(pinoOptions)) as PinoLogger<Config>
return desination ? pino(pinoOptions, desination) : pino(pinoOptions)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type { TransportTargetOptions, Level, LevelMapping, ChildLoggerOptions, B
/**
* Logger config inherited from pino logger options
*/
export type LoggerConfig = Omit<LoggerOptions, 'browser' | 'timestamp'> & {
export type LoggerConfig = Omit<LoggerOptions<any>, 'browser' | 'timestamp'> & {
enabled?: boolean
desination?: DestinationStream
timestamp?: TimestampKeywords | boolean | (() => string)
Expand Down
3 changes: 2 additions & 1 deletion tests/logger_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { test } from '@japa/runner'

import { Logger } from '../index.js'
import { LoggerConfig } from '../src/types.js'
import { getFakeStream } from '../factories/logger.js'
import { defineConfig } from '../src/define_config.js'
import { LoggerManager } from '../src/logger_manager.js'
import { getFakeStream } from '../factories/logger.js'

test.group('Logger manager', () => {
test('create logger instances only once', ({ assert, expectTypeOf }) => {
Expand All @@ -30,6 +30,7 @@ test.group('Logger manager', () => {
})

const manager = new LoggerManager(config)

expectTypeOf(manager.use('app')).toEqualTypeOf<Logger<{ level: 'trace' }>>()
expectTypeOf(manager.use('main')).toEqualTypeOf<Logger<{ level: 'info' }>>()
expectTypeOf(manager.use()).toEqualTypeOf<Logger<LoggerConfig>>()
Expand Down

0 comments on commit 01957e5

Please sign in to comment.