package logger

import (
	"os"

	"github.com/sirupsen/logrus"
)

var (
	log *logrus.Logger
)

func Init() {
	log = logrus.New()
	log.SetOutput(os.Stdout)
	log.SetFormatter(&logrus.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
	})
	log.SetLevel(logrus.InfoLevel)
}

func Info(msg string, fields ...interface{}) {
	if len(fields) > 0 {
		log.WithFields(logrus.Fields{"message": msg}).Info()
	} else {
		log.Info(msg)
	}
}

func Infof(format string, args ...interface{}) {
	log.Infof(format, args...)
}

func Debug(msg string, fields ...interface{}) {
	if len(fields) > 0 {
		log.WithFields(logrus.Fields{"message": msg}).Debug()
	} else {
		log.Debug(msg)
	}
}

func Debugf(format string, args ...interface{}) {
	log.Debugf(format, args...)
}

func Warn(msg string, fields ...interface{}) {
	if len(fields) > 0 {
		log.WithFields(logrus.Fields{"message": msg}).Warn()
	} else {
		log.Warn(msg)
	}
}

func Warnf(format string, args ...interface{}) {
	log.Warnf(format, args...)
}

func Error(msg string, fields ...interface{}) {
	if len(fields) > 0 {
		log.WithFields(logrus.Fields{"message": msg}).Error()
	} else {
		log.Error(msg)
	}
}

func Errorf(format string, args ...interface{}) {
	log.Errorf(format, args...)
}

func Fatal(msg string, fields ...interface{}) {
	if len(fields) > 0 {
		log.WithFields(logrus.Fields{"message": msg}).Fatal()
	} else {
		log.Fatal(msg)
	}
}

func Fatalf(format string, args ...interface{}) {
	log.Fatalf(format, args...)
}

func WithField(key string, value interface{}) *logrus.Entry {
	return log.WithField(key, value)
}
