Class: Dataflow::Logger

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

Defined Under Namespace

Classes: LoggerImpl

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, use_notifications: false) ⇒ Logger

Returns a new instance of Logger.



7
8
9
10
11
# File 'lib/dataflow/logger.rb', line 7

def initialize(prefix:, use_notifications: false)
  @prefix = prefix
  @use_notifications = use_notifications
  @@impl = LoggerImpl.new
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



4
5
6
# File 'lib/dataflow/logger.rb', line 4

def prefix
  @prefix
end

#use_notificationsObject

Returns the value of attribute use_notifications.



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

def use_notifications
  @use_notifications
end

Instance Method Details

#error(error:, custom_message: '') ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/dataflow/logger.rb', line 21

def error(error:, custom_message: '')
  first_line = "[ERROR => #{error.class}: '#{error.message}']"
  first_line += " #{custom_message}" if custom_message.present?
  first_line += ' Backtrace: '
  log(first_line)
  log('--')
  (error.backtrace || []).each_with_index { |line, idx| log("#{idx}: #{line}") }
end

#log(str) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/dataflow/logger.rb', line 13

def log(str)
  return if ENV['DATAFLOW_SKIP_LOGGING']
  now = DateTime.now.strftime('%y-%m-%d %H:%M:%S')
  message = "[#{now}][#{trace_id}] #{prefix} | #{str}"
  logger_impl = @@impl
  logger_impl.log(message)
end

#trace_idObject



30
31
32
# File 'lib/dataflow/logger.rb', line 30

def trace_id
  (Process.pid + Thread.current.object_id).to_s(16)[-8..-1]
end