dvla-herodotus 1.2.1 → 2.1.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 +4 -4
- data/.github/workflows/gem-build.yml +1 -1
- data/.github/workflows/gem-push.yml +3 -3
- data/.github/workflows/gem-test.yml +1 -1
- data/.ruby-version +1 -0
- data/README.md +53 -43
- data/bin/console +7 -0
- data/lib/dvla/herodotus/herodotus_logger.rb +54 -41
- data/lib/dvla/herodotus/string.rb +18 -0
- data/lib/dvla/herodotus/version.rb +1 -1
- data/lib/dvla/herodotus.rb +13 -19
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc15c4c23089f2d6e98ddff53324e32a3d576fc185cd5fd35ddff63a4a866651
|
4
|
+
data.tar.gz: 251dff6cefa66914f10fee24be7e18ef4f06831a20c90c7318f1719a21fbfa14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd63ffcf36a5b217ba467b0b3bdec19da6a40a2e0c6e2072ecc5bb5d3be3aaf65f6894903d3329ce516fed6eec3bc3a5980b1870d2ac9a068234422285a860ba
|
7
|
+
data.tar.gz: eff9d40bd3676ae15eaeb15ed8173bbebcf4f437b67c99598ec4d551720705bd350df0cea15bceda2e44be238df528d6d2b8d8aaf0fbf8f503ae7716085e7ae1
|
@@ -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
|
33
|
+
- name: Set up Ruby
|
34
34
|
uses: ruby/setup-ruby@v1
|
35
35
|
with:
|
36
|
-
ruby-version: 3.
|
36
|
+
ruby-version: 3.3.0
|
37
37
|
bundler-cache: true
|
38
38
|
|
39
39
|
- name: Publish to RubyGems
|
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
|
-
|
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.
|
51
|
-
config.
|
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
|
-
|
63
|
+
### Syncing logs
|
61
64
|
|
62
|
-
|
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
|
-
|
67
|
+
The HerodotusLogger flagged as `main` will be used as the source.
|
67
68
|
|
68
69
|
```ruby
|
69
|
-
|
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
|
-
|
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
|
-
|
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:
|
@@ -87,29 +88,38 @@ Also included is a series of additional methods on `String` that allow you to mo
|
|
87
88
|
example_string = 'Multicoloured String'.blue.bg_red.bold
|
88
89
|
```
|
89
90
|
|
90
|
-
| Method | Function
|
91
|
-
|
92
|
-
| blue | Sets the string's colour to blue
|
93
|
-
| red | Sets the string's colour to red
|
94
|
-
| green | Sets the string's colour to green
|
95
|
-
| brown | Sets the string's colour to brown
|
96
|
-
| blue | Sets the string's colour to blue
|
97
|
-
| magenta | Sets the string's colour to magenta
|
98
|
-
| cyan | Sets the string's colour to cyan
|
99
|
-
| gray | Sets the string's colour to gray
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
91
|
+
| Method | Function |
|
92
|
+
|---------------|--------------------------------------------------|
|
93
|
+
| blue | Sets the string's colour to blue |
|
94
|
+
| red | Sets the string's colour to red |
|
95
|
+
| green | Sets the string's colour to green |
|
96
|
+
| brown | Sets the string's colour to brown |
|
97
|
+
| blue | Sets the string's colour to blue |
|
98
|
+
| magenta | Sets the string's colour to magenta |
|
99
|
+
| cyan | Sets the string's colour to cyan |
|
100
|
+
| gray | Sets the string's colour to gray |
|
101
|
+
| grey | Sets the string's colour to grey (alias of gray) |
|
102
|
+
| bright_blue | Sets the string's colour to bright blue |
|
103
|
+
| bright_red | Sets the string's colour to bright red |
|
104
|
+
| bright_green | Sets the string's colour to bright green |
|
105
|
+
| bright_yellow | Sets the string's colour to bright yellow |
|
106
|
+
| bright_blue | Sets the string's colour to bright blue |
|
107
|
+
| bright_magenta| Sets the string's colour to bright magenta |
|
108
|
+
| bright_cyan | Sets the string's colour to bright cyan |
|
109
|
+
| white | Sets the string's colour to white |
|
110
|
+
| bg_blue | Sets the string's background colour to blue |
|
111
|
+
| bg_red | Sets the string's background colour to red |
|
112
|
+
| bg_green | Sets the string's background colour to green |
|
113
|
+
| bg_brown | Sets the string's background colour to brown |
|
114
|
+
| bg_blue | Sets the string's background colour to blue |
|
115
|
+
| bg_magenta | Sets the string's background colour to magenta |
|
116
|
+
| bg_cyan | Sets the string's background colour to cyan |
|
117
|
+
| bg_gray | Sets the string's background colour to gray |
|
118
|
+
| bold | Sets the string to be bold |
|
119
|
+
| italic | Sets the string to be italic |
|
120
|
+
| underline | Sets the string to be underline |
|
121
|
+
| blink | Sets the string to blink |
|
122
|
+
| reverse_color | Reverses the colour of the string |
|
113
123
|
|
114
124
|
## Development
|
115
125
|
|
data/bin/console
ADDED
@@ -3,73 +3,86 @@ require 'securerandom'
|
|
3
3
|
module DVLA
|
4
4
|
module Herodotus
|
5
5
|
class HerodotusLogger < Logger
|
6
|
-
attr_accessor :system_name, :
|
6
|
+
attr_accessor :system_name, :correlation_id, :main, :display_pid, :scenario_id
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
16
|
-
|
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
|
-
|
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 ==
|
22
|
-
logger.
|
23
|
-
logger.
|
24
|
-
logger.
|
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,
|
31
|
-
|
32
|
-
|
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
|
-
|
44
|
-
|
45
|
-
def
|
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
|
-
"#{@
|
53
|
-
"#{' '.concat(Process.pid.to_s) if
|
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
|
-
|
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 = @
|
85
|
+
pr.scenario = @scenario_id
|
73
86
|
end
|
74
87
|
end
|
75
88
|
end
|
@@ -15,6 +15,24 @@ class String
|
|
15
15
|
|
16
16
|
def gray = "\e[37m#{self}\e[0m"
|
17
17
|
|
18
|
+
alias grey gray
|
19
|
+
|
20
|
+
def bright_black = "\e[90m#{self}\e[0m"
|
21
|
+
|
22
|
+
def bright_red = "\e[91m#{self}\e[0m"
|
23
|
+
|
24
|
+
def bright_green = "\e[92m#{self}\e[0m"
|
25
|
+
|
26
|
+
def bright_yellow = "\e[93m#{self}\e[0m"
|
27
|
+
|
28
|
+
def bright_blue = "\e[94m#{self}\e[0m"
|
29
|
+
|
30
|
+
def bright_magenta = "\e[95m#{self}\e[0m"
|
31
|
+
|
32
|
+
def bright_cyan = "\e[96m#{self}\e[0m"
|
33
|
+
|
34
|
+
def white = "\e[97m#{self}\e[0m"
|
35
|
+
|
18
36
|
def bg_black = "\e[40m#{self}\e[0m"
|
19
37
|
|
20
38
|
def bg_red = "\e[41m#{self}\e[0m"
|
data/lib/dvla/herodotus.rb
CHANGED
@@ -6,41 +6,35 @@ require_relative 'herodotus/string'
|
|
6
6
|
|
7
7
|
module DVLA
|
8
8
|
module Herodotus
|
9
|
-
|
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
|
-
|
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
|
-
|
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:
|
4
|
+
version: 2.1.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-
|
12
|
+
date: 2024-12-04 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
|
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
|