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

Methods included from Rhales::Utils

#now, #now_in_μs

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.

Parameters:

  • logger (Logger)

    The logger instance to use

  • level (Symbol)

    The log level (:debug, :info, :warn, :error)

  • message (String)

    The log message

  • metadata (Hash)

    Additional metadata to include in the log

Yields:

  • The block to execute and time

Returns:

  • The result of the block



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, message, **, &block)
  start_time = now_in_μs
  result = yield
  duration = now_in_μs - start_time

  (logger, level, message, .merge(duration: duration))

  result
rescue StandardError => ex
  duration = now_in_μs - start_time
  (logger, :error, "#{message} failed",
    .merge(
      duration: duration,
      error: ex.message,
      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, message,  = {})
  return logger.public_send(level, message) if .empty?

   = .map { |k, v| "#{k}=#{format_value(v)}" }.join(' ')
  logger.public_send(level, "#{message}: #{}")
end