Class: BackupRestore::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/backup_restore/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id: nil, client_id: nil) ⇒ Logger

Returns a new instance of Logger.



7
8
9
10
11
12
13
# File 'lib/backup_restore/logger.rb', line 7

def initialize(user_id: nil, client_id: nil)
  @user_id = user_id
  @client_id = client_id
  @publish_to_message_bus = @user_id.present? && @client_id.present?

  @logs = []
end

Instance Attribute Details

#logsObject (readonly)

Returns the value of attribute logs.



5
6
7
# File 'lib/backup_restore/logger.rb', line 5

def logs
  @logs
end

Instance Method Details

#log(message, ex = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/backup_restore/logger.rb', line 15

def log(message, ex = nil)
  return if Rails.env.test?

  timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  puts(message)
  publish_log(message, timestamp)
  save_log(message, timestamp)

  if ex
    formatted_ex = "#{ex}\n" + ex.backtrace.join("\n")
    puts formatted_ex
    Rails.logger.error(formatted_ex)
  end
end