Class: SavvyOpenrouter::ApiCallLogger
- Inherits:
-
Object
- Object
- SavvyOpenrouter::ApiCallLogger
- Defined in:
- lib/savvy_openrouter/api_call_logger.rb
Overview
Persists OpenRouter HTTP exchanges when configured via api_call_log (YAML or Client kwargs). Failures while saving never raise into application code.
Constant Summary collapse
- DEFAULT_MAX_BODY_BYTES =
65_536- CANONICAL_KEYS =
%w[ method path status duration_ms request_body response_body error_class error_message streaming ].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #enabled? ⇒ Boolean
-
#initialize(config) ⇒ ApiCallLogger
constructor
A new instance of ApiCallLogger.
- #max_body_limit ⇒ Object
-
#record(attrs) ⇒ Object
attrsuses canonical string keys (see CANONICAL_KEYS).
Constructor Details
#initialize(config) ⇒ ApiCallLogger
Returns a new instance of ApiCallLogger.
44 45 46 |
# File 'lib/savvy_openrouter/api_call_logger.rb', line 44 def initialize(config) @config = config.is_a?(Hash) ? Configuration.stringify_keys_static(config) : {} end |
Class Method Details
.format_body_for_log(obj, max_bytes: DEFAULT_MAX_BODY_BYTES) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/savvy_openrouter/api_call_logger.rb', line 16 def format_body_for_log(obj, max_bytes: DEFAULT_MAX_BODY_BYTES) str = case obj when nil then +"" when String then obj.b else JSON.generate(obj) end str = redact_secrets(str) truncate_bytes(str, max_bytes) end |
Instance Method Details
#enabled? ⇒ Boolean
48 49 50 51 52 |
# File 'lib/savvy_openrouter/api_call_logger.rb', line 48 def enabled? m = @config["model"] !m.nil? && !m.to_s.strip.empty? && @config["columns"].is_a?(Hash) && !@config["columns"].empty? end |
#max_body_limit ⇒ Object
54 55 56 57 |
# File 'lib/savvy_openrouter/api_call_logger.rb', line 54 def max_body_limit n = @config["max_body_bytes"] n.is_a?(Integer) && n.positive? ? n : DEFAULT_MAX_BODY_BYTES end |
#record(attrs) ⇒ Object
attrs uses canonical string keys (see CANONICAL_KEYS). Column mapping is applied before create.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/savvy_openrouter/api_call_logger.rb', line 60 def record(attrs) return unless enabled? row = build_row(attrs) return if row.empty? constantize_model(@config["model"].to_s.strip).create!(row) rescue StandardError => e warn "[savvy_openrouter] api_call_log skipped: #{e.class}: #{e.}" if $VERBOSE nil end |