const winston = require('winston');
require('winston-daily-rotate-file');
const config = require('../config/env');

const rotateTransport = (level, filename) => new winston.transports.DailyRotateFile({
  level,
  filename: `logs/${filename}-%DATE%.log`,
  datePattern: 'YYYY-MM-DD',
  zippedArchive: true,
  maxSize: '20m',
  maxFiles: '30d'
});

const logger = winston.createLogger({
  level: config.nodeEnv === 'production' ? 'info' : 'debug',
  format: winston.format.combine(
    winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    winston.format.errors({ stack: true }),
    winston.format.splat(),
    winston.format.json()
  ),
  defaultMeta: { service: 'tracking-leaders-api' },
  transports: [
    rotateTransport('error', 'error'),
    rotateTransport('info',  'combined')
  ]
});

// In Entwicklung auch auf Konsole ausgeben
if (config.nodeEnv !== 'production') {
  logger.add(new winston.transports.Console({
    format: winston.format.combine(
      winston.format.colorize(),
      winston.format.simple()
    )
  }));
}

module.exports = logger;
