Class: DVLA::Herodotus::HerodotusLogger
- Inherits:
-
Logger
- Object
- Logger
- DVLA::Herodotus::HerodotusLogger
- Defined in:
- lib/dvla/herodotus/herodotus_logger.rb
Instance Attribute Summary collapse
-
#correlation_id ⇒ Object
Returns the value of attribute correlation_id.
-
#display_pid ⇒ Object
Returns the value of attribute display_pid.
-
#main ⇒ Object
Returns the value of attribute main.
-
#prefix_colour ⇒ Object
Returns the value of attribute prefix_colour.
-
#scenario_id ⇒ Object
Returns the value of attribute scenario_id.
-
#system_name ⇒ Object
Returns the value of attribute system_name.
Instance Method Summary collapse
-
#initialize(system_name, *args, config: DVLA::Herodotus.config, **kwargs) ⇒ HerodotusLogger
constructor
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.
-
#new_scenario(scenario_id) ⇒ Object
Creates a new correlation_id and re-creates the formatter per scenario.
-
#set_formatter ⇒ Object
Sets the format of the log.
-
#spawn_child_logger(system_name:) ⇒ Object
Creates a new logger with a different system name but inherits config and output path.
-
#sync_correlation_ids ⇒ Object
Finds all instances of HerodotusLogger and updates their correlation_id and scenario_id to match that of the main HerodotusLogger.
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 29 |
# 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 @prefix_colour = config.prefix_colour || {} validate_colour_config if @prefix_colour.any? @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_id ⇒ Object
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_pid ⇒ Object
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 |
#main ⇒ Object
Returns the value of attribute main.
6 7 8 |
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 6 def main @main end |
#prefix_colour ⇒ Object
Returns the value of attribute prefix_colour.
6 7 8 |
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 6 def prefix_colour @prefix_colour end |
#scenario_id ⇒ Object
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_name ⇒ Object
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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 34 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_formatter ⇒ Object
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 effect.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 80 def set_formatter self.formatter = proc do |severity, _datetime, _progname, msg| now = Time.now components = { system: @system_name, date: now.strftime('%Y-%m-%d'), time: now.strftime('%H:%M:%S'), correlation: @correlation_id, pid: @display_pid ? Process.pid.to_s : nil, level: severity, separator: '-- :', } prefix = @prefix_colour.any? ? build_prefix_with_colour(components) : build_prefix(components) "#{prefix}#{msg}\n" end end |
#spawn_child_logger(system_name:) ⇒ Object
Creates a new logger with a different system name but inherits config and output path
62 63 64 65 66 67 68 69 |
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 62 def spawn_child_logger(system_name:) config = DVLA::Herodotus.config do |c| c.display_pid = @display_pid c.main = false c.prefix_colour = @prefix_colour end HerodotusLogger.new(system_name, @logdev.dev, config: config) end |
#sync_correlation_ids ⇒ Object
Finds all instances of HerodotusLogger and updates their correlation_id and scenario_id to match that of the main HerodotusLogger.
51 52 53 54 55 56 57 58 59 |
# File 'lib/dvla/herodotus/herodotus_logger.rb', line 51 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 |