Module: Rhales::Utils::LoggingHelpers
- Includes:
- Rhales::Utils
- Included in:
- CSP, HydrationDataAggregator, TemplateEngine, View, ViewComposition
- Defined in:
- lib/rhales/utils/logging_helpers.rb
Overview
Helper methods for consistent logging and timing instrumentation across Rhales components
Instance Method Summary collapse
-
#format_value(value) ⇒ Object
private
Format individual log values.
-
#log_timed_operation(logger, level, message, **metadata) { ... } ⇒ Object
Log with timing for an operation.
-
#log_with_metadata(logger, level, message, metadata = {}) ⇒ Object
Log a message with structured metadata.
Methods included from Rhales::Utils
#now, #now_in_μs, #pretty_path
Instance Method Details
#format_value(value) ⇒ Object (private)
Format individual log values
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rhales/utils/logging_helpers.rb', line 52 def format_value(value) case value when String value.include?(' ') ? "\"#{value}\"" : value when Symbol, Numeric, true, false, nil value.to_s when Array # For arrays longer than 5 items, show count + first/last items if value.size > 5 first_three = value.first(3).join(', ') last_two = value.last(2).join(', ') "[#{value.size} items: #{first_three} ... #{last_two}]" elsif value.empty? '[]' else "[#{value.join(', ')}]" end else value.to_s end end |
#log_timed_operation(logger, level, message, **metadata) { ... } ⇒ Object
Log with timing for an operation
Logs the operation with timing information in microseconds.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rhales/utils/logging_helpers.rb', line 21 def log_timed_operation(logger, level, , **) start_time = now_in_μs result = yield duration = now_in_μs - start_time (logger, level, , .merge(duration: duration)) result rescue StandardError => ex duration = now_in_μs - start_time (logger, :error, "#{} failed", .merge( duration: duration, error: ex., error_class: ex.class.name, ) ) raise end |
#log_with_metadata(logger, level, message, metadata = {}) ⇒ Object
Log a message with structured metadata
42 43 44 45 46 47 |
# File 'lib/rhales/utils/logging_helpers.rb', line 42 def (logger, level, , = {}) return logger.public_send(level, ) if .empty? = .map { |k, v| "#{k}=#{format_value(v)}" }.join(' ') logger.public_send(level, "#{}: #{}") end |