- Добавлен модуль logger.js с настройками winston для логирования в файлы и консоль - Заменены все console.log/warn/error в bot.js на вызовы logger.info/warn/error - Добавлена зависимость winston в package.json - Логи теперь структурированы, с таймстампами и цветовой подсветкой в консоли - Улучшена читаемость и удобство отладки приложения
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
const winston = require('winston');
|
||
const path = require('path');
|
||
|
||
// __dirname уже доступен в CommonJS, нет нужды использовать fileURLToPath
|
||
|
||
// Формат для файла (подробный JSON)
|
||
const fileFormat = winston.format.combine(
|
||
winston.format.timestamp(),
|
||
winston.format.json()
|
||
);
|
||
|
||
// Формат для консоли (краткий, с цветами)
|
||
const consoleFormat = winston.format.combine(
|
||
winston.format.colorize(),
|
||
winston.format.timestamp({ format: 'HH:mm:ss' }),
|
||
winston.format.printf(({ level, message, timestamp }) => {
|
||
return `${timestamp} ${level}: ${message}`;
|
||
})
|
||
);
|
||
|
||
const logger = winston.createLogger({
|
||
level: 'info',
|
||
transports: [
|
||
// Логирование ошибок в отдельный файл
|
||
new winston.transports.File({
|
||
filename: path.join(__dirname, '../logs/error.log'),
|
||
level: 'error',
|
||
format: fileFormat
|
||
}),
|
||
// Общий лог файл
|
||
new winston.transports.File({
|
||
filename: path.join(__dirname, '../logs/combined.log'),
|
||
format: fileFormat
|
||
}),
|
||
// Консольный вывод
|
||
new winston.transports.Console({
|
||
format: consoleFormat
|
||
})
|
||
]
|
||
});
|
||
|
||
module.exports = logger;
|