Skip to content

Commit ec9b0b5

Browse files
authoredDec 1, 2021
fix: type regression on fastify (#1246)
1 parent 23a8edc commit ec9b0b5

File tree

2 files changed

+114
-88
lines changed

2 files changed

+114
-88
lines changed
 

‎pino.d.ts

+95-88
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import type { EventEmitter } from "events";
2020
// @ts-ignore -- gracefully falls back to `any` if not installed
21-
import type { PrettyOptions } from "pino-pretty";
21+
import type { PrettyOptions as PinoPrettyOptions } from "pino-pretty";
2222
import type { SonicBoom, SonicBoomOpts } from "sonic-boom";
2323
import type { WorkerOptions } from "worker_threads";
2424

@@ -27,9 +27,6 @@ import * as pinoStdSerializers from "pino-std-serializers";
2727

2828
//// Non-exported types and interfaces
2929

30-
type SerializerFn = (value: any) => any;
31-
type WriteFn = (o: object) => void;
32-
3330
// ToDo https://github.com/pinojs/thread-stream/issues/24
3431
type ThreadStream = any
3532

@@ -42,90 +39,6 @@ interface redactOptions {
4239
remove?: boolean;
4340
}
4441

45-
interface BaseLogger {
46-
/**
47-
* Set this property to the desired logging level. In order of priority, available levels are:
48-
*
49-
* - 'fatal'
50-
* - 'error'
51-
* - 'warn'
52-
* - 'info'
53-
* - 'debug'
54-
* - 'trace'
55-
*
56-
* The logging level is a __minimum__ level. For instance if `logger.level` is `'info'` then all `'fatal'`, `'error'`, `'warn'`,
57-
* and `'info'` logs will be enabled.
58-
*
59-
* You can pass `'silent'` to disable logging.
60-
*/
61-
level: pino.LevelWithSilent | string;
62-
63-
/**
64-
* Log at `'fatal'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
65-
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
66-
*
67-
* @typeParam T: the interface of the object being serialized. Default is object.
68-
* @param obj: object to be serialized
69-
* @param msg: the log message to write
70-
* @param ...args: format string values when `msg` is a format string
71-
*/
72-
fatal: pino.LogFn;
73-
/**
74-
* Log at `'error'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
75-
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
76-
*
77-
* @typeParam T: the interface of the object being serialized. Default is object.
78-
* @param obj: object to be serialized
79-
* @param msg: the log message to write
80-
* @param ...args: format string values when `msg` is a format string
81-
*/
82-
error: pino.LogFn;
83-
/**
84-
* Log at `'warn'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
85-
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
86-
*
87-
* @typeParam T: the interface of the object being serialized. Default is object.
88-
* @param obj: object to be serialized
89-
* @param msg: the log message to write
90-
* @param ...args: format string values when `msg` is a format string
91-
*/
92-
warn: pino.LogFn;
93-
/**
94-
* Log at `'info'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
95-
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
96-
*
97-
* @typeParam T: the interface of the object being serialized. Default is object.
98-
* @param obj: object to be serialized
99-
* @param msg: the log message to write
100-
* @param ...args: format string values when `msg` is a format string
101-
*/
102-
info: pino.LogFn;
103-
/**
104-
* Log at `'debug'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
105-
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
106-
*
107-
* @typeParam T: the interface of the object being serialized. Default is object.
108-
* @param obj: object to be serialized
109-
* @param msg: the log message to write
110-
* @param ...args: format string values when `msg` is a format string
111-
*/
112-
debug: pino.LogFn;
113-
/**
114-
* Log at `'trace'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
115-
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
116-
*
117-
* @typeParam T: the interface of the object being serialized. Default is object.
118-
* @param obj: object to be serialized
119-
* @param msg: the log message to write
120-
* @param ...args: format string values when `msg` is a format string
121-
*/
122-
trace: pino.LogFn;
123-
/**
124-
* Noop function.
125-
*/
126-
silent: pino.LogFn;
127-
}
128-
12942
interface LoggerExtras extends EventEmitter {
13043
/**
13144
* Exposes the Pino package version. Also available on the exported pino function.
@@ -192,10 +105,98 @@ interface LoggerExtras extends EventEmitter {
192105

193106
declare namespace pino {
194107
//// Exported types and interfaces
108+
109+
interface BaseLogger {
110+
/**
111+
* Set this property to the desired logging level. In order of priority, available levels are:
112+
*
113+
* - 'fatal'
114+
* - 'error'
115+
* - 'warn'
116+
* - 'info'
117+
* - 'debug'
118+
* - 'trace'
119+
*
120+
* The logging level is a __minimum__ level. For instance if `logger.level` is `'info'` then all `'fatal'`, `'error'`, `'warn'`,
121+
* and `'info'` logs will be enabled.
122+
*
123+
* You can pass `'silent'` to disable logging.
124+
*/
125+
level: pino.LevelWithSilent | string;
126+
127+
/**
128+
* Log at `'fatal'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
129+
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
130+
*
131+
* @typeParam T: the interface of the object being serialized. Default is object.
132+
* @param obj: object to be serialized
133+
* @param msg: the log message to write
134+
* @param ...args: format string values when `msg` is a format string
135+
*/
136+
fatal: pino.LogFn;
137+
/**
138+
* Log at `'error'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
139+
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
140+
*
141+
* @typeParam T: the interface of the object being serialized. Default is object.
142+
* @param obj: object to be serialized
143+
* @param msg: the log message to write
144+
* @param ...args: format string values when `msg` is a format string
145+
*/
146+
error: pino.LogFn;
147+
/**
148+
* Log at `'warn'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
149+
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
150+
*
151+
* @typeParam T: the interface of the object being serialized. Default is object.
152+
* @param obj: object to be serialized
153+
* @param msg: the log message to write
154+
* @param ...args: format string values when `msg` is a format string
155+
*/
156+
warn: pino.LogFn;
157+
/**
158+
* Log at `'info'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
159+
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
160+
*
161+
* @typeParam T: the interface of the object being serialized. Default is object.
162+
* @param obj: object to be serialized
163+
* @param msg: the log message to write
164+
* @param ...args: format string values when `msg` is a format string
165+
*/
166+
info: pino.LogFn;
167+
/**
168+
* Log at `'debug'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
169+
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
170+
*
171+
* @typeParam T: the interface of the object being serialized. Default is object.
172+
* @param obj: object to be serialized
173+
* @param msg: the log message to write
174+
* @param ...args: format string values when `msg` is a format string
175+
*/
176+
debug: pino.LogFn;
177+
/**
178+
* Log at `'trace'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
179+
* If more args follows `msg`, these will be used to format `msg` using `util.format`.
180+
*
181+
* @typeParam T: the interface of the object being serialized. Default is object.
182+
* @param obj: object to be serialized
183+
* @param msg: the log message to write
184+
* @param ...args: format string values when `msg` is a format string
185+
*/
186+
trace: pino.LogFn;
187+
/**
188+
* Noop function.
189+
*/
190+
silent: pino.LogFn;
191+
}
192+
195193
type Bindings = Record<string, any>;
196194

197195
type Level = "fatal" | "error" | "warn" | "info" | "debug" | "trace";
198196
type LevelWithSilent = pino.Level | "silent";
197+
198+
type SerializerFn = (value: any) => any;
199+
type WriteFn = (o: object) => void;
199200

200201
type LevelChangeEventListener = (
201202
lvl: LevelWithSilent | string,
@@ -278,6 +279,8 @@ declare namespace pino {
278279
(msg: string, ...args: any[]): void;
279280
}
280281

282+
interface PrettyOptions extends PinoPrettyOptions {}
283+
281284
interface LoggerOptions {
282285
transport?: TransportSingleOptions | TransportMultiOptions | TransportPipelineOptions
283286
/**
@@ -756,10 +759,13 @@ export type LevelChangeEventListener = pino.LevelChangeEventListener;
756759
export type LogDescriptor = pino.LogDescriptor;
757760
export type Logger = pino.Logger;
758761
export type SerializedError = pino.SerializedError;
762+
export type SerializerFn = pino.SerializerFn;
759763
export type SerializedRequest = pino.SerializedRequest;
760764
export type SerializedResponse = pino.SerializedResponse;
765+
export type WriteFn = pino.WriteFn;
761766

762767
// Interfaces
768+
export interface BaseLogger extends pino.BaseLogger {}
763769
export interface ChildLoggerOptions extends pino.ChildLoggerOptions {}
764770
export interface DestinationStream extends pino.DestinationStream {}
765771
export interface LevelMapping extends pino.LevelMapping {}
@@ -768,6 +774,7 @@ export interface LogFn extends pino.LogFn {}
768774
export interface LoggerOptions extends pino.LoggerOptions {}
769775
export interface MultiStreamOptions extends pino.MultiStreamOptions {}
770776
export interface MultiStreamRes extends pino.MultiStreamRes {}
777+
export interface PrettyOptions extends pino.PrettyOptions {}
771778
export interface StreamEntry extends pino.StreamEntry {}
772779
export interface TransportBaseOptions extends pino.TransportBaseOptions {}
773780
export interface TransportMultiOptions extends pino.TransportMultiOptions {}

‎test/types/pino.test-d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,22 @@ const logLine: pino.LogDescriptor = {
277277
interface CustomLogger extends pino.Logger {
278278
customMethod(msg: string, ...args: unknown[]): void;
279279
}
280+
281+
const serializerFunc: pino.SerializerFn = () => {}
282+
const writeFunc: pino.WriteFn = () => {}
283+
284+
interface CustomBaseLogger extends pino.BaseLogger {
285+
child(): CustomBaseLogger
286+
}
287+
288+
const customBaseLogger: CustomBaseLogger = {
289+
level: 'info',
290+
fatal() {},
291+
error() {},
292+
warn() {},
293+
info() {},
294+
debug() {},
295+
trace() {},
296+
silent() {},
297+
child() { return this }
298+
}

0 commit comments

Comments
 (0)
Please sign in to comment.