Class: DVLA::Herodotus::HerodotusLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/dvla/herodotus/herodotus_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system_name, *args, config: DVLA::Herodotus.config, **kwargs) ⇒ HerodotusLogger

Initializes the logger Sets a default correlation_id and creates the formatter Syncs all instances of the HerodotusLogger when the main flag is present Any subsequent loggers will also be synced



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 12

def initialize(system_name, *args, config: DVLA::Herodotus.config, **kwargs)
  super(*args, **kwargs)

  @system_name = system_name
  @main = config[:main]
  @display_pid = config[:display_pid]

  @correlation_id = SecureRandom.uuid[0, 8]
  set_formatter

  if DVLA::Herodotus.main_logger && @main
    warn("Main logger already set: '#{DVLA::Herodotus.main_logger.system_name}'. This will be overwritten by '#{system_name}'")
  end

  DVLA::Herodotus.main_logger = self if @main
  sync_correlation_ids if DVLA::Herodotus.main_logger
end

Instance Attribute Details

#correlation_idObject

Returns the value of attribute correlation_id.



6
7
8
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 6

def correlation_id
  @correlation_id
end

#display_pidObject

Returns the value of attribute display_pid.



6
7
8
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 6

def display_pid
  @display_pid
end

#mainObject

Returns the value of attribute main.



6
7
8
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 6

def main
  @main
end

#scenario_idObject

Returns the value of attribute scenario_id.



6
7
8
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 6

def scenario_id
  @scenario_id
end

#system_nameObject

Returns the value of attribute system_name.



6
7
8
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 6

def system_name
  @system_name
end

Instance Method Details

#new_scenario(scenario_id) ⇒ Object

Creates a new correlation_id and re-creates the formatter per scenario. If this method is called on an instance of HerodotusLogger not flagged as main the correlation_id of the main logger will be updated and all logger’s correlation_ids re-synced.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 33

def new_scenario(scenario_id)
  @scenario_id = scenario_id
  @correlation_id = SecureRandom.uuid[0, 8]

  if DVLA::Herodotus.main_logger && self != DVLA::Herodotus.main_logger
    warn('You are calling new_scenario on a non-main logger.')

    DVLA::Herodotus.main_logger.correlation_id = @correlation_id
    DVLA::Herodotus.main_logger.scenario_id = @scenario_id
  end

  set_formatter
  sync_correlation_ids if DVLA::Herodotus.main_logger
end

#set_formatterObject

Sets the format of the log. Needs to be called each time correlation_id is changed after initialization in-order for the changes to take affect.



69
70
71
72
73
74
75
76
77
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 69

def set_formatter
  self.formatter = proc do |severity, _datetime, _progname, msg|
    "[#{@system_name} " \
      "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} " \
      "#{@correlation_id}" \
      "#{' '.concat(Process.pid.to_s) if @display_pid}] " \
      "#{severity} -- : #{msg}\n"
  end
end

#sync_correlation_idsObject

Finds all instances of HerodotusLogger and updates their correlation_id and scenario_id to match that of the main HerodotusLogger.



50
51
52
53
54
55
56
57
58
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 50

def sync_correlation_ids
  ObjectSpace.each_object(DVLA::Herodotus::HerodotusLogger) do |logger|
    unless logger == DVLA::Herodotus.main_logger
      logger.correlation_id = DVLA::Herodotus.main_logger.correlation_id
      logger.scenario_id = DVLA::Herodotus.main_logger.scenario_id
      logger.set_formatter
    end
  end
end