Skip to content

Commit

Permalink
fix(types): child is not respect customLevels (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 authored Jan 25, 2022
1 parent 6299e60 commit a59f8bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import type { EventEmitter } from "events";
// @ts-ignore -- gracefully falls back to `any` if not installed
import type { PrettyOptions as PinoPrettyOptions } from "pino-pretty";
import * as pinoStdSerializers from "pino-std-serializers";
import type { SonicBoom, SonicBoomOpts } from "sonic-boom";
import type { WorkerOptions } from "worker_threads";

import * as pinoStdSerializers from "pino-std-serializers";


//// Non-exported types and interfaces
Expand All @@ -33,15 +33,15 @@ type ThreadStream = any
type TimeFn = () => string;
type MixinFn = () => object;

type CustomLevelLogger<Options> = Options extends LoggerOptions ? Record<keyof Options["customLevels"], LogFn> : never
type CustomLevelLogger<Options> = Options extends { customLevels: Record<string, number> } ? Record<keyof Options["customLevels"], LogFn> : Record<never, LogFn>

interface redactOptions {
paths: string[];
censor?: string | ((v: any) => any);
remove?: boolean;
}

interface LoggerExtras extends EventEmitter {
interface LoggerExtras<Options = LoggerOptions> extends EventEmitter {
/**
* Exposes the Pino package version. Also available on the exported pino function.
*/
Expand Down Expand Up @@ -76,7 +76,7 @@ interface LoggerExtras extends EventEmitter {
* @param options: an options object that will override child logger inherited options.
* @returns a child logger instance.
*/
child(bindings: pino.Bindings, options?: pino.ChildLoggerOptions): pino.Logger;
child<ChildOptions extends pino.ChildLoggerOptions>(bindings: pino.Bindings, options?: ChildOptions): pino.Logger<Options & ChildOptions>;

/**
* Registers a listener function that is triggered when the level is changed.
Expand Down Expand Up @@ -209,7 +209,7 @@ declare namespace pino {

type LogDescriptor = Record<string, any>;

type Logger<Options = LoggerOptions> = BaseLogger & LoggerExtras & CustomLevelLogger<Options>;
type Logger<Options = LoggerOptions> = BaseLogger & LoggerExtras<Options> & CustomLevelLogger<Options>;

type SerializedError = pinoStdSerializers.SerializedError;
type SerializedResponse = pinoStdSerializers.SerializedResponse;
Expand Down Expand Up @@ -798,7 +798,6 @@ export interface TransportTargetOptions extends pino.TransportTargetOptions {}
// as default (`import pino from "pino"`) and named variable
// (`import {pino} from "pino"`).
export { pino as default, pino };

// Export just the type side of the namespace as "P", allows
// `import {P} from "pino"; const log: P.Logger;`.
// (Legacy support for early 7.x releases, remove in 8.x.)
Expand Down
14 changes: 14 additions & 0 deletions test/types/pino.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,17 @@ const log3 = pino({ customLevels: { myLevel: 100 } })
expectError(log3.log())
log3.level = 'myLevel'
log3.myLevel('')
log3.child({}).myLevel('')

const clog3 = log3.child({}, { customLevels: { childLevel: 120 } })
// child inherit parant
clog3.myLevel('')
// child itself
clog3.childLevel('')
const cclog3 = clog3.child({}, { customLevels: { childLevel2: 130 } })
// child inherit root
cclog3.myLevel('')
// child inherit parant
cclog3.childLevel('')
// child itself
cclog3.childLevel2('')

0 comments on commit a59f8bc

Please sign in to comment.