Module: RailsFlowMap::Logging
Overview
Centralized logging functionality for RailsFlowMap
This module provides a standardized logging interface across all components of the RailsFlowMap gem. It supports configurable log levels, structured logging, and automatic context inclusion.
Defined Under Namespace
Classes: StructuredFormatter
Constant Summary collapse
- CUSTOM_LEVELS =
Custom log levels for domain-specific logging
{ security: Logger::WARN, performance: Logger::INFO, graph_analysis: Logger::DEBUG }.freeze
Instance Method Summary collapse
-
#configure_for_environment(environment, options = {}) ⇒ Object
Configure logging for different environments.
-
#log_error(error, context: {}, level: :error) ⇒ Object
Log an error with full context and backtrace.
-
#log_graph_analysis(analysis_type, graph_metrics = {}) ⇒ Object
Log graph analysis information.
-
#log_performance(operation, duration, additional_metrics = {}) ⇒ Object
Log a performance metric.
-
#log_security(event, severity: :medium, details: {}) ⇒ Object
Log a security event.
-
#logger ⇒ Logger
The shared logger instance.
-
#logger=(custom_logger) ⇒ Object
Sets a custom logger instance.
-
#time_operation(operation, additional_metrics = {}) { ... } ⇒ Object
Time a block and log its performance.
-
#with_context(context = {}) { ... } ⇒ Object
Execute a block with additional logging context.
Instance Method Details
#configure_for_environment(environment, options = {}) ⇒ Object
Configure logging for different environments
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rails_flow_map/logging.rb', line 181 def configure_for_environment(environment, = {}) case environment when :development logger.level = Logger::DEBUG logger.formatter = StructuredFormatter.new when :test logger.level = Logger::WARN logger.formatter = Logger::Formatter.new when :production logger.level = Logger::INFO logger.formatter = StructuredFormatter.new end # Apply any custom options .each do |key, value| case key when :level logger.level = value when :formatter logger.formatter = value end end end |
#log_error(error, context: {}, level: :error) ⇒ Object
Log an error with full context and backtrace
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rails_flow_map/logging.rb', line 63 def log_error(error, context: {}, level: :error) error_context = { error_class: error.class.name, error_message: error., **context } with_context(error_context) do logger.send(level, "#{error.class}: #{error.message}") if error.backtrace && logger.level <= Logger::DEBUG logger.debug("Backtrace:\n#{error.backtrace.join("\n")}") end end end |
#log_graph_analysis(analysis_type, graph_metrics = {}) ⇒ Object
Log graph analysis information
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rails_flow_map/logging.rb', line 134 def log_graph_analysis(analysis_type, graph_metrics = {}) return unless logger.level <= CUSTOM_LEVELS[:graph_analysis] analysis_context = { analysis_type: analysis_type, **graph_metrics } with_context(analysis_context) do logger.debug("Graph Analysis: #{analysis_type}") if graph_metrics.any? metrics_str = graph_metrics.map { |k, v| "#{k}=#{v}" }.join(', ') logger.debug("Graph metrics: #{metrics_str}") end end end |
#log_performance(operation, duration, additional_metrics = {}) ⇒ Object
Log a performance metric
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rails_flow_map/logging.rb', line 97 def log_performance(operation, duration, additional_metrics = {}) return unless logger.level <= CUSTOM_LEVELS[:performance] metrics = { operation: operation, duration_seconds: duration.round(3), **additional_metrics } with_context(metrics) do logger.info("Performance: #{operation} completed in #{duration.round(3)}s") end end |
#log_security(event, severity: :medium, details: {}) ⇒ Object
Log a security event
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rails_flow_map/logging.rb', line 116 def log_security(event, severity: :medium, details: {}) return unless logger.level <= CUSTOM_LEVELS[:security] security_context = { security_event: event, severity: severity, **details } with_context(security_context) do logger.warn("SECURITY: #{event}") end end |
#logger ⇒ Logger
47 48 49 |
# File 'lib/rails_flow_map/logging.rb', line 47 def logger @logger ||= create_logger end |
#logger=(custom_logger) ⇒ Object
Sets a custom logger instance
54 55 56 |
# File 'lib/rails_flow_map/logging.rb', line 54 def logger=(custom_logger) @logger = custom_logger end |
#time_operation(operation, additional_metrics = {}) { ... } ⇒ Object
Time a block and log its performance
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rails_flow_map/logging.rb', line 158 def time_operation(operation, additional_metrics = {}) start_time = Time.now begin result = yield duration = Time.now - start_time log_performance(operation, duration, additional_metrics) result rescue => e duration = Time.now - start_time log_error(e, context: { operation: operation, duration_seconds: duration.round(3), **additional_metrics }) raise end end |
#with_context(context = {}) { ... } ⇒ Object
Execute a block with additional logging context
83 84 85 86 87 88 89 90 |
# File 'lib/rails_flow_map/logging.rb', line 83 def with_context(context = {}) old_context = Thread.current[:rails_flow_map_context] || {} Thread.current[:rails_flow_map_context] = old_context.merge(context) yield ensure Thread.current[:rails_flow_map_context] = old_context end |