Skip to content

Commit

Permalink
fix(utils): DefaultLogger does not trim messages by default and accep…
Browse files Browse the repository at this point in the history
…ts more truthy strings for DEBUG env var (#7467)
  • Loading branch information
enisdenjo authored Aug 6, 2024
1 parent 44f6710 commit a324c5e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
12 changes: 12 additions & 0 deletions .changeset/gorgeous-moons-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@graphql-mesh/utils': minor
---

Trimming log messages is an option independant of the DEBUG environment variable.

Instead of trimming messages at 100 characters by default when the `DEBUG` environment variable is falsy, have the user configure the trim length that is not set by default.

```js
import { DefaultLogger } from '@graphql-mesh/utils';
const trimmedLogger = new DefaultLogger('my-logger', undefined, 100 /* trim at 100 characters*/);
``
5 changes: 5 additions & 0 deletions .changeset/twenty-apes-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/utils': patch
---

Accept '1', 't', 'true', 'y' and 'yes' as truthy values for DEBUG environment variable
41 changes: 18 additions & 23 deletions packages/legacy/utils/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,31 @@ export enum LogLevel {

const noop: VoidFunction = () => {};

function truthy(str: unknown) {
return str === true || str === 1 || ['1', 't', 'true', 'y', 'yes'].includes(String(str));
}

function getTimestamp() {
return new Date().toISOString();
}

export class DefaultLogger implements Logger {
constructor(
public name?: string,
public logLevel = process.env.DEBUG === '1' ? LogLevel.debug : LogLevel.info,
public logLevel = truthy(process.env.DEBUG) ? LogLevel.debug : LogLevel.info,
private trim?: number,
) {}

private getLoggerMessage({ args = [], trim = !this.isDebug }: { args: any[]; trim?: boolean }) {
private getLoggerMessage({ args = [] }: { args: any[] }) {
return args
.flat(Infinity)
.map(arg => {
if (typeof arg === 'string') {
if (trim && arg.length > 100) {
if (this.trim && arg.length > this.trim) {
return (
arg.slice(0, 100) +
arg.slice(0, this.trim) +
'...' +
'<Message is too long. Enable DEBUG=1 to see the full message.>'
'<Message is trimmed. Enable DEBUG=1 to see the full message.>'
);
}
return arg;
Expand All @@ -61,27 +66,24 @@ export class DefaultLogger implements Logger {
}
return util.inspect(arg);
})
.join(` `);
.join(' ');
}

private handleLazyMessage({ lazyArgs, trim }: { lazyArgs: LazyLoggerMessage[]; trim?: boolean }) {
private handleLazyMessage({ lazyArgs }: { lazyArgs: LazyLoggerMessage[] }) {
const flattenedArgs = lazyArgs.flat(Infinity).flatMap(arg => {
if (typeof arg === 'function') {
return arg();
}
return arg;
});
return this.getLoggerMessage({
args: flattenedArgs,
trim,
});
return this.getLoggerMessage({ args: flattenedArgs });
}

private get isDebug() {
if (process.env.DEBUG) {
return (
process.env.DEBUG === '1' ||
(globalThis as any).DEBUG === '1' ||
truthy(process.env.DEBUG) ||
truthy((globalThis as any).DEBUG) ||
this.name.includes(process.env.DEBUG || (globalThis as any).DEBUG)
);
}
Expand All @@ -96,9 +98,7 @@ export class DefaultLogger implements Logger {
if (this.logLevel > LogLevel.info) {
return noop;
}
const message = this.getLoggerMessage({
args,
});
const message = this.getLoggerMessage({ args });
const fullMessage = `[${getTimestamp()}] ${this.prefix} ${message}`;
if (process?.stderr?.write(fullMessage + '\n')) {
return;
Expand All @@ -110,9 +110,7 @@ export class DefaultLogger implements Logger {
if (this.logLevel > LogLevel.warn) {
return noop;
}
const message = this.getLoggerMessage({
args,
});
const message = this.getLoggerMessage({ args });
const fullMessage = `[${getTimestamp()}] ${this.prefix} WARN ${warnColor(message)}`;
if (process?.stderr?.write(fullMessage + '\n')) {
return;
Expand All @@ -139,10 +137,7 @@ export class DefaultLogger implements Logger {
if (this.logLevel > LogLevel.error) {
return noop;
}
const message = this.getLoggerMessage({
args,
trim: false,
});
const message = this.getLoggerMessage({ args });
const fullMessage = `[${getTimestamp()}] ERROR ${this.prefix} ${errorColor(message)}`;
if (typeof process?.stderr?.write === 'function') {
process.stderr.write(fullMessage + '\n');
Expand Down

0 comments on commit a324c5e

Please sign in to comment.