const winston = require('winston');

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf, numsession } = format;

const myFormatFile = printf(({ level, message, label, timestamp, numsession }) => {
  return `${timestamp} ${level} [${label}] SID[${numsession}] ${message}`;
});

const myFormatConsole = printf(({ level, message, label, timestamp, numsession }) => {
  return `${timestamp} ${level} [${label}] SID[${numsession}] ${message}`;
});


const DailyRotateFile = require('winston-daily-rotate-file');


const logger = winston.createLogger({
  //level: 'info',
  format: combine(
    //label({ label: '' }),
    timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
    myFormatFile
  ),
  defaultMeta: { service: 'user-service' },
  transports: [
    new DailyRotateFile({ filename: 'logs/adhesion/log_adhesion.log' }),
  ],
  exceptionHandlers: [
    new DailyRotateFile({ filename: 'logs/adhesion/log_adhesion.log' }),
    new winston.transports.Console()
  ],
  rejectionHandlers: [
    new DailyRotateFile({ filename: 'logs/adhesion/log_adhesion.log' }),
    new winston.transports.Console()
  ]
});



//pour afficher également les exceptions dans la console
if (process.env.ENV !== 'production' ) {
  logger.add(new winston.transports.Console(
    {
      format: combine(
        //label({ label: '' }),
        timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
        myFormatConsole
      ),
    handleExceptions: true
    })
  );
}

/////////////////////////////////////////////////
// EXPORT DE MODULE POUR QU'IL SOIT LU DANS LES AUTRES FICHIERS JS
/////////////////////////////////////////////////
module.exports = logger;