Class: ActiveModelLogger::BlockLogger

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_metadataObject (readonly)

Returns the value of attribute base_metadata.



7
8
9
# File 'lib/active_model_logger/block_logger.rb', line 7

def 
  
end

#log_chainObject (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_entriesObject (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_levelObject (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

#loggableObject (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_toObject (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

Examples:

logger.log("Process started")
logger.log("Step completed", status: "success", category: "processing")
logger.log("Error occurred", log_level: "error", error_code: "E001")

Parameters:

  • message (String)

    The log message (required)

  • visible_to (String) (defaults to: nil)

    Override visibility for this specific log (optional)

  • log_level (String) (defaults to: nil)

    Override log level for this specific log (optional)

  • metadata (Hash)

    Additional metadata for this specific log

Returns:

  • (Hash)

    The log entry hash that was added 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(message, 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(message, 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: 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, message, effective_visible_to, normalized_log_level, )

  log_entry
end

#save_logsArray<ActiveModelLogger::Log>

Save all collected log entries to the database

Returns:



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