diff options
| author | Bernhard Guillon <Bernhard.Guillon@begu.org> | 2026-07-06 22:16:37 +0200 |
|---|---|---|
| committer | Bernhard Guillon <Bernhard.Guillon@begu.org> | 2026-07-06 22:16:37 +0200 |
| commit | 913a974eca2522ba7c6d27066481bb2c6635db53 (patch) | |
| tree | f0cfc1ee173a762662b586fca5db9db422592093 /log.go | |
| parent | 5f2ba9c3d37a1c8658aea411188a6763e982b1ab (diff) | |
| download | sourcewatch-913a974eca2522ba7c6d27066481bb2c6635db53.tar.gz sourcewatch-913a974eca2522ba7c6d27066481bb2c6635db53.zip | |
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
Diffstat (limited to 'log.go')
| -rw-r--r-- | log.go | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -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...) + } +} |
