Refactor log disable check

This commit is contained in:
Theodor S. Midtlien
2026-06-02 11:07:27 +02:00
parent 97b8c53dff
commit 471e2f98d7
+13 -15
View File
@@ -60,15 +60,7 @@ func InitLogger(logger *log.Logger, logLevel string, logs ...string) error {
case "": case "":
logger.Warnf("empty log path received: %#v", logPath) logger.Warnf("empty log path received: %#v", logPath)
default: default:
conflict, configPath := FindFirstLogrotateConflict() writer, err := setupLogFile(logPath, isRotationDisabled(logger))
if conflict {
logger.Warnf("log rotation conflict detected in: %#v, rotation is disabled", configPath)
}
rotationDisabled := isRotationDisabled()
if rotationDisabled {
logger.Warn("log rotation is disabled by env flag")
}
writer, err := setupLogFile(logPath, conflict || rotationDisabled)
if err != nil { if err != nil {
logger.Errorf("failed setting up log file: %s, %s", logPath, err) logger.Errorf("failed setting up log file: %s, %s", logPath, err)
return err return err
@@ -108,13 +100,19 @@ func FindFirstLogPath(logs []string) string {
return "" return ""
} }
func isRotationDisabled() bool { func isRotationDisabled(logger *log.Logger) bool {
v, ok := os.LookupEnv("NB_LOG_DISABLE_ROTATION") v, _ := os.LookupEnv("NB_LOG_DISABLE_ROTATION")
if !ok {
return false
}
disabled, _ := strconv.ParseBool(v) disabled, _ := strconv.ParseBool(v)
return disabled if disabled {
logger.Warnf("log rotation is disabled by env flag")
return true
}
conflict, configPath := FindFirstLogrotateConflict()
if conflict {
logger.Warnf("log rotation conflict detected in: %#v, rotation is disabled", configPath)
return true
}
return false
} }
func setupLogFile(logPath string, disableRotation bool) (io.Writer, error) { func setupLogFile(logPath string, disableRotation bool) (io.Writer, error) {