Skip to content

Commit

Permalink
feat: add logger object
Browse files Browse the repository at this point in the history
  • Loading branch information
madkarmaa committed Nov 17, 2024
1 parent 7ac9e5a commit c4de587
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const levels = {
debug: {
message: 'DEBUG',
styles: ['color: #0f0'].join(';')
},
info: {
message: 'INFO',
styles: ['color: #0ff'].join(';')
},
warn: {
message: 'WARN',
styles: ['color: #ff0'].join(';')
},
error: {
message: 'ERROR',
styles: ['color: #f00'].join(';')
},
critical: {
message: 'CRITICAL',
styles: ['color: #f00', 'font-weight: 600'].join(';')
}
} as const;

type LogLevel = keyof typeof levels;

const _log = (level: LogLevel, ...args: any[]) =>
console.log(`[%c${levels[level].message}%c]`, levels[level].styles, '', ...args);

const logger = Object.keys(levels).reduce(
(acc, level) => {
acc[level as LogLevel] = (...args: any[]) => _log(level as LogLevel, ...args);
return acc;
},
{} as Record<LogLevel, (...args: any[]) => void>
);

export default logger;

0 comments on commit c4de587

Please sign in to comment.