From 06536f57b1fdc76212da6b85fbc9287cc4f0de70 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 26 Jun 2026 14:14:52 +0200 Subject: 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 --- src/loglevel.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/loglevel.go (limited to 'src/loglevel.go') 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) +} -- cgit v1.2.3