diff options
| author | dev | 2026-06-26 14:14:52 +0200 |
|---|---|---|
| committer | dev | 2026-06-26 14:14:52 +0200 |
| commit | 06536f57b1fdc76212da6b85fbc9287cc4f0de70 (patch) | |
| tree | 755e49a396091f953c428cb59a4cf56f03463267 /src/loglevel.go | |
| parent | 13992fedaa0beaf93f6214993c95e685d249638f (diff) | |
| download | hnimdbbot-06536f57b1fdc76212da6b85fbc9287cc4f0de70.tar.gz | |
feat: add three-level logging with per-request debug output
- New --log-level flag: debug (default info), info, silent
debug: every API request logged (method, URL, status, duration)
info: normal events (batch progress, entry counts, summaries)
silent: only warnings and fatal errors
- Replaced all log.Printf/Fatalf calls with level-gated helpers
- API request timing added to queryWikiArticle, queryWikidataBatch, downloadFile
- Retries and backoff logged in debug mode
Diffstat (limited to 'src/loglevel.go')
| -rw-r--r-- | src/loglevel.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/loglevel.go b/src/loglevel.go new file mode 100644 index 0000000..6f0e8aa --- /dev/null +++ b/src/loglevel.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "log" + "os" +) + +// LogLevel controls verbosity. +type LogLevel int + +const ( + LogDebug LogLevel = iota + LogInfo + LogSilent +) + +// logLevel is set in main() from the --log-level flag. +var logLevel LogLevel = LogInfo + +// setLogLevel parses the flag value. +func setLogLevel(val string) { + switch val { + case "debug": + logLevel = LogDebug + case "info": + logLevel = LogInfo + case "silent": + logLevel = LogSilent + default: + log.Fatalf("invalid --log-level %q (want debug, info, or silent)", val) + } +} + +func logDebug(format string, v ...interface{}) { + if logLevel == LogDebug { + log.Printf(format, v...) + } +} + +func logInfo(format string, v ...interface{}) { + if logLevel <= LogInfo { + log.Printf(format, v...) + } +} + +func logWarn(format string, v ...interface{}) { + log.Printf(format, v...) +} + +func logFatal(format string, v ...interface{}) { + log.Fatalf(format, v...) +} + +// logHTTPRequest logs an HTTP request in debug mode. +func logHTTPRequest(method, url string, statusCode int, duration float64) { + if logLevel == LogDebug { + log.Printf("HTTP %s %s -> %d (%.2fs)", method, url, statusCode, duration) + } +} + +// logHTTPRetry logs a retry attempt in debug mode. +func logHTTPRetry(attempt, maxAttempts int, backoffSecs int, target string) { + if logLevel == LogDebug { + log.Printf("retry %d/%d for %s after %ds backoff", attempt, maxAttempts, target, backoffSecs) + } +} + +// logFatalErr is like logFatal but takes a message and error separately. +func logFatalErr(msg string, err error) { + log.Fatalf("%s: %v", msg, err) +} + +// Fatal with two args like the original log.Fatal(err, msg) pattern. +func logFatalTwoArgs(err error, msg string) { + fmt.Fprintln(os.Stderr, err) + log.Fatalf("%s", msg) +} |
