Skip to content
Draft
Changes from all 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
51 changes: 28 additions & 23 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ const ansiCodes = {
magenta: '\x1b[35m',
cyan: '\x1b[36m',
reset: '\x1b[0m',
bold: '\x1b[1m',
} as const;

export const warnPrefix = ansiCodes.yellow + 'WARN' + ansiCodes.reset;
export const infoPrefix = ansiCodes.cyan + 'INFO' + ansiCodes.reset;
export const errorPrefix = ansiCodes.red + 'ERR' + ansiCodes.reset;
export const debugPrefix = ansiCodes.magenta + 'DEBUG' + ansiCodes.reset;
export const titleBold = (msg: string) => ansiCodes.bold + msg + ansiCodes.reset;

export type LogLevel = 'debug' | 'info' | 'warn' | 'error';

export type YogaLogger = Record<LogLevel, (...args: any[]) => void>;
export type YogaLogger = {
debug: (...args: any[]) => void;
info: (...args: any[]) => void;
warn: (...args: any[]) => void;
error: (...args: any[]) => void;
createChild?: (prefix: string) => YogaLogger;
};

const logLevelScores: Record<LogLevel | 'silent', number> = {
debug: 0,
Expand All @@ -29,32 +37,29 @@ const logLevelScores: Record<LogLevel | 'silent', number> = {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};

const consoleLog =
(prefix: string) =>
(...args: Array<any>) =>
console.log(prefix, ...args);

const debugLog = console.debug
? (...args: Array<any>) => console.debug(debugPrefix, ...args)
: consoleLog(debugPrefix);
const infoLog = console.info
? (...args: Array<any>) => console.info(infoPrefix, ...args)
: consoleLog(infoPrefix);
const warnLog = console.warn
? (...args: Array<any>) => console.warn(warnPrefix, ...args)
: consoleLog(warnPrefix);
const errorLog = console.error
? (...args: Array<any>) => console.error(errorPrefix, ...args)
: consoleLog(errorPrefix);
const withPrefix =
(prefix: string, name?: string) =>
(...args: Array<any>) => {
const namePrefix = name ? `[${titleBold(name)}] ` + ' ' : '';
console.log(namePrefix + prefix, ...args);
};

const createLogFunction = (prefix: string, name?: string) => withPrefix(prefix, name);

export const createLogger = (
logLevel: LogLevel | 'silent' = globalThis.process?.env['DEBUG'] === '1' ? 'debug' : 'info',
name?: string,
): YogaLogger => {
const score = logLevelScores[logLevel];
return {
debug: score > logLevelScores.debug ? noop : debugLog,
info: score > logLevelScores.info ? noop : infoLog,
warn: score > logLevelScores.warn ? noop : warnLog,
error: score > logLevelScores.error ? noop : errorLog,

const logger: YogaLogger = {
debug: score > logLevelScores.debug ? noop : createLogFunction(debugPrefix, name),
info: score > logLevelScores.info ? noop : createLogFunction(infoPrefix, name),
warn: score > logLevelScores.warn ? noop : createLogFunction(warnPrefix, name),
error: score > logLevelScores.error ? noop : createLogFunction(errorPrefix, name),
createChild: (childPrefix: string) =>
createLogger(logLevel, String(name ? `${name}]-[${childPrefix}` : '')),
};

return logger;
};