UNPKG

4.01 kBTypeScriptView Raw
1import { FastifyError } from '@fastify/error'
2import { RouteGenericInterface } from './route'
3import { FastifyRequest } from './request'
4import { FastifyReply } from './reply'
5import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
6import { FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
7import { FastifySchema } from './schema'
8import { FastifyInstance } from './instance'
9
10import pino from 'pino'
11
12/**
13 * Standard Fastify logging function
14 */
15export type FastifyLogFn = pino.LogFn
16
17export type LogLevel = pino.LevelWithSilent
18
19export type Bindings = pino.Bindings
20
21export type ChildLoggerOptions = pino.ChildLoggerOptions
22
23export interface FastifyBaseLogger extends pino.BaseLogger {
24 child(bindings: Bindings, options?: ChildLoggerOptions): FastifyBaseLogger
25}
26
27// TODO delete FastifyBaseLogger in the next major release. It seems that it is enough to have only FastifyBaseLogger.
28/**
29 * @deprecated Use FastifyBaseLogger instead
30 */
31export type FastifyLoggerInstance = FastifyBaseLogger
32
33export interface FastifyLoggerStreamDestination {
34 write(msg: string): void;
35}
36
37export type PinoLoggerOptions = pino.LoggerOptions
38
39// TODO: once node 18 is EOL, this type can be replaced with plain FastifyReply.
40/**
41 * Specialized reply type used for the `res` log serializer, since only `statusCode` is passed in certain cases.
42 */
43export type ResSerializerReply<
44 RawServer extends RawServerBase,
45 RawReply extends FastifyReply<RawServer>
46> = Partial<RawReply> & Pick<RawReply, 'statusCode'>;
47
48/**
49 * Fastify Custom Logger options.
50 */
51export interface FastifyLoggerOptions<
52 RawServer extends RawServerBase = RawServerDefault,
53 RawRequest extends FastifyRequest<RouteGenericInterface, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, FastifyTypeProvider> = FastifyRequest<RouteGenericInterface, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, FastifyTypeProviderDefault>,
54 RawReply extends FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RouteGenericInterface, ContextConfigDefault, FastifySchema, FastifyTypeProvider> = FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RouteGenericInterface, ContextConfigDefault, FastifySchema, FastifyTypeProviderDefault>,
55> {
56 serializers?: {
57 req?: (req: RawRequest) => {
58 method?: string;
59 url?: string;
60 version?: string;
61 hostname?: string;
62 remoteAddress?: string;
63 remotePort?: number;
64 [key: string]: unknown;
65 };
66 err?: (err: FastifyError) => {
67 type: string;
68 message: string;
69 stack: string;
70 [key: string]: unknown;
71 };
72 res?: (res: ResSerializerReply<RawServer, RawReply>) => {
73 statusCode?: string | number;
74 [key: string]: unknown;
75 };
76 };
77 level?: string;
78 file?: string;
79 genReqId?: (req: RawRequest) => string;
80 stream?: FastifyLoggerStreamDestination;
81}
82
83export interface FastifyChildLoggerFactory<
84 RawServer extends RawServerBase = RawServerDefault,
85 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
86 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
87 Logger extends FastifyBaseLogger = FastifyBaseLogger,
88 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
89> {
90 /**
91 * @param logger The parent logger
92 * @param bindings The bindings object that will be passed to the child logger
93 * @param childLoggerOpts The logger options that will be passed to the child logger
94 * @param rawReq The raw request
95 * @this The fastify instance
96 * @returns The child logger instance
97 */
98 (
99 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
100 logger: Logger,
101 bindings: Bindings,
102 childLoggerOpts: ChildLoggerOptions,
103 rawReq: RawRequest
104 ): Logger
105}