Files
feishin/src/logger.ts
T
jeffvli 3b155cc6e8 Remove throw from log function
- Typescript cannot determine if a function throws an error
- Does not work as a type guard when using ts-rest
2023-12-13 18:19:57 -08:00

38 lines
1.0 KiB
TypeScript

import dayjs from 'dayjs';
const reset = '\x1b[0m';
const baseLog = (errorType: 'error' | 'info' | 'success' | 'warn') => {
let logString = '';
switch (errorType) {
case 'error':
logString = '\x1b[31m[ERROR] ';
break;
case 'info':
logString = '\x1b[34m[INFO] ';
break;
case 'success':
logString = '\x1b[32m[SUCCESS] ';
break;
case 'warn':
logString = '\x1b[33m[WARNING] ';
break;
default:
logString = '\x1b[34m[INFO] ';
break;
}
return (text: string, options?: { context?: Record<string, any>; toast?: boolean }): void => {
// const { toast } = options || {};
const now = dayjs().toISOString();
console.log(`${logString}${now}${text}${JSON.stringify(options?.context)}${reset}`);
};
};
export const fsLog = {
error: baseLog('error'),
info: baseLog('info'),
success: baseLog('success'),
warn: baseLog('warn'),
};