Class: ActiveModelLogger::BlockLogger
- Inherits:
-
Object
- Object
- ActiveModelLogger::BlockLogger
- Defined in:
- lib/active_model_logger/block_logger.rb
Overview
BlockLogger is a helper class used by the log_block method to collect log entries during block execution and save them all at once when the block exits.
Instance Attribute Summary collapse
-
#base_metadata ⇒ Object
readonly
Returns the value of attribute base_metadata.
-
#log_chain ⇒ Object
readonly
Returns the value of attribute log_chain.
-
#log_entries ⇒ Object
readonly
Returns the value of attribute log_entries.
-
#log_level ⇒ Object
readonly
Returns the value of attribute log_level.
-
#loggable ⇒ Object
readonly
Returns the value of attribute loggable.
-
#visible_to ⇒ Object
readonly
Returns the value of attribute visible_to.
Instance Method Summary collapse
-
#initialize(loggable, log_chain, visible_to, log_level, base_metadata = {}) ⇒ BlockLogger
constructor
A new instance of BlockLogger.
-
#log(message, visible_to: nil, log_level: nil, **metadata) ⇒ Hash
Add a log entry to the collection.
-
#save_logs ⇒ Array<ActiveModelLogger::Log>
Save all collected log entries to the database.
Constructor Details
#initialize(loggable, log_chain, visible_to, log_level, base_metadata = {}) ⇒ BlockLogger
Returns a new instance of BlockLogger.
9 10 11 12 13 14 15 16 |
# File 'lib/active_model_logger/block_logger.rb', line 9 def initialize(loggable, log_chain, visible_to, log_level, = {}) @loggable = loggable @log_chain = log_chain @visible_to = visible_to @log_level = log_level = @log_entries = [] end |
Instance Attribute Details
#base_metadata ⇒ Object (readonly)
Returns the value of attribute base_metadata.
7 8 9 |
# File 'lib/active_model_logger/block_logger.rb', line 7 def end |
#log_chain ⇒ Object (readonly)
Returns the value of attribute log_chain.
7 8 9 |
# File 'lib/active_model_logger/block_logger.rb', line 7 def log_chain @log_chain end |
#log_entries ⇒ Object (readonly)
Returns the value of attribute log_entries.
7 8 9 |
# File 'lib/active_model_logger/block_logger.rb', line 7 def log_entries @log_entries end |
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
7 8 9 |
# File 'lib/active_model_logger/block_logger.rb', line 7 def log_level @log_level end |
#loggable ⇒ Object (readonly)
Returns the value of attribute loggable.
7 8 9 |
# File 'lib/active_model_logger/block_logger.rb', line 7 def loggable @loggable end |
#visible_to ⇒ Object (readonly)
Returns the value of attribute visible_to.
7 8 9 |
# File 'lib/active_model_logger/block_logger.rb', line 7 def visible_to @visible_to end |
Instance Method Details
#log(message, visible_to: nil, log_level: nil, **metadata) ⇒ Hash
Add a log entry to the collection
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_model_logger/block_logger.rb', line 31 def log(, visible_to: nil, log_level: nil, **) # Use block defaults if not provided effective_visible_to = visible_to || @visible_to effective_log_level = log_level || @log_level # Validate inputs validate_log_inputs(, effective_visible_to, effective_log_level, ) # Normalize inputs normalized_log_level = effective_log_level.downcase = .merge(.transform_keys(&:to_s)) # Create log entry hash log_entry = { loggable_type: @loggable.class.name, loggable_id: @loggable.id, message: .to_s, metadata: { log_chain: @log_chain, status: ["status"], category: ["category"], type: ["type"], title: ["title"], visible_to: effective_visible_to.to_s, log_level: normalized_log_level, data: ["data"], }, created_at: Time.current, updated_at: Time.current, } # Add to collection @log_entries << log_entry # Send to stdout if enabled log_to_stdout_block(log_entry, , effective_visible_to, normalized_log_level, ) log_entry end |
#save_logs ⇒ Array<ActiveModelLogger::Log>
Save all collected log entries to the database
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/active_model_logger/block_logger.rb', line 74 def save_logs return [] if @log_entries.empty? # Insert all logs at once using insert_all (requires ActiveRecord 6.0+) ActiveModelLogger::Log.insert_all(@log_entries) # Reload the association to include new logs @loggable.active_model_logs.reload # Return the created log entries - query directly to avoid ordering conflicts # Always query by the loggable and order by created_at to ensure chronological order # Filter by log_chain in Ruby to avoid database-specific JSON query issues all_logs = ActiveModelLogger::Log.where(loggable_type: @loggable.class.name, loggable_id: @loggable.id) .order(:created_at) all_logs.select { |log| log.["log_chain"] == @log_chain } end |