---
title: Log All Relevant Context On Errors
impact: HIGH
impactDescription: enables quick debugging and incident response
tags: error-handling, logging, context, debugging, quality
---

## Log All Relevant Context On Errors

Context-rich logs enable quick debugging. Without proper context, finding root causes is difficult.

**Incorrect (minimal context):**

```go
slog.Error("error occurred")
slog.Error(err.Error())
```

**Correct (comprehensive context using slog):**

```go
slog.Error("failed to process order",
  // What happened
  "error", err,
  "stack", string(debug.Stack()), // Optional: but useful for critical errors
  
  // Context
  "order_id", order.ID,
  "user_id", user.ID,
  "request_id", ctx.Value("request_id"),
  
  // Input that caused the issue
  "item_count", len(order.Items),
  "total_amount", order.Total,
  
  // Timing
  "processing_time_ms", time.Since(startTime).Milliseconds(),
)
```

**Essential context:**
- Error details
- Entity identifiers
- Request/correlation IDs
- Relevant input summary
- Timing information

**Tools:** `slog`, `zap`, `logback` equivalent
