2023-07-18 14:09:28 +01:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2023-07-21 12:12:16 +01:00
|
|
|
|
2024-03-25 11:26:07 +00:00
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
2023-07-18 14:09:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var Msg *zap.Logger
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Initialises the logger
|
2023-07-18 14:09:28 +01:00
|
|
|
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
|
|
|
|
|
2023-07-21 12:12:16 +01:00
|
|
|
// Determine the log level based on the runtime mode
|
|
|
|
logLevel := zapcore.DebugLevel
|
|
|
|
if helpers.Runtime == "production" {
|
|
|
|
logLevel = zapcore.InfoLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the log level
|
|
|
|
config.Level = zap.NewAtomicLevelAt(logLevel)
|
|
|
|
|
|
|
|
Msg, err = config.Build() // Potential source of the error
|
2023-07-18 14:09:28 +01:00
|
|
|
if err != nil {
|
|
|
|
panic("Failed to initialize logger: " + err.Error())
|
|
|
|
}
|
2023-07-21 12:12:16 +01:00
|
|
|
|
|
|
|
// Log the selected log level (optional, can be helpful for debugging)
|
|
|
|
Msg.Info("Log level set to: " + logLevel.String())
|
2023-07-18 14:09:28 +01:00
|
|
|
}
|