UNPKG

5.67 kBTypeScriptView Raw
1/*
2 * Rationale for not directly importing types from @types/pino for use in fastify interfaces:
3 * - pino does not itself provide types so the types from @types must be used.
4 * - the types from @types are unofficial and the preference is to avoid using them or requiring them as a dependency of fastify.
5 * - the goal is to provide the minimum viable type definitions necessary to use fastify's official logger, pino.
6 * - the types provided should cover the basic use cases for the majority of fastify users while also being easy to maintain.
7 * - for advanced use cases needing the full set of types, users should be directed to manually install the unofficial types with
8 * `npm i -D @types/pino` and to supply their own logger instance as described at https://www.fastify.io/docs/latest/Logging/.
9 * - some fastify contributors have volunteered to maintain official types within pino (https://github.com/pinojs/pino/issues/910)
10 * in which case if the proposal is followed through with then in the future fastify will be able to directly import the full
11 * set of types rather than only duplicating and maintaining the subset chosen for providing a minimum viable logger api.
12 *
13 * Relevant discussions:
14 *
15 * https://github.com/fastify/fastify/pull/2550
16 * https://github.com/pinojs/pino/issues/910
17 * https://github.com/fastify/fastify/pull/1532
18 * https://github.com/fastify/fastify/issues/649
19 */
20
21import { FastifyError } from 'fastify-error'
22import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression } from './utils'
23import { FastifyRequest, RequestGenericInterface } from './request'
24
25/**
26 * Standard Fastify logging function
27 */
28export interface FastifyLogFn {
29 (msg: string, ...args: unknown[]): void;
30 (obj: object, msg?: string, ...args: unknown[]): void;
31}
32
33export type LogLevel = 'info' | 'error' | 'debug' | 'fatal' | 'warn' | 'trace'
34
35export type SerializerFn = (value: unknown) => unknown;
36
37export interface Bindings {
38 level?: LogLevel | string;
39 serializers?: { [key: string]: SerializerFn };
40 [key: string]: unknown;
41}
42
43export interface FastifyLoggerInstance {
44 info: FastifyLogFn;
45 warn: FastifyLogFn;
46 error: FastifyLogFn;
47 fatal: FastifyLogFn;
48 trace: FastifyLogFn;
49 debug: FastifyLogFn;
50 child(bindings: Bindings): FastifyLoggerInstance;
51}
52
53// This interface is accurate for pino 6.3 and was copied from the following permalink:
54// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/72c9bd83316bd31e93ab86d64ddf598d922f33cd/types/pino/index.d.ts#L514-L567
55export interface PrettyOptions {
56 /**
57 * Translate the epoch time value into a human readable date and time string.
58 * This flag also can set the format string to apply when translating the date to human readable format.
59 * The default format is yyyy-mm-dd HH:MM:ss.l o in UTC.
60 * For a list of available pattern letters see the {@link https://www.npmjs.com/package/dateformat|dateformat documentation}.
61 */
62 translateTime?: boolean | string;
63 /**
64 * If set to true, it will print the name of the log level as the first field in the log line. Default: `false`.
65 */
66 levelFirst?: boolean;
67 /**
68 * The key in the JSON object to use as the highlighted message. Default: "msg".
69 */
70 messageKey?: string;
71 /**
72 * The key in the JSON object to use for timestamp display. Default: "time".
73 */
74 timestampKey?: string;
75 /**
76 * Format output of message, e.g. {level} - {pid} will output message: INFO - 1123 Default: `false`.
77 */
78 messageFormat?: false | string;
79 /**
80 * If set to true, will add color information to the formatted output message. Default: `false`.
81 */
82 colorize?: boolean;
83 /**
84 * Appends carriage return and line feed, instead of just a line feed, to the formatted log line.
85 */
86 crlf?: boolean;
87 /**
88 * Define the log keys that are associated with error like objects. Default: ["err", "error"]
89 */
90 errorLikeObjectKeys?: string[];
91 /**
92 * When formatting an error object, display this list of properties.
93 * The list should be a comma separated list of properties. Default: ''
94 */
95 errorProps?: string;
96 /**
97 * Specify a search pattern according to {@link http://jmespath.org|jmespath}
98 */
99 search?: string;
100 /**
101 * Ignore one or several keys. Example: "time,hostname"
102 */
103 ignore?: string;
104 /**
105 * Suppress warning on first synchronous flushing.
106 */
107 suppressFlushSyncWarning?: boolean;
108}
109
110/**
111 * Fastify Custom Logger options. To enable configuration of all Pino options,
112 * refer to this example:
113 * https://github.com/fastify/fastify/blob/2f56e10a24ecb70c2c7950bfffd60eda8f7782a6/docs/TypeScript.md#example-5-specifying-logger-types
114 */
115export interface FastifyLoggerOptions<
116 RawServer extends RawServerBase = RawServerDefault,
117 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
118 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>
119> {
120 serializers?: {
121 req?: (req: RawRequest) => {
122 method?: string;
123 url?: string;
124 version?: string;
125 hostname?: string;
126 remoteAddress?: string;
127 remotePort?: number;
128 [key: string]: unknown;
129 };
130 err?: (err: FastifyError) => {
131 type: string;
132 message: string;
133 stack: string;
134 [key: string]: unknown;
135 };
136 res?: (res: RawReply) => {
137 statusCode: string | number;
138 [key: string]: unknown;
139 };
140 };
141 level?: string;
142 genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface>(req: FastifyRequest<RequestGeneric, RawServer, RawRequest>) => string;
143 prettyPrint?: boolean | PrettyOptions;
144}