Từ Go 1.21, stdlib cung cấp package log/slog cho structured logging với cú pháp slog.Info("user created", "id", user.ID, "email", user.Email), output ra JSON có cấu trúc rõ ràng thay vì chuỗi text khó parse.
- Slog hỗ trợ các mức log chuẩn (Debug, Info, Warn, Error) và cho phép tùy chỉnh handler để thay đổi format hoặc destination output.
- Trước khi có slog, cộng đồng Go dùng zap (Uber, tối ưu hiệu năng cao nhất) hoặc zerolog (zero allocation) cho structured logging; cả hai vẫn phổ biến trong production vì có thêm nhiều tính năng nâng cao.
- Ưu điểm lớn nhất của structured logging là log dạng key-value pairs dễ dàng được index và query bởi các log aggregator như ELK Stack, Datadog, hay Grafana Loki.
Since Go 1.21, the stdlib provides log/slog for structured logging: slog.Info("user created", "id", user.ID, "email", user.Email) outputs clean JSON instead of hard-to-parse text.
- Slog supports standard log levels (Debug, Info, Warn, Error) and allows custom handlers to change format or output destination.
- Before slog, the community used zap (Uber, highest performance) or zerolog (zero allocation) — both remain popular in production for their advanced features.
- The key advantage of structured logging is that key-value pairs are easily indexed and queried by log aggregators like ELK Stack, Datadog, or Grafana Loki.