@@ -14,24 +14,48 @@ export function timestamp (d = new Date()) {
1414 . slice ( 0 , - 3 )
1515}
1616
17+ const serializer = ( k , v ) => ( typeof v === 'bigint' ) ? String ( v ) : v
18+ const toJSON = data => JSON . stringify ( data , serializer )
19+
1720export class Console extends defineCallable ( function log ( ...args ) {
1821 this . log ( ...args )
1922} ) {
2023
2124 constructor ( label , options = { } ) {
2225 super ( )
23- this . label = options . label ?? label ?? this . label ?? ''
26+ this . label = options . label ?? label ?? this . label ?? ''
2427 this . parent = options . parent ?? console
28+ this . _print = options . json
29+ ? ( method , tag , message ) => {
30+ this . parent [ method ] ( toJSON ( { logTag : tag , logMethod : method , logMessage : message } ) )
31+ return this
32+ }
33+ : ( method , tag , ...args ) => {
34+ this . parent [ method ] ( tag , ...args )
35+ return this
36+ }
37+ this . _tag = ( options . color && ! options . json )
38+ ? ( color , string ) => {
39+ const tag1 = ( string ? ( chalk . inverse ( color ( bold ( string ) ) ) + ( this . label ? ' ' : '' ) ) : '' )
40+ const tag2 = ( this . label ? color ( this . label ) : '' )
41+ return tag1 + tag2
42+ }
43+ : ( color , string ) => {
44+ const tag1 = string
45+ const tag2 = this . label
46+ return tag1 + tag2
47+ }
2548 hideProperties ( this ,
26- 'label' , 'tags' , 'tag' , '_tag' , 'parent' , 'sub' ,
49+ 'label' , 'tags' , 'tag' , '_tag' , '_print' , ' parent', 'sub' ,
2750 'br' , 'log' , 'info' , 'warn' , 'error' , 'debug' , 'trace' , 'table' ,
2851 '_print' ,
2952 )
3053 }
3154
3255 label
33-
3456 parent
57+ _print
58+ _tag
3559
3660 sub = ( label , options = { } ) => new SubConsole ( label , { ...options , parent : this } )
3761 br = ( ) => { this . parent . log ( ) ; return this }
@@ -46,16 +70,6 @@ export class Console extends defineCallable(function log(...args){
4670 this . _print ( 'table' , ...args )
4771 }
4872
49- _print = ( method , tag , ...args ) => {
50- this . parent [ method ] ( tag , ...args ) ;
51- return this
52- }
53-
54- _tag = ( color , string ) => {
55- const tag1 = ( string ? ( chalk . inverse ( color ( bold ( string ) ) ) + ( this . label ? ' ' : '' ) ) : '' )
56- const tag2 = ( this . label ? color ( this . label ) : '' )
57- return tag1 + tag2
58- }
5973
6074 get [ Symbol . toStringTag ] ( ) {
6175 return this . label
0 commit comments