Skip to content

Go

Add logging to Go apps

Terminal window
go get github.com/karloscodes/lognorth-sdk-go
package main
import lognorth "github.com/karloscodes/lognorth-sdk-go"
func main() {
lognorth.Config("https://logs.yoursite.com", "your-api-key")
lognorth.Log("User signed up", map[string]any{"user_id": 123})
lognorth.Error("Checkout failed", err, map[string]any{"order_id": 42})
}
slog.SetDefault(slog.New(lognorth.NewHandler()))
slog.Info("User signed up", "user_id", 123)
slog.Error("Checkout failed", "error", err)
http.HandleFunc("/", homeHandler)
http.HandleFunc("/users", usersHandler)
http.ListenAndServe(":8080", lognorth.Middleware(http.DefaultServeMux))

The middleware automatically:

  • Generates a trace_id per request (or uses X-Trace-ID from the incoming header)
  • Sets X-Trace-ID on the response
  • Logs the request with duration_ms and trace_id as top-level fields

Skip health checks and other noisy endpoints:

lognorth.IgnorePaths("/healthz", "/_health", "/metrics")

These paths won’t be logged by the middleware.

MethodBehavior
Log()Batched (10 events or 5s)
Error()Sent immediately with stack trace
MiddlewareLogs requests with trace_id + duration_ms
ShutdownAuto-flush on SIGINT/SIGTERM

Error() automatically captures the error class, file, line, caller function, and full stack trace. These are used for three-tier issue grouping.