-
Notifications
You must be signed in to change notification settings - Fork 0
/
log2stream.d.ts
436 lines (394 loc) · 10.1 KB
/
log2stream.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/// <reference types="node" />
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
import * as stream from 'stream';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Represents a severity level.
*/
export class Level
{
/**
* Creates a new level.
*
* @param name The name of the level.
* @param severity The severity of the level, this will be used to compare the level with other levels.
*/
constructor(name : string, severity : number);
/**
* The name of this level.
*/
readonly name : string;
/**
* Determines whether this level is of equal severity to another level.
*
* Example usage:
*
* ``` js
* Level.Info.isEqualTo(Level.Info);
* // => true
*
* Level.Info.isEqualTo(Level.Warn);
* // => false
* ```
*
* @param level The level to compare to this level.
*
* @returns `true` if this level is of equal severity to `level`, otherwise `false`.
*/
isEqualTo(level : Level) : boolean;
/**
* Determines whether this level is less severe than another level.
*
* Example usage:
*
* ``` js
* Level.Warn.isLessThan(Level.Error);
* // => true
*
* Level.Warn.isLessThan(Level.Debug);
* // => false
* ```
*
* @param level The level to compare to this level.
*
* @returns `true` if this level is less severe than `level`, otherwise `false`.
*/
isLessThan(level : Level) : boolean;
/**
* Determines whether this level is less severe than or equally severe to another level.
*
* Example usage:
*
* ``` js
* Level.Warn.isLessThanOrEqualTo(Level.Warn);
* // => true
*
* Level.Warn.isLessThanOrEqualTo(Level.Error);
* // => true
*
* Level.Warn.isLessThanOrEqualTo(Level.Debug);
* // => false
* ```
*
* @param level The level to compare to this level.
*
* @returns `true` if this level is less severe than or equally severe to `level`, otherwise `false`.
*/
isLessThanOrEqualTo(level : Level) : boolean;
/**
* Determines whether this level is more severe than another level.
*
* Example usage:
*
* ``` js
* Level.Warn.isGreaterThan(Level.Info);
* // => true
*
* Level.Warn.isGreaterThan(Level.Fatal);
* // => false
* ```
*
* @param level The level to compare to this level.
*
* @returns `true` if this level is more severe than `level`, otherwise `false`.
*/
isGreaterThan(level : Level) : boolean;
/**
* Determines whether this level is more severe than or equally as severe to another level.
*
* Example usage:
*
* ``` js
* Level.Warn.isGreaterThanOrEqualTo(Level.Warn);
* // => true
*
* Level.Warn.isGreaterThanOrEqualTo(Level.Debug);
* // => true
*
* Level.Warn.isGreaterThanOrEqualTo(Level.Error);
* // => false
* ```
*
* @param level The level to compare to this level.
*
* @returns `true` if this level is more severe than or equally severe to `level`, otherwise `false`.
*/
isGreaterThanOrEqualTo(level : Level) : boolean;
/**
* Converts a string into a predefined level. If a level could not be determined, `null` will be returned instead.
*
* Example usage:
*
* ``` js
* Level.toLevel('Error');
* // => Level.Error
*
* Level.toLevel('Info');
* // => Level.Info
*
* Level.toLevel('Trace');
* // => null
* ```
*
* @param string The string to convert into a level.
*
* @returns The predefined level corresponding to `string`, or `null` if a level could not be determined.
*/
static toLevel(string : string) : Level | null;
/**
* A predefined level that represents details useful for debugging an application.
*/
static readonly Debug : Level;
/**
* A predefined level that represents progress of an application.
*/
static readonly Info : Level;
/**
* A predefined level that represents potentially harmful situations.
*/
static readonly Warn : Level;
/**
* A predefined level that represents errors that wouldn't normally stop the application from running.
*/
static readonly Error : Level;
/**
* A predefined level that represents errors that wouldn't normally stop the application from running.
*/
static readonly Fatal : Level;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Represents a log record produced by a logger.
*/
export class Record
{
/**
* Creates a new log record.
*
* @param level The severity level of the record.
* @param category The category of the record.
* @param message The message describing the record.
* @param metadata The metadata associated with the record.
*/
constructor(level : Level, category : string, message : string, metadata? : any);
/**
* The severity level of this log record.
*/
readonly level : Level;
/**
* The category of this log record.
*
* This will likely be the name of the logger that created this log record.
*/
readonly category : string;
/**
* The message describing this log record.
*/
readonly message : string;
/**
* The date and time of when this log record was created.
*/
readonly date : Date;
/**
* The metadata associated with this log record.
*/
readonly metadata : any;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Represents a logger.
*/
export class Logger
{
/**
* Creates a new logger.
*
* @param name The name of the logger.
*/
constructor(name : string);
/**
* The name of this logger.
*/
readonly name : string;
/**
* The stream that this logger will write log records to.
*/
readonly stream : stream.Readable;
/**
* Writes a log record that is useful for debugging an application.
*
* Example usage:
*
* ``` js
* logger.debug('Something happened.', {
* detail : 'Detail on what happened...'
* });
* ```
*
* @param message The message to describe the log record.
* @param metadata The metadata to associate with the log record.
*/
debug(message : string, metadata? : any) : void;
/**
* Writes a log record that highlights the progress of an application.
*
* Example usage:
*
* ``` js
* logger.info('Something happened.', {
* detail : 'Detail on what happened...'
* });
* ```
*
* @param message The message to describe the log record.
* @param metadata The metadata to associate with the log record.
*/
info(message : string, metadata? : any) : void;
/**
* Writes a log record that highlights potentially harmful situations.
*
* Example usage:
*
* ``` js
* logger.warn('Something potentially alarming happened.', {
* detail : 'Detail on what happened...'
* });
* ```
*
* @param message The message to describe the log record.
* @param metadata The metadata to associate with the log record.
*/
warn(message : string, metadata? : any) : void;
/**
* Writes a log record that highlights errors that wouldn't normally stop the application from running.
*
* Example usage:
*
* ``` js
* logger.error('Something bad happened.', {
* detail : 'Detail on what happened...'
* });
* ```
*
* @param message The message to describe the log record.
* @param metadata The metadata to associate with the log record.
*/
error(message : string, metadata? : any) : void;
/**
* Writes a log record that highlights severe errors that may stop the application from running.
*
* Example usage:
*
* ``` js
* logger.fatal('Something really bad happened.', {
* detail : 'Detail on what happened...'
* });
* ```
*
* @param message The message to describe the log record.
* @param metadata The metadata to associate with the log record.
*/
fatal(message : string, metadata? : any) : void;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Represents a factory responsible for creating loggers.
*/
export class LoggerFactory
{
/**
* The loggers that have been created by this factory.
*/
readonly loggers : Logger[];
/**
* The stream that all loggers created by this factory will write log records to.
*/
readonly stream : stream.Readable;
/**
* Creates a logger.
*
* Example usage:
*
* ``` js
* const logger = factory.getLogger('http');
*
* logger.error('A request has failed.', {
* url : 'https://www.zelda.com'
* });
* ```
*
* If a logger already exists with the provided name, then that logger will be returned instead:
*
* ``` js
* factory.getLogger('http') === factory.getLogger('http');
* // => true
* ```
*
* @param name The name of the logger.
*
* @returns The created logger, or the already existing logger with `name` as its name.
*/
getLogger(name : string) : Logger;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* A synchronous function that will transform a log record into something else.
*
* @param record The log record being transformed.
*
* @returns The transformed log record.
*/
export interface RecordTransformer
{
(record : Record) : any;
}
/**
* A synchronous function that will determine whether a log record meets some condition(s).
*
* @param record The log record being tested.
*
* @returns `true` if the record meets the condition(s), otherwise `false`.
*/
export interface RecordTester
{
(record : Record) : boolean;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Creates a transformation stream that will output the input log record transformed by a given transformer.
*
* Example usage:
*
* ``` js
* const formatter = transform(record =>
* {
* return `${record.category} - ${record.message}`;
* });
*
* factory.stream.pipe(formatter);
* ```
*
* @param transform The function that will be used to transform each input log record.
*
* @returns A transformation stream that you can pipe a stream of log records into.
*/
export function transform(transform : RecordTransformer) : stream.Transform;
/**
* Creates a transformation stream that will only output the input log record if said log record passes a given test.
*
* Example usage:
*
* ``` js
* const refine = filter(record =>
* {
* return record.level.isGreaterThan(Level.WARN);
* });
*
* factory.stream.pipe(refine);
* ```
*
* @param test The function that will be used to test each input log record.
*
* @returns A transformation stream that you can pipe a stream of log records into.
*/
export function filter(test : RecordTester) : stream.Transform;