Skip to content

Commit 42f618d

Browse files
committed
Disable ansi colours if not supported
1 parent b4cc1ad commit 42f618d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

backend/src/common/logger/logger.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export function moduleLogger(): Logger {
2424
return new Logger(discriminator);
2525
}
2626

27+
const HAS_COLORS = process.stdout.hasColors();
28+
2729
function logLevelColor(level: LogLevel): string {
2830
switch (level) {
2931
case LogLevel.Debug:
@@ -111,11 +113,17 @@ export class Logger {
111113
}
112114

113115
return (message, data) => {
114-
// you mean you DON'T know ansi escape codes off by heart
115-
// too bad!
116-
console.error(
117-
`\x1b[2m${dateToHMSString()} \x1b[0m${logLevelColor(level)}${logLevelName(level)}:\x1b[0m ${message} \x1b[2m(${this.discriminator})\x1b[0m`,
118-
);
116+
if (HAS_COLORS) {
117+
// you mean you DON'T know ansi escape codes off by heart
118+
// too bad!
119+
console.error(
120+
`\x1b[2m${dateToHMSString()} \x1b[0m${logLevelColor(level)}${logLevelName(level)}:\x1b[0m ${message} \x1b[2m(${this.discriminator})\x1b[0m`,
121+
);
122+
} else {
123+
console.error(
124+
`[${dateToHMSString()}] [${logLevelName(level)}]: ${message} (${this.discriminator})`,
125+
);
126+
}
119127

120128
if (data !== undefined) {
121129
console.group();

0 commit comments

Comments
 (0)