dvla-herodotus 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e438adf38dbfed19a683688cbd116e9df9cd2381c64142ce1334eeea84ae681c
4
- data.tar.gz: 6c4fc10b6f03e57261971d56fb86570283020c48217bdb4cdbb7ed6920a2a894
3
+ metadata.gz: 6e3ef91707cd06f7a091f1648f34606d50e88feac527ba1addf96dfe9b9fd0cd
4
+ data.tar.gz: 96f8a2e8b39f34c3552e7ad7f5824c8facbd1af79b29a76fc9254f465d9c8d6a
5
5
  SHA512:
6
- metadata.gz: c4942f36ded7e851ba5e9a43254dd02ec02a3e2bf1df3580103502873fe16d2f28787960c0277417c415103411f17d8396fe4947f5feeead0ac408cbf928756c
7
- data.tar.gz: '095addb8fe673dcf789bec32357bad374aec2ffc6fc58e7331fbf20070dc22680630ca386ea5dc6b5a4fa2114e6ed701787747cd988d171788cf1a444bddf8c8'
6
+ metadata.gz: 357756d95d6774075440eba3f1cc7f7c38d1980586a0e0d82cefae36e959b6a3ba18406b77a87617ccc34cb320e0d3e9e3ded369b5f8db1bae62f97b30f2e809
7
+ data.tar.gz: e32fe8df4155456455e275601a7f885c8596b9c6700aa73e00169b67b63658fc49f3c8799e6d09a18048c0f4eff61ce17d4a55b37f09e15a102c08d9badccc89
@@ -9,7 +9,7 @@ jobs:
9
9
  runs-on: ubuntu-latest
10
10
  strategy:
11
11
  matrix:
12
- ruby-version: [ '3.0', '3.1', '3.2' ]
12
+ ruby-version: [ '3.0', '3.1', '3.2', head ]
13
13
 
14
14
  steps:
15
15
  - uses: actions/checkout@v2
@@ -9,7 +9,7 @@ jobs:
9
9
  runs-on: ubuntu-latest
10
10
  strategy:
11
11
  matrix:
12
- ruby-version: [ '3.0', '3.1', '3.2' ]
12
+ ruby-version: [ '3.0', '3.1', '3.2', head ]
13
13
 
14
14
  steps:
15
15
  - uses: actions/checkout@v2
@@ -30,10 +30,10 @@ jobs:
30
30
 
31
31
  steps:
32
32
  - uses: actions/checkout@v2
33
- - name: Set up Ruby 3.0.1
33
+ - name: Set up Ruby
34
34
  uses: ruby/setup-ruby@v1
35
35
  with:
36
- ruby-version: 3.1.2
36
+ ruby-version: 3.3.0
37
37
  bundler-cache: true
38
38
 
39
39
  - name: Publish to RubyGems
@@ -9,7 +9,7 @@ jobs:
9
9
  runs-on: ubuntu-latest
10
10
  strategy:
11
11
  matrix:
12
- ruby-version: [ '3.0', '3.1', '3.2' ]
12
+ ruby-version: [ '3.0', '3.1', '3.2', head ]
13
13
 
14
14
  steps:
15
15
  - uses: actions/checkout@v2
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.0
data/README.md CHANGED
@@ -18,6 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install dvla-herodotus
20
20
 
21
+ ---
21
22
  ## Usage
22
23
 
23
24
  ### Logger
@@ -25,60 +26,60 @@ Or install it yourself as:
25
26
  You can get a logger by calling the following once Herodotus is installed:
26
27
 
27
28
  ```ruby
28
- logger = DVLA::Herodotus.logger
29
+ logger = DVLA::Herodotus.logger('<system-name>')
29
30
  ```
30
31
 
31
32
  You can also log out to a file. If you want all the logs in a single file, provide a string of the path to that output file and it will be logged to simultaneously with standard console logger
32
33
 
33
34
  ```ruby
34
- logger = DVLA::Herodotus.logger(output_path: 'logs.txt')
35
+ logger = DVLA::Herodotus.logger('<system-name>', output_path: 'logs.txt')
35
36
  ```
36
37
 
37
38
  Alternatively, if you want each scenario to log out to a separate file based on the scenario name, pass in a lambda that returns a string that attempts to interpolate `@scenario`.
38
39
 
39
40
  ```ruby
40
- logger = DVLA::Herodotus.logger(output_path: -> { "#{@scenario}_log.txt" })
41
+ logger = DVLA::Herodotus.logger('<system-name>', output_path: -> { "#{@scenario}_log.txt" })
41
42
  ```
42
43
 
43
44
  This is a standard Ruby logger, so anything that would work on a logger acquired the traditional way will also work here, however it is formatted such that all logs will be output in the following format:
44
45
 
45
- `[CurrentDate CurrentTime CorrelationId] Level : -- Message`
46
46
 
47
- Additionally, you can configure Herodotus in the following way to add a System Name and the Process Id to the output:
47
+ `[SystemName CurrentDate CurrentTime CorrelationId] Level : -- Message`
48
+
49
+ ### Configuration
50
+ You can configure Herodotus in the following way to add a Process Id to the output:
48
51
 
49
52
  ```ruby
50
- DVLA::Herodotus.configure do |config|
51
- config.system_name = 'SystemName'
52
- config.pid = true
53
+ config = DVLA::Herodotus.config do |config|
54
+ config.display_pid = true
53
55
  end
56
+ logger = DVLA::Herodotus.logger('<system-name>', config: config)
54
57
  ```
55
58
 
56
59
  This would result in logs in the following format:
57
60
 
58
61
  `[SystemName CurrentDate CurrentTime CorrelationId PID] Level : -- Message`
59
62
 
60
- Additionally, if you wish to have different correlation ids based on the scenario that is being currently being run, you can pass a unique identifier for your scenario as part of the logging call, with each scenario having a unique correlation id.
63
+ ### Syncing logs
61
64
 
62
- ```ruby
63
- logger.info('String to log out', 'Scenario Id')
64
- ```
65
+ Herodotus allows you to Sync correlation_ids between instantiated HerodotusLogger objects.
65
66
 
66
- Alternatively, you can call `new_scenario` with the identifier just before each scenario to achieve the same result without having to pass the identifier around.
67
+ The HerodotusLogger flagged as `main` will be used as the source.
67
68
 
68
69
  ```ruby
69
- logger.new_scenario('Scenario Id')
70
+ config = DVLA::Herodotus.config do |config|
71
+ config.main = true
72
+ end
73
+ main_logger = DVLA::Herodotus.logger('<system-name>', config: config)
70
74
  ```
71
75
 
72
- Finally, you can set Herodotus up to integrate with any other instances of Herodotus that are loaded indirectly into your application, for example within a gem you use. To take advantage of this, when configuring Herodotus within your project, ensure that you set its `merge` value to true, as below:
76
+ ### new_scenario method
77
+ You can call `new_scenario` with the identifier just before each scenario to create a unique correlation_id per scenario.
73
78
 
74
79
  ```ruby
75
- DVLA::Herodotus.configure do |config|
76
- config.merge = true
77
- end
80
+ logger.new_scenario('Scenario Id')
78
81
  ```
79
82
 
80
- This will cause your correlation ids to be shared out with all the loggers that exist outside of our direct control. The instance of the Herodotus that will take precedence will be the last one to be loaded, which should be the one you are creating with `DVLA::Herodotus.logger`.
81
-
82
83
  ### Strings
83
84
 
84
85
  Also included is a series of additional methods on `String` that allow you to modify the colour and style of logs. As these exist on `String`, you can call them on any string such as:
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'dvla/herodotus'
5
+ require 'irb'
6
+
7
+ IRB.start(__FILE__)
@@ -3,73 +3,86 @@ require 'securerandom'
3
3
  module DVLA
4
4
  module Herodotus
5
5
  class HerodotusLogger < Logger
6
- attr_accessor :system_name, :requires_pid, :merge, :correlation_ids
6
+ attr_accessor :system_name, :correlation_id, :main, :display_pid, :scenario_id
7
7
 
8
- def register_default_correlation_id
9
- @correlation_ids = { default: SecureRandom.uuid[0, 8] }
10
- @current_scenario = :default
11
- reset_format
8
+ # Initializes the logger
9
+ # Sets a default correlation_id and creates the formatter
10
+ # Syncs all instances of the HerodotusLogger when the main flag is present
11
+ # Any subsequent loggers will also be synced
12
+ def initialize(system_name, *args, config: DVLA::Herodotus.config, **kwargs)
13
+ super(*args, **kwargs)
14
+
15
+ @system_name = system_name
16
+ @main = config[:main]
17
+ @display_pid = config[:display_pid]
18
+
19
+ @correlation_id = SecureRandom.uuid[0, 8]
20
+ set_formatter
21
+
22
+ if DVLA::Herodotus.main_logger && @main
23
+ warn("Main logger already set: '#{DVLA::Herodotus.main_logger.system_name}'. This will be overwritten by '#{system_name}'")
24
+ end
25
+
26
+ DVLA::Herodotus.main_logger = self if @main
27
+ sync_correlation_ids if DVLA::Herodotus.main_logger
12
28
  end
13
29
 
30
+ # Creates a new correlation_id and re-creates the formatter per scenario.
31
+ # If this method is called on an instance of HerodotusLogger not flagged as main
32
+ # the correlation_id of the main logger will be updated and all logger's correlation_ids re-synced.
14
33
  def new_scenario(scenario_id)
15
- update_format(scenario_id)
16
- merge_correlation_ids(new_scenario: scenario_id) if @merge
34
+ @scenario_id = scenario_id
35
+ @correlation_id = SecureRandom.uuid[0, 8]
36
+
37
+ if DVLA::Herodotus.main_logger && self != DVLA::Herodotus.main_logger
38
+ warn('You are calling new_scenario on a non-main logger.')
39
+
40
+ DVLA::Herodotus.main_logger.correlation_id = @correlation_id
41
+ DVLA::Herodotus.main_logger.scenario_id = @scenario_id
42
+ end
43
+
44
+ set_formatter
45
+ sync_correlation_ids if DVLA::Herodotus.main_logger
17
46
  end
18
47
 
19
- def merge_correlation_ids(new_scenario: nil)
48
+ # Finds all instances of HerodotusLogger and updates their correlation_id and scenario_id
49
+ # to match that of the main HerodotusLogger.
50
+ def sync_correlation_ids
20
51
  ObjectSpace.each_object(DVLA::Herodotus::HerodotusLogger) do |logger|
21
- unless logger == self # This copies the correlation ids this logger has over to all other loggers and (assuming a new scenario has just been switched to) updates the those loggers to the current scenario
22
- logger.merge = false if logger.merge #Stops the other logger from trying to propagate its correlation ids to all other loggers, otherwise this code ends up in an infinite loop
23
- logger.correlation_ids = self.correlation_ids
24
- logger.new_scenario(new_scenario) unless new_scenario.nil?
52
+ unless logger == DVLA::Herodotus.main_logger
53
+ logger.correlation_id = DVLA::Herodotus.main_logger.correlation_id
54
+ logger.scenario_id = DVLA::Herodotus.main_logger.scenario_id
55
+ logger.set_formatter
25
56
  end
26
57
  end
27
58
  end
28
59
 
29
60
  %i[debug info warn error fatal].each do |log_level|
30
- define_method log_level do |progname = nil, scenario_id = nil, &block|
31
- if scenario_id == nil
32
- set_proc_writer_scenario
33
- super(progname, &block)
34
- else
35
- update_format(scenario_id)
36
- set_proc_writer_scenario
37
- super(progname, &block)
38
- reset_format
39
- end
61
+ define_method log_level do |progname = nil, &block|
62
+ set_proc_writer_scenario
63
+ super(progname, &block)
40
64
  end
41
65
  end
42
66
 
43
- private
44
-
45
- def update_format(scenario_id)
46
- @current_scenario = scenario_id
47
- @correlation_ids[scenario_id] = SecureRandom.uuid[0, 8] unless @correlation_ids.key?(scenario_id)
48
-
67
+ # Sets the format of the log.
68
+ # Needs to be called each time correlation_id is changed after initialization in-order for the changes to take affect.
69
+ def set_formatter
49
70
  self.formatter = proc do |severity, _datetime, _progname, msg|
50
- "[#{@system_name}" \
71
+ "[#{@system_name} " \
51
72
  "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} " \
52
- "#{@correlation_ids[scenario_id]}" \
53
- "#{' '.concat(Process.pid.to_s) if requires_pid}] " \
73
+ "#{@correlation_id}" \
74
+ "#{' '.concat(Process.pid.to_s) if @display_pid}] " \
54
75
  "#{severity} -- : #{msg}\n"
55
76
  end
56
77
  end
57
78
 
58
- def reset_format
59
- self.formatter = proc do |severity, _datetime, _progname, msg|
60
- "[#{@system_name}" \
61
- "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} " \
62
- "#{@correlation_ids[:default]}" \
63
- "#{' '.concat(Process.pid.to_s) if requires_pid}] " \
64
- "#{severity} -- : #{msg}\n"
65
- end
66
- end
79
+ private
67
80
 
68
81
  def set_proc_writer_scenario
69
82
  if @logdev.dev.is_a?(DVLA::Herodotus::MultiWriter) && @logdev.dev.targets.any?(DVLA::Herodotus::ProcWriter)
70
83
  proc_writers = @logdev.dev.targets.select { |t| t.is_a? DVLA::Herodotus::ProcWriter }
71
84
  proc_writers.each do |pr|
72
- pr.scenario = @current_scenario
85
+ pr.scenario = @scenario_id
73
86
  end
74
87
  end
75
88
  end
@@ -1,5 +1,5 @@
1
1
  module DVLA
2
2
  module Herodotus
3
- VERSION = '1.2.1'.freeze
3
+ VERSION = '2.0.0'.freeze
4
4
  end
5
5
  end
@@ -6,41 +6,35 @@ require_relative 'herodotus/string'
6
6
 
7
7
  module DVLA
8
8
  module Herodotus
9
- CONFIG_ATTRIBUTES = %i(system_name pid merge).freeze
10
-
11
- def self.configure
12
- @config ||= Struct.new(*CONFIG_ATTRIBUTES).new
13
- yield(@config) if block_given?
14
- @config
9
+ class << self
10
+ attr_accessor :main_logger
15
11
  end
16
12
 
13
+ CONFIG_ATTRIBUTES = %i[display_pid main].freeze
14
+
17
15
  def self.config
18
- @config || configure
16
+ config ||= Struct.new(*CONFIG_ATTRIBUTES, keyword_init: true).new
17
+ yield(config) if block_given?
18
+ config
19
19
  end
20
20
 
21
- def self.logger(output_path: nil)
22
- logger = create_logger(output_path)
23
- logger.system_name = "#{config.system_name} " unless config.system_name.nil?
24
- logger.requires_pid = config.pid
25
- logger.merge = config.merge
26
- logger.register_default_correlation_id
27
- logger.merge_correlation_ids if config.merge
28
- logger
21
+ def self.logger(system_name, config: self.config, output_path: nil)
22
+ create_logger(system_name, config, output_path)
29
23
  end
30
24
 
31
- private_class_method def self.create_logger(output_path)
25
+ private_class_method def self.create_logger(system_name, config, output_path)
32
26
  if output_path
33
27
  if output_path.is_a? String
34
28
  output_file = File.open(output_path, 'a')
35
- return HerodotusLogger.new(MultiWriter.new(output_file, $stdout))
29
+ return HerodotusLogger.new(system_name, MultiWriter.new(output_file, $stdout), config: config)
36
30
  elsif output_path.is_a? Proc
37
31
  proc_writer = ProcWriter.new(output_path)
38
- return HerodotusLogger.new(MultiWriter.new(proc_writer, $stdout))
32
+ return HerodotusLogger.new(system_name, MultiWriter.new(proc_writer, $stdout), config: config)
39
33
  else
40
34
  raise ArgumentError.new 'Unexpected output_path provided. Expecting either a string or a proc'
41
35
  end
42
36
  end
43
- HerodotusLogger.new($stdout)
37
+ HerodotusLogger.new(system_name, $stdout, config: config)
44
38
  end
45
39
  end
46
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dvla-herodotus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Driver and Vehicle Licensing Agency (DVLA)
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-02-09 00:00:00.000000000 Z
12
+ date: 2024-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dvla-lint
@@ -51,9 +51,11 @@ files:
51
51
  - ".github/workflows/gem-test.yml"
52
52
  - ".gitignore"
53
53
  - ".rubocop.yml"
54
+ - ".ruby-version"
54
55
  - Gemfile
55
56
  - LICENSE
56
57
  - README.md
58
+ - bin/console
57
59
  - dvla-herodotus.gemspec
58
60
  - lib/dvla/herodotus.rb
59
61
  - lib/dvla/herodotus/herodotus_logger.rb
@@ -80,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
82
  - !ruby/object:Gem::Version
81
83
  version: '0'
82
84
  requirements: []
83
- rubygems_version: 3.3.7
85
+ rubygems_version: 3.5.3
84
86
  signing_key:
85
87
  specification_version: 4
86
88
  summary: Provides a lightweight logger with a common format