@@ -39,14 +39,20 @@ export class Time {
39
39
) { }
40
40
}
41
41
42
+ // tslint:disable-next-line no-any
43
+ export type FieldArray = Array < Field < any > > ;
44
+
45
+ // Same as FieldArray but supports taking an object or undefined.
42
46
// `undefined` is allowed to make it easy to conditionally display a field.
43
- // For example: `error && field("error", error)`
47
+ // For example: `error && field("error", error)`.
48
+ // For objects, the logger will iterate over the keys and turn them into
49
+ // instances of Field.
44
50
// tslint:disable-next-line no-any
45
- export type FieldArray = Array < Field < any > | undefined > ;
51
+ export type LogArgument = Array < Field < any > | undefined | object > ;
46
52
47
53
// Functions can be used to remove the need to perform operations when the
48
54
// logging level won't output the result anyway.
49
- export type LogCallback = ( ) => [ string , ...FieldArray ] ;
55
+ export type LogCallback = ( ) => [ string , ...LogArgument ] ;
50
56
51
57
/**
52
58
* Creates a time field
@@ -82,13 +88,19 @@ export abstract class Formatter {
82
88
public abstract tag ( name : string , color : string ) : void ;
83
89
84
90
/**
85
- * Add string or arbitrary variable.
91
+ * Add string variable.
86
92
*/
87
93
public abstract push ( arg : string , color ?: string , weight ?: string ) : void ;
94
+ /**
95
+ * Add arbitrary variable.
96
+ */
88
97
public abstract push ( arg : any ) : void ; // tslint:disable-line no-any
89
98
99
+ /**
100
+ * Add fields.
101
+ */
90
102
// tslint:disable-next-line no-any
91
- public abstract fields ( fields : Array < Field < any > > ) : void ;
103
+ public abstract fields ( fields : FieldArray ) : void ;
92
104
93
105
/**
94
106
* Flush out the built arguments.
@@ -120,6 +132,9 @@ export abstract class Formatter {
120
132
* Browser formatter.
121
133
*/
122
134
export class BrowserFormatter extends Formatter {
135
+ /**
136
+ * Add a tag.
137
+ */
123
138
public tag ( name : string , color : string ) : void {
124
139
this . format += `%c ${ name } ` ;
125
140
this . args . push (
@@ -131,6 +146,9 @@ export class BrowserFormatter extends Formatter {
131
146
this . push ( " " ) ;
132
147
}
133
148
149
+ /**
150
+ * Add an argument.
151
+ */
134
152
public push ( arg : any , color : string = "inherit" , weight : string = "normal" ) : void { // tslint:disable-line no-any
135
153
if ( color || weight ) {
136
154
this . format += "%c" ;
@@ -143,8 +161,11 @@ export class BrowserFormatter extends Formatter {
143
161
this . args . push ( arg ) ;
144
162
}
145
163
164
+ /**
165
+ * Add fields.
166
+ */
146
167
// tslint:disable-next-line no-any
147
- public fields ( fields : Array < Field < any > > ) : void {
168
+ public fields ( fields : FieldArray ) : void {
148
169
// tslint:disable-next-line no-console
149
170
console . groupCollapsed ( ...this . flush ( ) ) ;
150
171
fields . forEach ( ( field ) => {
@@ -166,6 +187,9 @@ export class BrowserFormatter extends Formatter {
166
187
* Server (Node) formatter.
167
188
*/
168
189
export class ServerFormatter extends Formatter {
190
+ /**
191
+ * Add a tag.
192
+ */
169
193
public tag ( name : string , color : string ) : void {
170
194
const [ r , g , b ] = this . hexToRgb ( color ) ;
171
195
while ( name . length < 5 ) {
@@ -175,6 +199,9 @@ export class ServerFormatter extends Formatter {
175
199
this . format += `\u001B[38;2;${ r } ;${ g } ;${ b } m${ name } \u001B[0m` ;
176
200
}
177
201
202
+ /**
203
+ * Add an argument.
204
+ */
178
205
public push ( arg : any , color ?: string , weight ?: string ) : void { // tslint:disable-line no-any
179
206
if ( weight === "bold" ) {
180
207
this . format += "\u001B[1m" ;
@@ -190,8 +217,11 @@ export class ServerFormatter extends Formatter {
190
217
this . args . push ( arg ) ;
191
218
}
192
219
220
+ /**
221
+ * Add fields.
222
+ */
193
223
// tslint:disable-next-line no-any
194
- public fields ( fields : Array < Field < any > > ) : void {
224
+ public fields ( fields : FieldArray ) : void {
195
225
// tslint:disable-next-line no-any
196
226
const obj : { [ key : string ] : any } = { } ;
197
227
this . format += "\u001B[38;2;140;140;140m" ;
@@ -257,80 +287,83 @@ export class Logger {
257
287
this . muted = true ;
258
288
}
259
289
290
+ /**
291
+ * Extend the logger (for example to send the logs elsewhere).
292
+ */
260
293
public extend ( extender : Extender ) : void {
261
294
this . extenders . push ( extender ) ;
262
295
}
263
296
264
297
/**
265
- * Outputs information.
298
+ * Log information.
266
299
*/
267
300
public info ( fn : LogCallback ) : void ;
268
- public info ( message : string , ...fields : FieldArray ) : void ;
269
- public info ( message : LogCallback | string , ...fields : FieldArray ) : void {
301
+ public info ( message : string , ...args : LogArgument ) : void ;
302
+ public info ( message : LogCallback | string , ...args : LogArgument ) : void {
270
303
this . handle ( {
271
304
type : "info" ,
272
305
message,
273
- fields ,
306
+ args ,
274
307
tagColor : "#008FBF" ,
275
308
level : Level . Info ,
276
309
} ) ;
277
310
}
278
311
279
312
/**
280
- * Outputs a warning.
313
+ * Log a warning.
281
314
*/
282
315
public warn ( fn : LogCallback ) : void ;
283
- public warn ( message : string , ...fields : FieldArray ) : void ;
284
- public warn ( message : LogCallback | string , ...fields : FieldArray ) : void {
316
+ public warn ( message : string , ...args : LogArgument ) : void ;
317
+ public warn ( message : LogCallback | string , ...args : LogArgument ) : void {
285
318
this . handle ( {
286
319
type : "warn" ,
287
320
message,
288
- fields ,
321
+ args ,
289
322
tagColor : "#FF9D00" ,
290
323
level : Level . Warning ,
291
324
} ) ;
292
325
}
293
326
294
327
/**
295
- * Outputs a trace message.
328
+ * Log a trace message.
296
329
*/
297
330
public trace ( fn : LogCallback ) : void ;
298
- public trace ( message : string , ...fields : FieldArray ) : void ;
299
- public trace ( message : LogCallback | string , ...fields : FieldArray ) : void {
331
+ public trace ( message : string , ...args : LogArgument ) : void ;
332
+ public trace ( message : LogCallback | string , ...args : LogArgument ) : void {
300
333
this . handle ( {
301
334
type : "trace" ,
302
335
message,
303
- fields ,
336
+ args ,
304
337
tagColor : "#888888" ,
305
338
level : Level . Trace ,
306
339
} ) ;
307
340
}
308
341
309
342
/**
310
- * Outputs a debug message.
343
+ * Log a debug message.
311
344
*/
312
345
public debug ( fn : LogCallback ) : void ;
313
- public debug ( message : string , ...fields : FieldArray ) : void ;
314
- public debug ( message : LogCallback | string , ...fields : FieldArray ) : void {
346
+ public debug ( message : string , ...args : LogArgument ) : void ;
347
+ public debug ( message : LogCallback | string , ...args : LogArgument ) : void {
315
348
this . handle ( {
316
349
type : "debug" ,
317
350
message,
318
- fields ,
351
+ args ,
319
352
tagColor : "#84009E" ,
320
353
level : Level . Debug ,
321
354
} ) ;
322
355
}
323
356
324
357
/**
325
- * Outputs an error.
358
+ * Log an error.
326
359
*/
327
360
public error ( fn : LogCallback ) : void ;
328
- public error ( message : string , ...fields : FieldArray ) : void ;
329
- public error ( message : LogCallback | string , ...fields : FieldArray ) : void {
361
+ public error ( message : string , ...args : LogArgument ) : void ;
362
+ public error ( message : LogCallback | string , ...args : LogArgument ) : void {
330
363
this . handle ( {
331
364
type : "error" ,
332
365
message,
333
- fields ,
366
+ args ,
334
367
tagColor : "#B00000" ,
335
368
level : Level . Error ,
336
369
} ) ;
@@ -350,29 +383,41 @@ export class Logger {
350
383
}
351
384
352
385
/**
353
- * Outputs a message.
386
+ * Log a message.
354
387
*/
355
388
private handle ( options : {
356
389
type : "trace" | "info" | "warn" | "debug" | "error" ;
357
390
message : string | LogCallback ;
358
- fields ?: FieldArray ;
391
+ args ?: LogArgument ;
359
392
level : Level ;
360
393
tagColor : string ;
361
394
} ) : void {
362
395
if ( this . level > options . level || this . muted ) {
363
396
return ;
364
397
}
365
398
366
- let passedFields = options . fields || [ ] ;
367
399
if ( typeof options . message === "function" ) {
368
400
const values = options . message ( ) ;
369
401
options . message = values . shift ( ) as string ;
370
- passedFields = values as FieldArray ;
402
+ options . args = values as FieldArray ;
371
403
}
372
404
373
- const fields = ( this . defaultFields
374
- ? passedFields . filter ( ( f ) => ! ! f ) . concat ( this . defaultFields )
375
- : passedFields . filter ( ( f ) => ! ! f ) ) as Array < Field < any > > ; // tslint:disable-line no-any
405
+ const fields : FieldArray = [ ] ;
406
+ if ( options . args ) {
407
+ options . args . forEach ( ( arg ) => {
408
+ if ( arg instanceof Field ) {
409
+ fields . push ( arg ) ;
410
+ } else if ( arg ) {
411
+ Object . keys ( arg ) . forEach ( ( k ) => {
412
+ // tslint:disable-next-line no-any
413
+ fields . push ( field ( k , ( arg as any ) [ k ] ) ) ;
414
+ } ) ;
415
+ }
416
+ } ) ;
417
+ }
418
+ if ( this . defaultFields ) {
419
+ fields . push ( ...this . defaultFields ) ;
420
+ }
376
421
377
422
const now = Date . now ( ) ;
378
423
let times : Array < Field < Time > > = [ ] ;
@@ -410,7 +455,7 @@ export class Logger {
410
455
this . extenders . forEach ( ( extender ) => {
411
456
extender ( {
412
457
section : this . name ,
413
- fields : options . fields ,
458
+ fields,
414
459
level : options . level ,
415
460
message : options . message as string ,
416
461
type : options . type ,
0 commit comments