From 913a974eca2522ba7c6d27066481bb2c6635db53 Mon Sep 17 00:00:00 2001 From: Bernhard Guillon Date: Mon, 6 Jul 2026 22:16:37 +0200 Subject: Add log level system and sync logging - log.go: log level system (DEBUG, INFO, WARN, ERROR, NONE) - CLI: -log-level flag (default info), -verbose as shorthand for debug - Watcher: logs sync/remove actions at INFO level - Watcher: logs events at DEBUG level - Errors: structured error messages (DISK FULL, PERMISSION DENIED, etc.) - Updated README with -log-level flag --- log.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 log.go (limited to 'log.go') diff --git a/log.go b/log.go new file mode 100644 index 0000000..7d5fa1b --- /dev/null +++ b/log.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "os" + "strings" +) + +type LogLevel int + +const ( + LevelDebug LogLevel = iota + LevelInfo + LevelWarn + LevelError + LevelNone +) + +var logLevelNames = map[LogLevel]string{ + LevelDebug: "DEBUG", + LevelInfo: "INFO", + LevelWarn: "WARN", + LevelError: "ERROR", + LevelNone: "NONE", +} + +var currentLevel = LevelInfo + +func SetLogLevel(level LogLevel) { + currentLevel = level +} + +func ParseLogLevel(s string) (LogLevel, error) { + switch strings.ToUpper(s) { + case "DEBUG": + return LevelDebug, nil + case "INFO": + return LevelInfo, nil + case "WARN": + return LevelWarn, nil + case "ERROR": + return LevelError, nil + case "NONE": + return LevelNone, nil + default: + return LevelInfo, fmt.Errorf("unknown log level: %s (valid: debug, info, warn, error, none)", s) + } +} + +func logDebug(format string, args ...interface{}) { + if currentLevel <= LevelDebug { + fmt.Fprintf(os.Stderr, "DEBUG: "+format+"\n", args...) + } +} + +func logInfo(format string, args ...interface{}) { + if currentLevel <= LevelInfo { + fmt.Fprintf(os.Stderr, format+"\n", args...) + } +} + +func logWarn(format string, args ...interface{}) { + if currentLevel <= LevelWarn { + fmt.Fprintf(os.Stderr, "WARN: "+format+"\n", args...) + } +} + +func logErrorf(format string, args ...interface{}) { + if currentLevel <= LevelError { + fmt.Fprintf(os.Stderr, "ERROR: "+format+"\n", args...) + } +} -- cgit v1.2.3