2019-10-05 21:53:22 +05:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Logger zerolog.Logger
|
|
|
|
SuperVerbosive bool
|
|
|
|
)
|
|
|
|
|
|
|
|
// Initialize initializes zerolog with proper formatting and log level.
|
2021-11-20 23:06:40 +05:00
|
|
|
// nolint:forbidigo
|
2019-10-05 21:53:22 +05:00
|
|
|
func Initialize() {
|
|
|
|
// Check environment for logger level.
|
|
|
|
// Defaulting to INFO.
|
|
|
|
loggerLevel, loggerLevelFound := os.LookupEnv("LOGGER_LEVEL")
|
|
|
|
if loggerLevelFound {
|
|
|
|
fmt.Println("Setting logger level to:", loggerLevel)
|
2019-10-16 23:32:21 +05:00
|
|
|
|
2019-10-05 21:53:22 +05:00
|
|
|
switch strings.ToUpper(loggerLevel) {
|
|
|
|
case "DEBUG":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
case "INFO":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
case "WARN":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
|
|
|
case "ERROR":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
|
|
|
|
case "FATAL":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.FatalLevel)
|
|
|
|
default:
|
|
|
|
fmt.Println("Invalid logger level passed:", loggerLevel)
|
2019-10-16 23:32:21 +05:00
|
|
|
fmt.Println("Forcing INFO")
|
2019-10-05 21:53:22 +05:00
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("Setting logger level to: info")
|
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
}
|
|
|
|
|
2021-11-20 23:06:40 +05:00
|
|
|
// nolint:exhaustivestruct
|
2019-10-05 21:53:22 +05:00
|
|
|
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
|
2021-11-20 23:06:40 +05:00
|
|
|
output.FormatLevel = func(lvlRaw interface{}) string {
|
2019-10-05 21:53:22 +05:00
|
|
|
var v string
|
2019-10-16 23:32:21 +05:00
|
|
|
|
2021-11-20 23:06:40 +05:00
|
|
|
if lvl, ok := lvlRaw.(string); ok {
|
|
|
|
lvl = strings.ToUpper(lvl)
|
|
|
|
switch lvl {
|
2019-10-05 21:53:22 +05:00
|
|
|
case "DEBUG":
|
2021-11-20 23:06:40 +05:00
|
|
|
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl)
|
2019-10-05 21:53:22 +05:00
|
|
|
case "ERROR":
|
2021-11-20 23:06:40 +05:00
|
|
|
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl)
|
2019-10-05 21:53:22 +05:00
|
|
|
case "FATAL":
|
2021-11-20 23:06:40 +05:00
|
|
|
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl)
|
2019-10-05 21:53:22 +05:00
|
|
|
case "INFO":
|
2021-11-20 23:06:40 +05:00
|
|
|
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl)
|
2019-10-05 21:53:22 +05:00
|
|
|
case "PANIC":
|
2021-11-20 23:06:40 +05:00
|
|
|
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl)
|
2019-10-05 21:53:22 +05:00
|
|
|
case "WARN":
|
2021-11-20 23:06:40 +05:00
|
|
|
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl)
|
2019-10-05 21:53:22 +05:00
|
|
|
default:
|
2021-11-20 23:06:40 +05:00
|
|
|
v = lvl
|
2019-10-05 21:53:22 +05:00
|
|
|
}
|
|
|
|
}
|
2019-10-16 23:32:21 +05:00
|
|
|
|
2019-10-05 21:53:22 +05:00
|
|
|
return fmt.Sprintf("| %s |", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger = zerolog.New(output).With().Timestamp().Logger()
|
|
|
|
}
|