---
title: Include Context in Error Logging
impact: MEDIUM
impactDescription: provides necessary details for triaging issues
tags: logging, error-handling, maintenance, quality
---

## Include Context in Error Logging

When an error occurs, include relevant contextual data (IDs, parameters, state) in the log message.

**Incorrect (missing context):**

```ruby
begin
  update_profile(params)
rescue => e
  Rails.logger.error "Profile update failed: #{e.message}"
end
```

**Correct (with context):**

```ruby
begin
  update_profile(params)
rescue => e
  # Log which record failed and with what data
  Rails.logger.error "Profile update failed for User ID: #{current_user.id}. Params: #{params.inspect}. Error: #{e.message}"
end
```

**Tools:** Manual Review
---
