Class: AndOne::JsonFormatter
- Inherits:
-
Object
- Object
- AndOne::JsonFormatter
- Defined in:
- lib/and_one/json_formatter.rb
Overview
Formats N+1 detections as structured JSON for log aggregation services (Datadog, Splunk, New Relic, etc.).
Usage:
AndOne.json_logging = true
Or use directly:
formatter = AndOne::JsonFormatter.new
formatter.format(detections) # => JSON string
Each detection becomes a JSON object with:
- event: "n_plus_one_detected"
- table, fingerprint, query_count, sample_query
- origin, fix_location, suggestion
- timestamp, severity
Instance Method Summary collapse
-
#format(detections) ⇒ Object
Returns a JSON string containing an array of detection objects.
-
#format_hashes(detections) ⇒ Object
Returns an array of hashes (useful for structured logging integrations that accept hashes directly, e.g., Rails tagged logging or Semantic Logger).
-
#initialize(backtrace_cleaner: nil) ⇒ JsonFormatter
constructor
A new instance of JsonFormatter.
Constructor Details
#initialize(backtrace_cleaner: nil) ⇒ JsonFormatter
Returns a new instance of JsonFormatter.
23 24 25 |
# File 'lib/and_one/json_formatter.rb', line 23 def initialize(backtrace_cleaner: nil) @backtrace_cleaner = backtrace_cleaner end |
Instance Method Details
#format(detections) ⇒ Object
Returns a JSON string containing an array of detection objects. When there's a single detection, returns just the object (not wrapped in array).
29 30 31 32 |
# File 'lib/and_one/json_formatter.rb', line 29 def format(detections) entries = detections.map { |d| format_detection(d) } entries.size == 1 ? JSON.generate(entries.first) : JSON.generate(entries) end |
#format_hashes(detections) ⇒ Object
Returns an array of hashes (useful for structured logging integrations that accept hashes directly, e.g., Rails tagged logging or Semantic Logger).
36 37 38 |
# File 'lib/and_one/json_formatter.rb', line 36 def format_hashes(detections) detections.map { |d| format_detection(d) } end |