From db658f5a68777ee19b7b710cb683b7bc6a1481dc Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Wed, 11 Feb 2026 10:48:35 +0300 Subject: [PATCH] feat(logs): change logs to structured json --- main.go | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 75a597a..1d06005 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "flag" "fmt" "io" - "log" + "log/slog" "net/http" "net/url" "os" @@ -111,13 +111,17 @@ func main() { configPath := flag.String("config", "config.toml", "path to config file") flag.Parse() + slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil))) + cfg, interval, err := loadConfig(*configPath) if err != nil { - log.Fatalf("config error: %v", err) + slog.Error("config load failed", "error", err) + os.Exit(1) } if err := os.MkdirAll(cfg.CacheDir, 0o755); err != nil { - log.Fatalf("cache dir: %v", err) + slog.Error("cache dir create failed", "error", err) + os.Exit(1) } agg := NewAggregator() @@ -133,7 +137,7 @@ func main() { for _, source := range cfg.Sources { cached, err := loadCachedLinks(cfg.CacheDir, source) if err != nil { - log.Printf("load cache for %s: %v", source, err) + slog.Error("load cache failed", "source", source, "error", err) } if len(cached) > 0 { agg.Update(source, cached) @@ -149,7 +153,7 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) { - log.Printf("request /list from %s [%s %s]", r.RemoteAddr, r.Method, r.UserAgent()) + slog.Info("request", "path", "/list", "remote", r.RemoteAddr, "method", r.Method, "user_agent", r.UserAgent()) links := agg.List() w.Header().Set("Content-Type", "text/plain; charset=utf-8") for i, link := range links { @@ -158,7 +162,7 @@ func main() { } _, _ = w.Write([]byte(link)) } - log.Printf("response /list to %s: %d links", r.RemoteAddr, len(links)) + slog.Info("response", "path", "/list", "remote", r.RemoteAddr, "links", len(links)) }) server := &http.Server{ @@ -173,15 +177,16 @@ func main() { <-ctx.Done() shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - log.Printf("server shutdown") + slog.Info("server shutting down") if err := server.Shutdown(shutdownCtx); err != nil { - log.Printf("server shutdown error: %v", err) + slog.Error("server shutdown failed", "error", err) } }() - log.Printf("listening on :%d", cfg.Port) + slog.Info("listening", "port", cfg.Port) if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { - log.Fatalf("server error: %v", err) + slog.Error("server listen failed", "error", err) + os.Exit(1) } } @@ -194,7 +199,7 @@ func pollSource(ctx context.Context, source string, interval time.Duration, cach for { select { case <-ctx.Done(): - log.Printf("poller shutdown") + slog.Info("poller stopped", "source", source) return case <-ticker.C: runOnce(ctx, source, cacheDir, agg, client) @@ -205,22 +210,22 @@ func pollSource(ctx context.Context, source string, interval time.Duration, cach func runOnce(ctx context.Context, source string, cacheDir string, agg *Aggregator, client *http.Client) { links, err := fetchSource(ctx, source, client) if err != nil { - log.Printf("poll %s: failed - %v", source, err) + slog.Error("poll failed", "source", source, "error", err) return } if len(links) == 0 { - log.Printf("poll %s: success - 0 links", source) + slog.Info("poll success", "source", source, "links", 0) agg.Update(source, nil) if err := writeCache(cacheDir, source, nil); err != nil { - log.Printf("write cache %s: %v", source, err) + slog.Error("write cache failed", "source", source, "error", err) } return } - log.Printf("poll %s: success - %d links", source, len(links)) + slog.Info("poll success", "source", source, "links", len(links)) agg.Update(source, links) if err := writeCache(cacheDir, source, links); err != nil { - log.Printf("write cache %s: %v", source, err) + slog.Error("write cache failed", "source", source, "error", err) } } @@ -244,11 +249,11 @@ func fetchSource(ctx context.Context, source string, client *http.Client) ([]str if resp.StatusCode < 200 || resp.StatusCode >= 300 { return nil, fmt.Errorf("unexpected status: %s", resp.Status) } - log.Printf("fetch %s: HTTP %d", source, resp.StatusCode) body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("read body: %w", err) } + slog.Info("fetch complete", "source", source, "status", resp.StatusCode, "bytes", len(body)) return normalizeLinks(string(body)), nil case "file": path := u.Path @@ -259,7 +264,7 @@ func fetchSource(ctx context.Context, source string, client *http.Client) ([]str if err != nil { return nil, fmt.Errorf("read file: %w", err) } - log.Printf("fetch %s: file read %d bytes", source, len(data)) + slog.Info("fetch complete", "source", source, "bytes", len(data)) return normalizeLinks(string(data)), nil default: return nil, fmt.Errorf("unsupported source scheme: %s", u.Scheme)