1
+ /* eslint-disable @typescript-eslint/no-unused-expressions -- simply more convenient to do `this.stderr(m) || console.log(m)` */
1
2
import { process , util } from '@graphql-mesh/cross-helpers' ;
2
3
import type { LazyLoggerMessage , Logger } from '@graphql-mesh/types' ;
3
4
@@ -79,17 +80,30 @@ export class DefaultLogger implements Logger {
79
80
return this . getLoggerMessage ( { args : flattenedArgs } ) ;
80
81
}
81
82
82
- private get isDebug ( ) {
83
- if ( process . env . DEBUG ) {
84
- return (
85
- truthy ( process . env . DEBUG ) ||
86
- truthy ( ( globalThis as any ) . DEBUG ) ||
87
- this . name . includes ( process . env . DEBUG || ( globalThis as any ) . DEBUG )
88
- ) ;
83
+ /**
84
+ * Tries writing to the process stderr. If unable, will return
85
+ * false so that the logger falls back to using the console.
86
+ *
87
+ * If process stderr is used, a new line is automatically appended
88
+ * after the {@link msg}.
89
+ */
90
+ private stderr ( msg : string ) {
91
+ if ( typeof process ?. stderr ?. write === 'function' ) {
92
+ process . stderr . write ( msg + '\n' ) ;
93
+ return true ;
89
94
}
90
95
return false ;
91
96
}
92
97
98
+ private get isDebug ( ) {
99
+ return (
100
+ this . logLevel <= LogLevel . debug ||
101
+ truthy ( process . env . DEBUG ) ||
102
+ truthy ( globalThis . DEBUG ) ||
103
+ this . name ?. includes ( process . env . DEBUG || globalThis . DEBUG )
104
+ ) ;
105
+ }
106
+
93
107
private get prefix ( ) {
94
108
return this . name ? titleBold ( this . name ) : `` ;
95
109
}
@@ -100,10 +114,7 @@ export class DefaultLogger implements Logger {
100
114
}
101
115
const message = this . getLoggerMessage ( { args } ) ;
102
116
const fullMessage = `[${ getTimestamp ( ) } ] ${ this . prefix } ${ message } ` ;
103
- if ( process ?. stderr ?. write ( fullMessage + '\n' ) ) {
104
- return ;
105
- }
106
- console . log ( fullMessage ) ;
117
+ this . stderr ( fullMessage ) || console . log ( fullMessage ) ;
107
118
}
108
119
109
120
warn ( ...args : any [ ] ) {
@@ -112,10 +123,7 @@ export class DefaultLogger implements Logger {
112
123
}
113
124
const message = this . getLoggerMessage ( { args } ) ;
114
125
const fullMessage = `[${ getTimestamp ( ) } ] WARN ${ this . prefix } ${ warnColor ( message ) } ` ;
115
- if ( process ?. stderr ?. write ( fullMessage + '\n' ) ) {
116
- return ;
117
- }
118
- console . warn ( fullMessage ) ;
126
+ this . stderr ( fullMessage ) || console . warn ( fullMessage ) ;
119
127
}
120
128
121
129
info ( ...args : any [ ] ) {
@@ -126,11 +134,7 @@ export class DefaultLogger implements Logger {
126
134
args,
127
135
} ) ;
128
136
const fullMessage = `[${ getTimestamp ( ) } ] INFO ${ this . prefix } ${ infoColor ( message ) } ` ;
129
- if ( typeof process ?. stderr ?. write === 'function' ) {
130
- process . stderr . write ( fullMessage + '\n' ) ;
131
- return ;
132
- }
133
- console . info ( fullMessage ) ;
137
+ this . stderr ( fullMessage ) || console . info ( fullMessage ) ;
134
138
}
135
139
136
140
error ( ...args : any [ ] ) {
@@ -139,28 +143,18 @@ export class DefaultLogger implements Logger {
139
143
}
140
144
const message = this . getLoggerMessage ( { args } ) ;
141
145
const fullMessage = `[${ getTimestamp ( ) } ] ERROR ${ this . prefix } ${ errorColor ( message ) } ` ;
142
- if ( typeof process ?. stderr ?. write === 'function' ) {
143
- process . stderr . write ( fullMessage + '\n' ) ;
144
- return ;
145
- }
146
- console . error ( fullMessage ) ;
146
+ this . stderr ( fullMessage ) || console . error ( fullMessage ) ;
147
147
}
148
148
149
149
debug ( ...lazyArgs : LazyLoggerMessage [ ] ) {
150
- if ( this . logLevel > LogLevel . debug ) {
150
+ if ( ! this . isDebug /** also checks whether the loglevel is at least debug */ ) {
151
151
return noop ;
152
152
}
153
- if ( this . isDebug ) {
154
- const message = this . handleLazyMessage ( {
155
- lazyArgs,
156
- } ) ;
157
- const fullMessage = `[${ getTimestamp ( ) } ] DEBUG ${ this . prefix } ${ debugColor ( message ) } ` ;
158
- if ( typeof process ?. stderr ?. write === 'function' ) {
159
- process . stderr . write ( fullMessage + '\n' ) ;
160
- return ;
161
- }
162
- console . debug ( fullMessage ) ;
163
- }
153
+ const message = this . handleLazyMessage ( {
154
+ lazyArgs,
155
+ } ) ;
156
+ const fullMessage = `[${ getTimestamp ( ) } ] DEBUG ${ this . prefix } ${ debugColor ( message ) } ` ;
157
+ this . stderr ( fullMessage ) || console . debug ( fullMessage ) ;
164
158
}
165
159
166
160
child ( name : string ) : Logger {
0 commit comments