Module: ActiveModelLogger::LoggableHelpers
- Included in:
- Loggable
- Defined in:
- lib/active_model_logger/loggable_helpers.rb
Overview
Helper methods for the Loggable concern
Instance Method Summary collapse
- #build_batch_log_entries(entries) ⇒ Object
- #build_log_attributes(message, visible_to, log_level, normalized_metadata, log_chain_value) ⇒ Object
- #cleanup_logs(older_than: 30.days, keep_recent: 100) ⇒ Object
- #create_log_entry(log_attributes) ⇒ Object
- #generate_log_chain(normalized_metadata) ⇒ Object
- #validate_log_inputs(message, visible_to, log_level, metadata) ⇒ Object
Instance Method Details
#build_batch_log_entries(entries) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_model_logger/loggable_helpers.rb', line 48 def build_batch_log_entries(entries) entries.map do |entry| { loggable_type: self.class.name, loggable_id: id, message: entry[:message].to_s, metadata: { log_chain: entry[:log_chain] || log_chain, status: entry[:status], category: entry[:category], type: entry[:type], title: entry[:title], visible_to: entry[:visible_to] || loggable_config[:default_visible_to], log_level: (entry[:log_level] || loggable_config[:default_log_level]).downcase, data: entry[:data], }, created_at: Time.current, updated_at: Time.current, } end end |
#build_log_attributes(message, visible_to, log_level, normalized_metadata, log_chain_value) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_model_logger/loggable_helpers.rb', line 26 def build_log_attributes(, visible_to, log_level, , log_chain_value) { message: .to_s, log_chain: log_chain_value, status: ["status"], category: ["category"], type: ["type"], title: ["title"], visible_to: visible_to.to_s, log_level: log_level, data: ["data"], } end |
#cleanup_logs(older_than: 30.days, keep_recent: 100) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/active_model_logger/loggable_helpers.rb', line 70 def cleanup_logs(older_than: 30.days, keep_recent: 100) old_logs = active_model_logs.where("created_at < ?", older_than.ago) recent_logs = active_model_logs.newest(keep_recent) logs_to_delete = old_logs.where.not(id: recent_logs.select(:id)) logs_to_delete.delete_all end |
#create_log_entry(log_attributes) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/active_model_logger/loggable_helpers.rb', line 40 def create_log_entry(log_attributes) if respond_to?(:create_log!) create_log!(log_attributes) else active_model_logs.create!(log_attributes) end end |
#generate_log_chain(normalized_metadata) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/active_model_logger/loggable_helpers.rb', line 18 def generate_log_chain() if loggable_config[:auto_log_chain] && ["log_chain"].nil? send(loggable_config[:log_chain_method]) else ["log_chain"] || send(loggable_config[:log_chain_method]) end end |
#validate_log_inputs(message, visible_to, log_level, metadata) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_model_logger/loggable_helpers.rb', line 6 def validate_log_inputs(, visible_to, log_level, ) raise ArgumentError, "Message cannot be nil" if .nil? raise ArgumentError, "Visible_to cannot be nil or empty" if visible_to.nil? || visible_to.empty? raise ArgumentError, "Log level cannot be nil or empty" if log_level.nil? || log_level.empty? raise ArgumentError, "Metadata must be a hash" unless .is_a?(Hash) valid_levels = %w[debug info warn error fatal] return if valid_levels.include?(log_level.downcase) raise ArgumentError, "Invalid log level '#{log_level}'. Must be one of: #{valid_levels.join(', ')}" end |