import { createLogger, format, transports } from 'winston'
const { combine, timestamp, printf } = format
import * as DailyRotateFile from 'winston-daily-rotate-file'
import config from '../config'

export const fatal = createLogger({
  format: combine(
    timestamp(),
    format.printf((info) => `${info.timestamp} ${info.level}: ${info.stack}`)
  ),
  transports: [
    new transports.Console(),
    new DailyRotateFile({
      filename: '%DATE%.fatal.log',
      level: 'error',
      ...config.LOGGER
    })
  ]
})

export const error = createLogger({
  format: combine(
    timestamp(),
    printf(({ timestamp, requestId, detail }) => {
      const { url, method } = detail
      return `${timestamp} ${requestId} ${method} ${url}: ${JSON.stringify(detail)}`
    })
  ),
  transports: [
    new transports.Console(),
    new DailyRotateFile({
      filename: '%DATE%.error.log',
      level: 'error',
      ...config.LOGGER
    })
  ]
})

export const info = createLogger({
  format: combine(
    timestamp(),
    printf(({ timestamp, data }) => {
      return `${timestamp} ${JSON.stringify(data)}`
    })
  ),
  transports: [
    new transports.Console(),
    new DailyRotateFile({
      filename: '%DATE%.log',
      ...config.LOGGER
    })
  ]
})
