UNPKG

ffmpeg-toolkit

Version:

A modern FFmpeg toolkit for Node.js

155 lines 4.79 kB
import chalk from 'chalk'; import dayjs from 'dayjs'; import timezone from 'dayjs/plugin/timezone.js'; import utc from 'dayjs/plugin/utc.js'; import winston from 'winston'; import 'winston-daily-rotate-file'; dayjs.extend(utc); dayjs.extend(timezone); const VN_TZ = 'Asia/Ho_Chi_Minh'; function formatVNTime(timestamp) { return dayjs(timestamp) .tz(VN_TZ) .format('DD/MM/YYYY HH:mm:ss'); } const customLevels = { levels: { error: 0, fatal: 1, warn: 2, success: 3, info: 4, http: 5, verbose: 6, debug: 7, silly: 8, trace: 9, }, colors: { error: 'red', fatal: 'bgRed', warn: 'yellow', success: 'green', info: 'blue', http: 'cyan', verbose: 'gray', debug: 'magenta', silly: 'white', trace: 'bgMagenta', }, }; export class LoggerService { constructor(path) { this.icons = { info: 'ℹ️', warn: '⚠️', error: '❌', debug: '🔍', success: '✅', fatal: '💀', silly: '🤪', verbose: '🗣️', http: '🌐', trace: '🧵', }; this.colors = { info: chalk.rgb(0, 255, 255), warn: chalk.rgb(255, 140, 0), error: chalk.rgb(255, 36, 0), debug: chalk.rgb(255, 0, 255), success: chalk.rgb(0, 255, 127), fatal: chalk.rgb(255, 0, 64), silly: chalk.rgb(255, 255, 0), verbose: chalk.rgb(0, 191, 255), http: chalk.rgb(57, 255, 20), trace: chalk.rgb(255, 20, 147), }; this.logger = winston.createLogger({ levels: customLevels.levels, level: 'trace', format: winston.format.combine(winston.format.timestamp(), winston.format.json()), transports: [ new winston.transports.Console({ format: winston.format.printf(({ level, message, timestamp }) => { const logLevel = level; const icon = this.icons[logLevel] || ''; const color = this.colors[logLevel] || ((txt) => txt); const levelText = (logLevel === 'success' ? 'SUCCESS' : level.toUpperCase()).padEnd(7); return `${color(`[${formatVNTime(timestamp)}]`)} ${icon} ${color(levelText)} ${color(message)}`; }), }), new winston.transports.DailyRotateFile({ dirname: path, filename: 'application-%DATE%.log', datePattern: 'YYYY-MM-DD', maxSize: '20m', maxFiles: '14d', format: winston.format.printf(({ level, message, timestamp }) => { const logLevel = level; const icon = this.icons[logLevel] || ''; const levelText = (logLevel === 'success' ? 'SUCCESS' : level.toUpperCase()).padEnd(7); return `[${formatVNTime(timestamp)}] ${icon} ${levelText} ${message}`; }), }), ], }); winston.addColors(customLevels.colors); } static getInstance(path) { if (!LoggerService.instance) { LoggerService.instance = new LoggerService(path); } return LoggerService.instance; } formatArgs(args) { return args .map((arg) => { if (typeof arg === 'string') return arg; if (arg instanceof Error) return arg.stack || arg.message; try { return JSON.stringify(arg); } catch { return String(arg); } }) .join(' '); } log(level, ...args) { this.logger.log(level, this.formatArgs(args)); } info(...args) { this.log('info', ...args); } warn(...args) { this.log('warn', ...args); } error(...args) { this.log('error', ...args); } debug(...args) { this.log('debug', ...args); } success(...args) { this.log('success', ...args); } trace(...args) { this.log('trace', ...args); } fatal(...args) { this.log('fatal', ...args); } silly(...args) { this.log('silly', ...args); } verbose(...args) { this.log('verbose', ...args); } http(...args) { this.log('http', ...args); } } export const logger = (path) => LoggerService.getInstance(path); //# sourceMappingURL=logger.js.map