dvla-herodotus 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44f213f7b551a5dedbe3caaca66eafc7471b539eac4ec2271b3687e0be26d934
4
- data.tar.gz: 21b3bf02e8ddf43b4eb8accadd47a607a25013943869e8b3b03587e3157568fd
3
+ metadata.gz: 26e2843c0b2fa7985bccb48c2d24d5e66cf19ff9a1008d27347d705605a979f1
4
+ data.tar.gz: 817acfe4641c6ad9a0bbcf607ce8b3968b2f2d5eccbe3586bfefae1a46236817
5
5
  SHA512:
6
- metadata.gz: 22fed7800687b7acaf63195afd9c93fcab67914b6456ad8928f2cd4345b256f92ff91b7c22239e2c9bb65853b1dd976f6835647f25431c42e1335af8c541c6e3
7
- data.tar.gz: 72177da622343e613def838c5e195e0da15c8fccaf8dadcb3bf84ce164eab7b1c481e45c898ca5b7ad99004634ad4e58bd339a6788aca1780786121852ada22b
6
+ metadata.gz: afa99a580bb2f1082252c2dd44139b89d48d48625ee1223f7af6a93cc3ab68a1e65b7379e5646959ab37e772c2a72828b95eba75eb2c6cee75737dc42be85917
7
+ data.tar.gz: 1eab9992df8482e4218f59b3f926e2bfa8383f3c141f708f9fe5a0ef64576fc5eef2bc6eabcb7ef281ccf4e7e4e1eb2cc02e630919c744d0ac5330d16907479c
data/README.md CHANGED
@@ -28,12 +28,18 @@ You can get a logger by calling the following once Herodotus is installed:
28
28
  logger = DVLA::Herodotus.logger
29
29
  ```
30
30
 
31
- You can also provide the path to an output file, which will be logged to simultaneously with standard console logger
31
+ 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
32
 
33
33
  ```ruby
34
34
  logger = DVLA::Herodotus.logger(output_path: 'logs.txt')
35
35
  ```
36
36
 
37
+ 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
+ ```ruby
40
+ logger = DVLA::Herodotus.logger(output_path: -> { "#{@scenario}_log.txt" })
41
+ ```
42
+
37
43
  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:
38
44
 
39
45
  `[CurrentDate CurrentTime CorrelationId] Level : -- Message`
@@ -7,6 +7,7 @@ module DVLA
7
7
 
8
8
  def register_default_correlation_id
9
9
  @correlation_ids = { default: SecureRandom.uuid[0, 8] }
10
+ @current_scenario = :default
10
11
  reset_format
11
12
  end
12
13
 
@@ -28,9 +29,11 @@ module DVLA
28
29
  %i[debug info warn error fatal].each do |log_level|
29
30
  define_method log_level do |progname = nil, scenario_id = nil, &block|
30
31
  if scenario_id == nil
32
+ set_proc_writer_scenario
31
33
  super(progname, &block)
32
34
  else
33
35
  update_format(scenario_id)
36
+ set_proc_writer_scenario
34
37
  super(progname, &block)
35
38
  reset_format
36
39
  end
@@ -40,6 +43,7 @@ module DVLA
40
43
  private
41
44
 
42
45
  def update_format(scenario_id)
46
+ @current_scenario = scenario_id
43
47
  @correlation_ids[scenario_id] = SecureRandom.uuid[0, 8] unless @correlation_ids.key?(scenario_id)
44
48
 
45
49
  self.formatter = proc do |severity, _datetime, _progname, msg|
@@ -60,6 +64,15 @@ module DVLA
60
64
  "#{severity} -- : #{msg}\n"
61
65
  end
62
66
  end
67
+
68
+ def set_proc_writer_scenario
69
+ if @logdev.dev.is_a? DVLA::Herodotus::MultiWriter and @logdev.dev.targets.any? DVLA::Herodotus::ProcWriter
70
+ proc_writers = @logdev.dev.targets.select { |t| t.is_a? DVLA::Herodotus::ProcWriter }
71
+ proc_writers.each do |pr|
72
+ pr.scenario = @current_scenario
73
+ end
74
+ end
75
+ end
63
76
  end
64
77
  end
65
78
  end
@@ -1,6 +1,8 @@
1
1
  module DVLA
2
2
  module Herodotus
3
3
  class MultiWriter
4
+ attr_reader :targets
5
+
4
6
  def initialize(*targets)
5
7
  @targets = *targets
6
8
  end
@@ -0,0 +1,20 @@
1
+ module DVLA
2
+ module Herodotus
3
+ class ProcWriter
4
+ attr_accessor :scenario
5
+ def initialize(proc)
6
+ @proc = proc
7
+ end
8
+
9
+ def write(*args)
10
+ output_file = File.open(self.instance_exec(&@proc), 'a')
11
+ output_file.write(args[0])
12
+ output_file.close
13
+ end
14
+
15
+ def close
16
+ # Nothing to close but we want to maintain consistency with other ways Herodotus can output
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  module DVLA
2
2
  module Herodotus
3
- VERSION = '1.1.0'.freeze
3
+ VERSION = '1.2.0'.freeze
4
4
  end
5
5
  end
@@ -1,6 +1,7 @@
1
1
  require 'logger'
2
2
  require_relative 'herodotus/herodotus_logger'
3
3
  require_relative 'herodotus/multi_writer'
4
+ require_relative 'herodotus/proc_writer'
4
5
  require_relative 'herodotus/string'
5
6
 
6
7
  module DVLA
@@ -29,11 +30,17 @@ module DVLA
29
30
 
30
31
  private_class_method def self.create_logger(output_path)
31
32
  if output_path
32
- output_file = File.open(output_path, 'a+')
33
- HerodotusLogger.new(MultiWriter.new(output_file, $stdout))
34
- else
35
- HerodotusLogger.new($stdout)
33
+ if output_path.is_a? String
34
+ output_file = File.open(output_path, 'a')
35
+ return HerodotusLogger.new(MultiWriter.new(output_file, $stdout))
36
+ elsif output_path.is_a? Proc
37
+ proc_writer = ProcWriter.new(output_path)
38
+ return HerodotusLogger.new(MultiWriter.new(proc_writer, $stdout))
39
+ else
40
+ raise ArgumentError.new "Unexpected output_path provided. Expecting either a string or a proc"
41
+ end
36
42
  end
43
+ HerodotusLogger.new($stdout)
37
44
  end
38
45
  end
39
46
  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.1.0
4
+ version: 1.2.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: 2023-08-15 00:00:00.000000000 Z
12
+ date: 2024-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -42,6 +42,7 @@ files:
42
42
  - lib/dvla/herodotus.rb
43
43
  - lib/dvla/herodotus/herodotus_logger.rb
44
44
  - lib/dvla/herodotus/multi_writer.rb
45
+ - lib/dvla/herodotus/proc_writer.rb
45
46
  - lib/dvla/herodotus/string.rb
46
47
  - lib/dvla/herodotus/version.rb
47
48
  homepage: https://github.com/dvla/herodotus