You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add active job support and colored job prefix (silva96#54)
* Add active job support and colored job prefix
* Add plain ActiveJob support
* Fix standard
* enrich existing log lines if they dont have the colored job id already
* Fix standard
* use job_id name instead of jid
* add job - request_id to map on the parsing process
Add job prefix if needed, on the parsing process
Add request id if needed, on the parsing process
Also, created a conviniece access method to the state so we don't need to pass it around everywhere.
Copy file name to clipboardExpand all lines: README.md
+76Lines changed: 76 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -187,6 +187,82 @@ LogBench works with JSON-formatted logs. Each log entry should include:
187
187
-`message`: SQL query with timing information
188
188
-`request_id`: Links query to HTTP request
189
189
190
+
## Job Logging
191
+
192
+
LogBench provides enhanced logging for background jobs with colored job prefixes in the TUI. Job logs are automatically prefixed with `[JobClass#job-id]` in orange/yellow color for easy identification.
193
+
194
+
### ActiveJob
195
+
196
+
For **ActiveJob** classes, use the job's `logger` method (not `Rails.logger`) to ensure logs get proper job context:
197
+
198
+
```ruby
199
+
classEmailDeliveryJob < ApplicationJob
200
+
defperform(user_id)
201
+
logger.info "Starting email delivery for user #{user_id}"# ✅ Will have job prefix
202
+
203
+
user =User.find(user_id)
204
+
logger.info "Found user: #{user.email}"# ✅ Will have job prefix
205
+
206
+
# SQL queries are automatically tagged
207
+
user.update!(last_email_sent_at:Time.current) # ✅ Will have job prefix
208
+
209
+
logger.info "Email delivery completed"# ✅ Will have job prefix
210
+
end
211
+
end
212
+
```
213
+
214
+
**❌ Don't use `Rails.logger` in ActiveJob:**
215
+
```ruby
216
+
classEmailDeliveryJob < ApplicationJob
217
+
defperform(user_id)
218
+
Rails.logger.info "This won't have job prefix"# ❌ No job context
219
+
end
220
+
end
221
+
```
222
+
223
+
### Plain Sidekiq Jobs
224
+
225
+
For **plain Sidekiq** jobs (not using ActiveJob), LogBench automatically captures job context:
226
+
227
+
```ruby
228
+
classDataProcessingJob
229
+
includeSidekiq::Job
230
+
231
+
defperform(data_id)
232
+
Rails.logger.info "Processing data #{data_id}"# ✅ Will have job prefix
233
+
234
+
# SQL queries are automatically tagged
235
+
data =Data.find(data_id) # ✅ Will have job prefix
236
+
data.process!
237
+
238
+
Rails.logger.info "Data processing completed"# ✅ Will have job prefix
239
+
end
240
+
end
241
+
```
242
+
243
+
### Job Log Output
244
+
245
+
In the TUI, job-related logs appear with colored prefixes:
246
+
247
+
```
248
+
🟡[EmailDeliveryJob#email-job-123] Starting email delivery for user 456
249
+
🟡[EmailDeliveryJob#email-job-123] Found user: user@example.com
250
+
🟡[EmailDeliveryJob#email-job-123] User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
0 commit comments