import path from 'path'
import { createLogger, format, transports } from 'winston'

const { combine, splat, timestamp, printf } = format

const myFormat = printf(({ level, message, timestamp, ...metadata }) => {
  let msg = `${timestamp} [${level}] : ${message}`
  // if (metadata) {
  //   msg += JSON.stringify(metadata)
  // }
  return msg
})

const logger = createLogger({
  level: 'debug',
  format: combine(format.colorize(), splat(), timestamp(), myFormat),
  transports: [
    new transports.Console({ level: 'info' }),

    new transports.File({ filename: path.join(__dirname, '../log'), level: 'debug' }),
  ],
})

export const morganStream = {
  write: (text) => {
    logger.http(text)
  },
}

export default logger
