2023-08-11 14:32:44 +01:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Msg *zap.Logger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Create a custom configuration with a human-readable "Console" encoder
|
|
|
|
config := zap.NewDevelopmentConfig()
|
|
|
|
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder // Adds color to log levels
|
|
|
|
|
|
|
|
// Determine the log level based on the runtime mode
|
|
|
|
mode := os.Getenv("RUNTIME_MODE")
|
|
|
|
logLevel := zapcore.DebugLevel
|
|
|
|
if mode == "production" {
|
|
|
|
logLevel = zapcore.InfoLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the log level
|
|
|
|
config.Level = zap.NewAtomicLevelAt(logLevel)
|
|
|
|
|
|
|
|
Msg, err = config.Build() // Potential source of the error
|
|
|
|
if err != nil {
|
2023-08-11 17:51:15 +01:00
|
|
|
panic("Failed to initialize logger" + err.Error())
|
2023-08-11 14:32:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log the selected log level (optional, can be helpful for debugging)
|
2023-08-11 17:51:15 +01:00
|
|
|
Msg.Info("", zap.String("Log level", logLevel.String()))
|
2023-08-11 14:32:44 +01:00
|
|
|
}
|