Skip to content

Commit 0e49907

Browse files
authored
DefaultLogger respects the LogLevel.debug and simplify stderr usage (#7966)
1 parent bfa873d commit 0e49907

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

.changeset/orange-buttons-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/utils': patch
3+
---
4+
5+
DefaultLogger respects the LogLevel.debug

packages/legacy/utils/src/logger.ts

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unused-expressions -- simply more convenient to do `this.stderr(m) || console.log(m)` */
12
import { process, util } from '@graphql-mesh/cross-helpers';
23
import type { LazyLoggerMessage, Logger } from '@graphql-mesh/types';
34

@@ -79,17 +80,30 @@ export class DefaultLogger implements Logger {
7980
return this.getLoggerMessage({ args: flattenedArgs });
8081
}
8182

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;
8994
}
9095
return false;
9196
}
9297

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+
93107
private get prefix() {
94108
return this.name ? titleBold(this.name) : ``;
95109
}
@@ -100,10 +114,7 @@ export class DefaultLogger implements Logger {
100114
}
101115
const message = this.getLoggerMessage({ args });
102116
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);
107118
}
108119

109120
warn(...args: any[]) {
@@ -112,10 +123,7 @@ export class DefaultLogger implements Logger {
112123
}
113124
const message = this.getLoggerMessage({ args });
114125
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);
119127
}
120128

121129
info(...args: any[]) {
@@ -126,11 +134,7 @@ export class DefaultLogger implements Logger {
126134
args,
127135
});
128136
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);
134138
}
135139

136140
error(...args: any[]) {
@@ -139,28 +143,18 @@ export class DefaultLogger implements Logger {
139143
}
140144
const message = this.getLoggerMessage({ args });
141145
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);
147147
}
148148

149149
debug(...lazyArgs: LazyLoggerMessage[]) {
150-
if (this.logLevel > LogLevel.debug) {
150+
if (!this.isDebug /** also checks whether the loglevel is at least debug */) {
151151
return noop;
152152
}
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);
164158
}
165159

166160
child(name: string): Logger {

0 commit comments

Comments
 (0)