2025-08-30 19:04:35 -03:00
|
|
|
const winston = require('winston');
|
|
|
|
|
const path = require('path');
|
2025-11-19 16:01:37 -03:00
|
|
|
require('winston-daily-rotate-file');
|
2025-09-05 20:27:35 -03:00
|
|
|
const fs = require('fs');
|
2025-08-30 19:04:35 -03:00
|
|
|
|
2025-09-05 20:27:35 -03:00
|
|
|
// verifica se a pasta de logs existe, se não, cria
|
|
|
|
|
const logsDir = path.join(__dirname, '../../logs');
|
|
|
|
|
if (!fs.existsSync(logsDir)) {
|
|
|
|
|
fs.mkdirSync(logsDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 17:03:17 -03:00
|
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
|
const logLevel = process.env.LOG_LEVEL || 'info';
|
|
|
|
|
const logToConsole = process.env.LOG_TO_CONSOLE
|
|
|
|
|
? process.env.LOG_TO_CONSOLE === 'true'
|
|
|
|
|
: !isProduction;
|
|
|
|
|
|
2025-09-05 20:27:35 -03:00
|
|
|
// Configuração do logger com winston
|
2025-08-30 19:04:35 -03:00
|
|
|
const logger = winston.createLogger({
|
2026-02-27 17:03:17 -03:00
|
|
|
level: logLevel,
|
2025-11-19 16:01:37 -03:00
|
|
|
format: winston.format.combine(winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format.errors({ stack: true })),
|
2025-08-30 19:04:35 -03:00
|
|
|
transports: [
|
2025-09-05 20:27:35 -03:00
|
|
|
// Log geral da aplicação
|
2025-11-19 16:01:37 -03:00
|
|
|
new winston.transports.DailyRotateFile({
|
|
|
|
|
filename: path.join(logsDir, 'app-%DATE%.log'),
|
|
|
|
|
datePattern: 'YYYY-MM-DD',
|
|
|
|
|
zippedArchive: true,
|
|
|
|
|
maxSize: '5m',
|
|
|
|
|
maxFiles: '10d',
|
|
|
|
|
format: winston.format.combine(
|
|
|
|
|
winston.format.printf((info) => {
|
|
|
|
|
const { timestamp, level, message, stack, ...meta } = info;
|
|
|
|
|
let logMessage = `${timestamp} [${level}]: ${stack || message}`;
|
|
|
|
|
if (Object.keys(meta).length) {
|
|
|
|
|
logMessage += ` ${JSON.stringify(meta, null, 2)}`;
|
|
|
|
|
}
|
|
|
|
|
return logMessage;
|
|
|
|
|
})
|
|
|
|
|
),
|
2025-09-05 20:27:35 -03:00
|
|
|
}),
|
|
|
|
|
// Log de erros
|
2025-11-19 16:01:37 -03:00
|
|
|
new winston.transports.DailyRotateFile({
|
|
|
|
|
filename: path.join(logsDir, 'error-%DATE%.log'),
|
2025-09-05 20:27:35 -03:00
|
|
|
level: 'error',
|
2025-11-19 16:01:37 -03:00
|
|
|
datePattern: 'YYYY-MM-DD',
|
|
|
|
|
zippedArchive: true,
|
|
|
|
|
maxSize: '5m',
|
|
|
|
|
maxFiles: '10d',
|
|
|
|
|
format: winston.format.combine(
|
|
|
|
|
winston.format.printf((info) => {
|
|
|
|
|
const { timestamp, level, message, stack, ...meta } = info;
|
|
|
|
|
let logMessage = `${timestamp} [${level}]: ${stack || message}`;
|
|
|
|
|
if (Object.keys(meta).length) {
|
|
|
|
|
logMessage += ` ${JSON.stringify(meta, null, 2)}`;
|
|
|
|
|
}
|
|
|
|
|
return logMessage;
|
|
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
})
|
2025-08-30 19:04:35 -03:00
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-27 17:03:17 -03:00
|
|
|
// Log no console (controlado por variavel de ambiente)
|
|
|
|
|
if (logToConsole) {
|
2025-08-30 19:04:35 -03:00
|
|
|
logger.add(new winston.transports.Console({
|
2025-09-05 20:27:35 -03:00
|
|
|
format: winston.format.combine(
|
|
|
|
|
winston.format.colorize(),
|
2025-11-19 16:01:37 -03:00
|
|
|
winston.format.printf((info) => {
|
|
|
|
|
const { timestamp, level, message, stack, ...meta } = info;
|
|
|
|
|
let logMessage = `${timestamp} [${level}]: ${stack || message}`;
|
|
|
|
|
if (Object.keys(meta).length) {
|
|
|
|
|
logMessage += ` ${JSON.stringify(meta, null, 2)}`;
|
|
|
|
|
}
|
|
|
|
|
return logMessage;
|
2025-09-05 20:27:35 -03:00
|
|
|
})
|
|
|
|
|
)
|
2025-08-30 19:04:35 -03:00
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 20:27:35 -03:00
|
|
|
// Funções utilitárias
|
|
|
|
|
const logError = (error, context = '') => {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
logger.error(`${context} - ${error.message}`, { stack: error.stack });
|
|
|
|
|
} else {
|
|
|
|
|
logger.error(`${context} - ${error}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const logInfo = (message, meta = {}) => {
|
|
|
|
|
logger.info(message, meta);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const logWarning = (message, meta = {}) => {
|
|
|
|
|
logger.warn(message, meta);
|
2025-08-30 19:04:35 -03:00
|
|
|
};
|
|
|
|
|
|
2025-09-05 20:27:35 -03:00
|
|
|
// Log de sincronização específico
|
|
|
|
|
const logSync = (service, count, type) => {
|
|
|
|
|
logger.info(`SYNC: ${service} - ${count} ${type} sincronizados`, {
|
|
|
|
|
service,
|
|
|
|
|
count,
|
|
|
|
|
type
|
|
|
|
|
});
|
2025-08-30 19:04:35 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2025-09-05 20:27:35 -03:00
|
|
|
logger,
|
2025-08-30 19:04:35 -03:00
|
|
|
logError,
|
|
|
|
|
logInfo,
|
2025-09-05 20:27:35 -03:00
|
|
|
logWarning,
|
2025-11-19 16:01:37 -03:00
|
|
|
logSync,
|
2026-02-27 17:03:17 -03:00
|
|
|
};
|