16
16
17
17
const LOG_LEVELS = [ 'debug' , 'info' , 'warn' , 'error' ] ;
18
18
19
- class Logger {
20
- logger : {
21
- debug ( ...args : any ) ;
22
- info ( ...args : any ) ;
23
- warn ( ...args : any ) ;
24
- error ( ...args : any ) ;
25
- level : string ;
26
- } ;
19
+ const loggerKey = '__genkit_logger' ;
27
20
28
- defaultLogger = {
29
- shouldLog ( targetLevel : string ) {
30
- return LOG_LEVELS . indexOf ( this . level ) <= LOG_LEVELS . indexOf ( targetLevel ) ;
31
- } ,
32
- debug ( ...args : any ) {
33
- this . shouldLog ( 'debug' ) && console . debug ( ...args ) ;
34
- } ,
35
- info ( ...args : any ) {
36
- this . shouldLog ( 'info' ) && console . info ( ...args ) ;
37
- } ,
38
- warn ( ...args : any ) {
39
- this . shouldLog ( 'warn' ) && console . warn ( ...args ) ;
40
- } ,
41
- error ( ...args : any ) {
42
- this . shouldLog ( 'error' ) && console . error ( ...args ) ;
43
- } ,
44
- level : 'info' ,
45
- } ;
21
+ const _defaultLogger = {
22
+ shouldLog ( targetLevel : string ) {
23
+ return LOG_LEVELS . indexOf ( this . level ) <= LOG_LEVELS . indexOf ( targetLevel ) ;
24
+ } ,
25
+ debug ( ...args : any ) {
26
+ this . shouldLog ( 'debug' ) && console . debug ( ...args ) ;
27
+ } ,
28
+ info ( ...args : any ) {
29
+ this . shouldLog ( 'info' ) && console . info ( ...args ) ;
30
+ } ,
31
+ warn ( ...args : any ) {
32
+ this . shouldLog ( 'warn' ) && console . warn ( ...args ) ;
33
+ } ,
34
+ error ( ...args : any ) {
35
+ this . shouldLog ( 'error' ) && console . error ( ...args ) ;
36
+ } ,
37
+ level : 'info' ,
38
+ } ;
46
39
47
- constructor ( ) {
48
- this . logger = this . defaultLogger ;
40
+ function getLogger ( ) {
41
+ if ( ! global [ loggerKey ] ) {
42
+ global [ loggerKey ] = _defaultLogger ;
49
43
}
44
+ return global [ loggerKey ] ;
45
+ }
46
+
47
+ class Logger {
48
+ readonly defaultLogger = _defaultLogger ;
50
49
51
- async init ( fn : any ) {
52
- this . logger = fn ;
50
+ init ( fn : any ) {
51
+ global [ loggerKey ] = fn ;
53
52
}
54
53
55
54
info ( ...args : any ) {
56
55
// eslint-disable-next-line prefer-spread
57
- this . logger . info . apply ( this . logger , args ) ;
56
+ getLogger ( ) . info . apply ( getLogger ( ) , args ) ;
58
57
}
59
58
debug ( ...args : any ) {
60
59
// eslint-disable-next-line prefer-spread
61
- this . logger . debug . apply ( this . logger , args ) ;
60
+ getLogger ( ) . debug . apply ( getLogger ( ) , args ) ;
62
61
}
63
62
error ( ...args : any ) {
64
63
// eslint-disable-next-line prefer-spread
65
- this . logger . error . apply ( this . logger , args ) ;
64
+ getLogger ( ) . error . apply ( getLogger ( ) , args ) ;
66
65
}
67
66
warn ( ...args : any ) {
68
67
// eslint-disable-next-line prefer-spread
69
- this . logger . warn . apply ( this . logger , args ) ;
68
+ getLogger ( ) . warn . apply ( getLogger ( ) , args ) ;
70
69
}
71
70
72
71
setLogLevel ( level : 'error' | 'warn' | 'info' | 'debug' ) {
73
- this . logger . level = level ;
72
+ getLogger ( ) . level = level ;
74
73
}
75
74
76
75
logStructured ( msg : string , metadata : any ) {
77
- this . logger . info ( msg , metadata ) ;
76
+ getLogger ( ) . info ( msg , metadata ) ;
78
77
}
79
78
80
79
logStructuredError ( msg : string , metadata : any ) {
81
- this . logger . error ( msg , metadata ) ;
80
+ getLogger ( ) . error ( msg , metadata ) ;
82
81
}
83
82
}
84
83
85
- export const logger = new Logger ( ) ;
84
+ export const logger = new Logger ( ) ;
0 commit comments