Skip to content

Commit

Permalink
feat(core): Comply with NO_COLOR in logs (#12347)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Dec 30, 2024
1 parent 78ef2ce commit 1e60bbc
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions packages/core/src/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export class Logger implements LoggerType {
return this.scopes.size > 0;
}

/** https://no-color.org/ */
private readonly noColor = process.env.NO_COLOR !== undefined && process.env.NO_COLOR !== '';

constructor(
private readonly globalConfig: GlobalConfig,
private readonly instanceSettingsConfig: InstanceSettingsConfig,
Expand Down Expand Up @@ -129,18 +132,22 @@ export class Logger implements LoggerType {
})();
}

private color() {
return this.noColor ? winston.format.uncolorize() : winston.format.colorize({ all: true });
}

private debugDevConsoleFormat() {
return winston.format.combine(
winston.format.metadata(),
winston.format.timestamp({ format: () => this.devTsFormat() }),
winston.format.colorize({ all: true }),
this.color(),
this.scopeFilter(),
winston.format.printf(({ level: _level, message, timestamp, metadata: _metadata }) => {
const SEPARATOR = ' '.repeat(3);
const LOG_LEVEL_COLUMN_WIDTH = 15; // 5 columns + ANSI color codes
const level = _level.toLowerCase().padEnd(LOG_LEVEL_COLUMN_WIDTH, ' ');
const metadata = this.toPrintable(_metadata);
return [timestamp, level, message + ' ' + pc.dim(metadata)].join(SEPARATOR);
winston.format.printf(({ level: rawLevel, message, timestamp, metadata: rawMetadata }) => {
const separator = ' '.repeat(3);
const logLevelColumnWidth = this.noColor ? 5 : 15; // when colorizing, account for ANSI color codes
const level = rawLevel.toLowerCase().padEnd(logLevelColumnWidth, ' ');
const metadata = this.toPrintable(rawMetadata);
return [timestamp, level, message + ' ' + pc.dim(metadata)].join(separator);
}),
);
}
Expand All @@ -149,10 +156,11 @@ export class Logger implements LoggerType {
return winston.format.combine(
winston.format.metadata(),
winston.format.timestamp(),
this.color(),
this.scopeFilter(),
winston.format.printf(({ level, message, timestamp, metadata }) => {
const _metadata = this.toPrintable(metadata);
return `${timestamp} | ${level.padEnd(5)} | ${message}${_metadata ? ' ' + _metadata : ''}`;
winston.format.printf(({ level, message, timestamp, metadata: rawMetadata }) => {
const metadata = this.toPrintable(rawMetadata);
return `${timestamp} | ${level.padEnd(5)} | ${message}${metadata ? ' ' + metadata : ''}`;
}),
);
}
Expand Down

0 comments on commit 1e60bbc

Please # to comment.