Skip to content

Commit

Permalink
DefaultLogger respects the LogLevel.debug and simplify stderr usage (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo authored Nov 20, 2024
1 parent bfa873d commit 0e49907
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-buttons-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/utils': patch
---

DefaultLogger respects the LogLevel.debug
68 changes: 31 additions & 37 deletions packages/legacy/utils/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-expressions -- simply more convenient to do `this.stderr(m) || console.log(m)` */
import { process, util } from '@graphql-mesh/cross-helpers';
import type { LazyLoggerMessage, Logger } from '@graphql-mesh/types';

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

private get isDebug() {
if (process.env.DEBUG) {
return (
truthy(process.env.DEBUG) ||
truthy((globalThis as any).DEBUG) ||
this.name.includes(process.env.DEBUG || (globalThis as any).DEBUG)
);
/**
* Tries writing to the process stderr. If unable, will return
* false so that the logger falls back to using the console.
*
* If process stderr is used, a new line is automatically appended
* after the {@link msg}.
*/
private stderr(msg: string) {
if (typeof process?.stderr?.write === 'function') {
process.stderr.write(msg + '\n');
return true;
}
return false;
}

private get isDebug() {
return (
this.logLevel <= LogLevel.debug ||
truthy(process.env.DEBUG) ||
truthy(globalThis.DEBUG) ||
this.name?.includes(process.env.DEBUG || globalThis.DEBUG)
);
}

private get prefix() {
return this.name ? titleBold(this.name) : ``;
}
Expand All @@ -100,10 +114,7 @@ export class DefaultLogger implements Logger {
}
const message = this.getLoggerMessage({ args });
const fullMessage = `[${getTimestamp()}] ${this.prefix} ${message}`;
if (process?.stderr?.write(fullMessage + '\n')) {
return;
}
console.log(fullMessage);
this.stderr(fullMessage) || console.log(fullMessage);
}

warn(...args: any[]) {
Expand All @@ -112,10 +123,7 @@ export class DefaultLogger implements Logger {
}
const message = this.getLoggerMessage({ args });
const fullMessage = `[${getTimestamp()}] WARN ${this.prefix} ${warnColor(message)}`;
if (process?.stderr?.write(fullMessage + '\n')) {
return;
}
console.warn(fullMessage);
this.stderr(fullMessage) || console.warn(fullMessage);
}

info(...args: any[]) {
Expand All @@ -126,11 +134,7 @@ export class DefaultLogger implements Logger {
args,
});
const fullMessage = `[${getTimestamp()}] INFO ${this.prefix} ${infoColor(message)}`;
if (typeof process?.stderr?.write === 'function') {
process.stderr.write(fullMessage + '\n');
return;
}
console.info(fullMessage);
this.stderr(fullMessage) || console.info(fullMessage);
}

error(...args: any[]) {
Expand All @@ -139,28 +143,18 @@ export class DefaultLogger implements Logger {
}
const message = this.getLoggerMessage({ args });
const fullMessage = `[${getTimestamp()}] ERROR ${this.prefix} ${errorColor(message)}`;
if (typeof process?.stderr?.write === 'function') {
process.stderr.write(fullMessage + '\n');
return;
}
console.error(fullMessage);
this.stderr(fullMessage) || console.error(fullMessage);
}

debug(...lazyArgs: LazyLoggerMessage[]) {
if (this.logLevel > LogLevel.debug) {
if (!this.isDebug /** also checks whether the loglevel is at least debug */) {
return noop;
}
if (this.isDebug) {
const message = this.handleLazyMessage({
lazyArgs,
});
const fullMessage = `[${getTimestamp()}] DEBUG ${this.prefix} ${debugColor(message)}`;
if (typeof process?.stderr?.write === 'function') {
process.stderr.write(fullMessage + '\n');
return;
}
console.debug(fullMessage);
}
const message = this.handleLazyMessage({
lazyArgs,
});
const fullMessage = `[${getTimestamp()}] DEBUG ${this.prefix} ${debugColor(message)}`;
this.stderr(fullMessage) || console.debug(fullMessage);
}

child(name: string): Logger {
Expand Down

0 comments on commit 0e49907

Please sign in to comment.