File: crash-handling.html.md

package info (click to toggle)
ruby-dry-logger 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 444 kB
  • sloc: ruby: 2,170; makefile: 4; sh: 4
file content (61 lines) | stat: -rw-r--r-- 1,465 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
---
title: Crash handling
layout: gem-single
name: dry-logger
---

By default, if logging itself crashes, dry-logger handles the error gracefully and logs to stderr. You can customize this behavior.

## Default crash behavior

```ruby
logger = Dry.Logger(:my_app)

# If logging crashes, you'll see something like:
# [my_app] [FATAL] [2023-10-15 14:00:00 +0000] Logging crashed
#   Original message
#   Error details (ExceptionClass)
#   Backtrace...
```

## Custom crash handler

```ruby
logger = Dry.Logger(:my_app,
  on_crash: -> (progname:, exception:, message:, payload:) {
    # Send to error tracking service
    Sentry.capture_exception(exception,
      extra: {
        progname: progname,
        log_message: message,
        log_payload: payload
      }
    )

    # Also write to a separate crash log
    File.open("logs/logging_crashes.log", "a") do |f|
      f.puts "Logging crashed: #{exception.message}"
      f.puts "Progname: #{progname}"
      f.puts "Message: #{message.inspect}"
      f.puts "Payload: #{payload.inspect}"
    end
  }
)
```

## Crash prevention

Ensure your crash handler doesn't itself crash:

```ruby
on_crash: -> (progname:, exception:, **) {
  begin
    # Try to send to monitoring service
    ErrorTracker.notify(exception, context: progname)
  rescue => error
    # Fallback: write to stderr
    warn "Logging crashed AND crash handler failed: #{error.message}"
    warn "Original error: #{exception.message}"
  end
}
```