utils.js 799 Bytes
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const logger = winston.createLogger({
  format: winston.format.json(),
  transports: [
    new DailyRotateFile({
      filename: `logs/payapi-%DATE%.log`,
      datePattern: 'YYYY-MM-DD-HH',
      zippedArchive: true,
      maxSize: '20m',
      maxFiles: '14d'
    })
  ]
});

if (process.env.NODE_ENV !== 'production') {
  logger.add(
    new winston.transports.Console({
      format: winston.format.simple()
    })
  );
}

exports.formatTime = (time, onlyDate) => {
  const date = `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDay()}`;

  if (onlyDate) {
    return date;
  }

  return date + ` ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
};

exports.logger = logger;