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