Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimize logger code readability #71

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import chalk from "chalk";
import chalk, { Chalk } from "chalk";
import { format, createLogger, transports } from "winston";

import { hasColor } from "./helpers.js";
JackHamer09 marked this conversation as resolved.
Show resolved Hide resolved

export const errorSymbol = "β“˜"; // used in ../test-utils/matchers.ts to check for errors in console output

const logLevelFormatter: Record<string, Chalk | ((msg: string) => string)> = {
error: (msg: string) => chalk.redBright(`${errorSymbol} ${msg}`),
warn: chalk.yellowBright,
info: chalk.magentaBright,
debug: chalk.gray,
};

const styleLogs = format.printf((info) => {
if (hasColor(info.message) || info.noFormat) {
return info.message;
}
if (info.level === "error") {
return chalk.redBright(`${errorSymbol} ${info.message}`);
} else if (info.level === "warn") {
return chalk.yellowBright(info.message);
} else if (info.level === "info") {
return chalk.magentaBright(info.message);
} else if (info.level === "debug") {
return chalk.gray(info.message);
}
return info.message;

const colorize = logLevelFormatter[info.level];
return colorize ? colorize(info.message) : info.message;
});

const logger = createLogger({
Expand Down