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
Instance Method Details
#format_value(value) ⇒ Object (private)
Format individual log values
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rhales/utils/logging_helpers.rb', line 49 def format_value(value) case value when String value.include?(' ') ? "\"#{value}\"" : value when Symbol, Numeric, true, false, nil value.to_s when Array "[#{value.join(', ')}]" 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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rhales/utils/logging_helpers.rb', line 19 def log_timed_operation(logger, level, , **, &block) 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
39 40 41 42 43 44 |
# File 'lib/rhales/utils/logging_helpers.rb', line 39 def (logger, level, , = {}) return logger.public_send(level, ) if .empty? = .map { |k, v| "#{k}=#{format_value(v)}" }.join(' ') logger.public_send(level, "#{}: #{}") end |