Skip to content
Install · free

Go

Send logs and errors from Go to LogNorth: an slog handler, HTTP middleware with trace IDs, and stack traces on errors.

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})
}

Config sends from every environment. To label events and keep development and test quiet, use Configure:

lognorth.Configure(lognorth.Options{
URL: os.Getenv("LOGNORTH_URL"),
APIKey: os.Getenv("LOGNORTH_API_KEY"),
Environment: os.Getenv("APP_ENV"), // stamped on every event as context.environment
})

With Configure, LogNorth sends nothing when Environment is development or test. Set Enabled to decide yourself.

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

Name the signed-in user inside a handler. Use an ID, never an email:

lognorth.SetUser(r.Context(), strconv.Itoa(user.ID))

The issue page then shows how many users an issue hit. Errors also carry the release, from Options.Release or from LOGNORTH_RELEASE, GIT_SHA, KAMAL_VERSION, or your host’s commit variable. At startup the SDK logs Release <version> started, and LogNorth draws each deploy as a dashed line on its charts.

Skip health checks and other noisy endpoints:

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

These paths won’t be logged by the middleware.

Method Behavior
Log() Batched (10 events or 5s)
Error() Sent immediately with stack trace
Middleware Logs requests with trace_id + duration_ms
Shutdown Auto-flush on SIGINT/SIGTERM

Error() automatically captures the error class, file, line, caller function, and full stack trace. LogNorth groups errors into issues by them.