/
logger.test-d.ts
193 lines (161 loc) · 5.11 KB
/
logger.test-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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { expectDeprecated, expectError, expectType } from 'tsd'
import fastify, {
FastifyLogFn,
LogLevel,
FastifyLoggerInstance,
FastifyRequest,
FastifyReply,
FastifyBaseLogger
} from '../../fastify'
import { Server, IncomingMessage, ServerResponse } from 'http'
import * as fs from 'fs'
import P from 'pino'
expectType<FastifyLoggerInstance>(fastify().log)
class Foo {}
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(logLevel => {
expectType<FastifyLogFn>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel])
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](''))
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({}))
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({ foo: 'bar' }))
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Error()))
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Foo()))
})
interface CustomLogger extends FastifyBaseLogger {
customMethod(msg: string, ...args: unknown[]): void;
}
class CustomLoggerImpl implements CustomLogger {
level = 'info'
customMethod (msg: string, ...args: unknown[]) { console.log(msg, args) }
// Implementation signature must be compatible with all overloads of FastifyLogFn
info (arg1: unknown, arg2?: unknown, ...args: unknown[]): void {
console.log(arg1, arg2, ...args)
}
warn (...args: unknown[]) { console.log(args) }
error (...args: unknown[]) { console.log(args) }
fatal (...args: unknown[]) { console.log(args) }
trace (...args: unknown[]) { console.log(args) }
debug (...args: unknown[]) { console.log(args) }
silent (...args: unknown[]) { }
child (bindings: P.Bindings, options?: P.ChildLoggerOptions): CustomLoggerImpl { return new CustomLoggerImpl() }
}
const customLogger = new CustomLoggerImpl()
const serverWithCustomLogger = fastify<
Server,
IncomingMessage,
ServerResponse,
CustomLoggerImpl
>({ logger: customLogger })
expectType<CustomLoggerImpl>(serverWithCustomLogger.log)
const serverWithPino = fastify<
Server,
IncomingMessage,
ServerResponse,
P.Logger
>({
logger: P({
level: 'info',
redact: ['x-userinfo']
})
})
expectType<P.Logger>(serverWithPino.log)
const serverWithLogOptions = fastify<
Server,
IncomingMessage,
ServerResponse
>({
logger: {
level: 'info'
}
})
expectType<FastifyLoggerInstance>(serverWithLogOptions.log)
const serverWithFileOption = fastify<
Server,
IncomingMessage,
ServerResponse
>({
logger: {
level: 'info',
file: '/path/to/file'
}
})
expectType<FastifyLoggerInstance>(serverWithFileOption.log)
const serverAutoInferringTypes = fastify({
logger: {
level: 'info'
}
})
expectType<FastifyBaseLogger>(serverAutoInferringTypes.log)
const serverWithAutoInferredPino = fastify({
logger: P({
level: 'info',
redact: ['x-userinfo']
})
})
expectType<P.Logger>(serverWithAutoInferredPino.log)
const serverAutoInferredFileOption = fastify({
logger: {
level: 'info',
file: '/path/to/file'
}
})
expectType<FastifyBaseLogger>(serverAutoInferredFileOption.log)
const serverAutoInferredSerializerObjectOption = fastify({
logger: {
serializers: {
req (IncomingMessage) {
expectType<FastifyRequest>(IncomingMessage)
return {
method: 'method',
url: 'url',
version: 'version',
hostname: 'hostname',
remoteAddress: 'remoteAddress',
remotePort: 80,
other: ''
}
},
res (ServerResponse) {
expectType<FastifyReply>(ServerResponse)
return {
statusCode: 'statusCode'
}
},
err (FastifyError) {
return {
other: '',
type: 'type',
message: 'msg',
stack: 'stack'
}
}
}
}
})
expectType<FastifyBaseLogger>(serverAutoInferredSerializerObjectOption.log)
const passStreamAsOption = fastify({
logger: {
stream: fs.createWriteStream('/tmp/stream.out')
}
})
expectType<FastifyBaseLogger>(passStreamAsOption.log)
const passPinoOption = fastify({
logger: {
redact: ['custom'],
messageKey: 'msg',
nestedKey: 'nested',
enabled: true
}
})
expectType<FastifyBaseLogger>(passPinoOption.log)
// FastifyLoggerInstance is deprecated
expectDeprecated({} as FastifyLoggerInstance)
const childParent = fastify().log
// we test different option variant here
expectType<FastifyLoggerInstance>(childParent.child({}, { level: 'info' }))
expectType<FastifyLoggerInstance>(childParent.child({}, { redact: ['pass', 'pin'] }))
expectType<FastifyLoggerInstance>(childParent.child({}, { serializers: { key: () => {} } }))
expectType<FastifyLoggerInstance>(childParent.child({}, { level: 'info', redact: ['pass', 'pin'], serializers: { key: () => {} } }))
// no option pass
expectError(childParent.child())
// wrong option
expectError(childParent.child({}, { nonExist: true }))