dvla-herodotus 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: def32028eabb2e8b3320cd06f229d9b882c1a586dbf8b7cdeef6e69f4998fd9a
4
+ data.tar.gz: 0f9aadf85396b5585db242196f0f5562b9af755ac69901c967fd537a824e5f7c
5
+ SHA512:
6
+ metadata.gz: 9bf48e9d451a7f9f42fdc44f6af3dcb1844ce36ac82cafc6c8bec0596bda2159d14ba2b9b6aad6ebfaf54b220e53bd66e762e36df68a938c4c6a04b95ccdd1c8
7
+ data.tar.gz: d6566b93fc7289175e97b989c4948040be0152eae81997920912c722f26d5d34c24286b01c37ac49b961d28637bface0883b14867f2069db598665ede9affe7b
data/.idea/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
9
+
10
+ /.idea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # DVLA::Herodotus
2
+
3
+ A Gem that produces loggers that are pre-formatted into an agreed log format
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dvla-herodotus'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dvla-herodotus
20
+
21
+ ## Usage
22
+
23
+ ### Logger
24
+
25
+ You can get a logger by calling the following once Herodotus is installed:
26
+
27
+ ```ruby
28
+ logger = DVLA::Herodotus.logger
29
+ ```
30
+
31
+ 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:
32
+
33
+ `[CurrentDate CurrentTime CorrelationId] Level : -- Message`
34
+
35
+ Additionally, you can configure Herodotus in the following way to add a System Name and the Process Id to the output:
36
+
37
+ ```ruby
38
+ DVLA::Herodotus.configure do |config|
39
+ config.system_name = 'SystemName'
40
+ config.pid = true
41
+ end
42
+ ```
43
+
44
+ This would result in logs in the following format:
45
+
46
+ `[SystemName CurrentDate CurrentTime CorrelationId PID] Level : -- Message`
47
+
48
+ 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.
49
+
50
+ ```ruby
51
+ logger.info('String to log out', 'Scenario Id')
52
+ ```
53
+
54
+ 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.
55
+
56
+ ```ruby
57
+ logger.new_scenario('Scenario Id')
58
+ ```
59
+
60
+ 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:
61
+
62
+ ```ruby
63
+ DVLA::Herodotus.configure do |config|
64
+ config.merge = true
65
+ end
66
+ ```
67
+
68
+ 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`.
69
+
70
+ ### Strings
71
+
72
+ 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:
73
+
74
+ ```ruby
75
+ example_string = 'Multicoloured String'.blue.bg_red.bold
76
+ ```
77
+
78
+ | Method | Function |
79
+ |---------------|------------------------------------------------|
80
+ | blue | Sets the string's colour to blue |
81
+ | red | Sets the string's colour to red |
82
+ | green | Sets the string's colour to green |
83
+ | brown | Sets the string's colour to brown |
84
+ | blue | Sets the string's colour to blue |
85
+ | magenta | Sets the string's colour to magenta |
86
+ | cyan | Sets the string's colour to cyan |
87
+ | gray | Sets the string's colour to gray |
88
+ | bg_blue | Sets the string's background colour to blue |
89
+ | bg_red | Sets the string's background colour to red |
90
+ | bg_green | Sets the string's background colour to green |
91
+ | bg_brown | Sets the string's background colour to brown |
92
+ | bg_blue | Sets the string's background colour to blue |
93
+ | bg_magenta | Sets the string's background colour to magenta |
94
+ | bg_cyan | Sets the string's background colour to cyan |
95
+ | bg_gray | Sets the string's background colour to gray |
96
+ | bold | Sets the string to be bold |
97
+ | italic | Sets the string to be italic |
98
+ | underline | Sets the string to be underline |
99
+ | blink | Sets the string to blink |
100
+ | reverse_color | Reverses the colour of the string |
101
+
102
+ ## Development
103
+
104
+ Herodotus is very lightweight. Currently all code to generate a new logger can be found in `herodotus.rb` and the code for the logger is in `herodotus_logger.rb` so that is the best place to start with any modifications
@@ -0,0 +1,22 @@
1
+ require_relative 'lib/dvla/herodotus/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'dvla-herodotus'
5
+ spec.version = DVLA::Herodotus::VERSION
6
+ spec.authors = ['Driver and Vehicle Licensing Agency (DVLA)', 'George Bell']
7
+ spec.email = ['george.bell.contractor@dvla.gov.uk']
8
+
9
+ spec.summary = 'Provides a lightweight logger with a common format'
10
+ spec.required_ruby_version = Gem::Requirement.new('>= 3')
11
+
12
+ # Specify which files should be added to the gem when it is released.
13
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
14
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = %w[lib]
20
+
21
+ spec.add_development_dependency 'rspec', '~> 3.8'
22
+ end
@@ -0,0 +1,65 @@
1
+ require 'securerandom'
2
+
3
+ module DVLA
4
+ module Herodotus
5
+ class HerodotusLogger < Logger
6
+ attr_accessor :system_name, :requires_pid, :merge, :correlation_ids
7
+
8
+ def register_default_correlation_id
9
+ @correlation_ids = { default: SecureRandom.uuid[0, 8] }
10
+ reset_format
11
+ end
12
+
13
+ def new_scenario(scenario_id)
14
+ update_format(scenario_id)
15
+ merge_correlation_ids(new_scenario: scenario_id) if @merge
16
+ end
17
+
18
+ def merge_correlation_ids(new_scenario: nil)
19
+ ObjectSpace.each_object(DVLA::Herodotus::HerodotusLogger) do |logger|
20
+ 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
21
+ 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
22
+ logger.correlation_ids = self.correlation_ids
23
+ logger.new_scenario(new_scenario) unless new_scenario.nil?
24
+ end
25
+ end
26
+ end
27
+
28
+ %i[debug info warn error fatal].each do |log_level|
29
+ define_method log_level do |progname = nil, scenario_id = nil, &block|
30
+ if scenario_id == nil
31
+ super(progname, &block)
32
+ else
33
+ update_format(scenario_id)
34
+ super(progname, &block)
35
+ reset_format
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def update_format(scenario_id)
43
+ @correlation_ids[scenario_id] = SecureRandom.uuid[0, 8] unless @correlation_ids.key?(scenario_id)
44
+
45
+ self.formatter = proc do |severity, _datetime, _progname, msg|
46
+ "[#{@system_name}" \
47
+ "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} " \
48
+ "#{@correlation_ids[scenario_id]}" \
49
+ "#{' '.concat(Process.pid.to_s) if requires_pid}] " \
50
+ "#{severity} -- : #{msg}\n"
51
+ end
52
+ end
53
+
54
+ def reset_format
55
+ self.formatter = proc do |severity, _datetime, _progname, msg|
56
+ "[#{@system_name}" \
57
+ "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} " \
58
+ "#{@correlation_ids[:default]}" \
59
+ "#{' '.concat(Process.pid.to_s) if requires_pid}] " \
60
+ "#{severity} -- : #{msg}\n"
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,43 @@
1
+ class String
2
+ def black = "\e[30m#{self}\e[0m"
3
+
4
+ def red = "\e[31m#{self}\e[0m"
5
+
6
+ def green = "\e[32m#{self}\e[0m"
7
+
8
+ def brown = "\e[33m#{self}\e[0m"
9
+
10
+ def blue = "\e[34m#{self}\e[0m"
11
+
12
+ def magenta = "\e[35m#{self}\e[0m"
13
+
14
+ def cyan = "\e[36m#{self}\e[0m"
15
+
16
+ def gray = "\e[37m#{self}\e[0m"
17
+
18
+ def bg_black = "\e[40m#{self}\e[0m"
19
+
20
+ def bg_red = "\e[41m#{self}\e[0m"
21
+
22
+ def bg_green = "\e[42m#{self}\e[0m"
23
+
24
+ def bg_brown = "\e[43m#{self}\e[0m"
25
+
26
+ def bg_blue = "\e[44m#{self}\e[0m"
27
+
28
+ def bg_magenta = "\e[45m#{self}\e[0m"
29
+
30
+ def bg_cyan = "\e[46m#{self}\e[0m"
31
+
32
+ def bg_gray = "\e[47m#{self}\e[0m"
33
+
34
+ def bold = "\e[1m#{self}\e[22m"
35
+
36
+ def italic = "\e[3m#{self}\e[23m"
37
+
38
+ def underline = "\e[4m#{self}\e[24m"
39
+
40
+ def blink = "\e[5m#{self}\e[25m"
41
+
42
+ def reverse_color = "\e[7m#{self}\e[27m"
43
+ end
@@ -0,0 +1,5 @@
1
+ module DVLA
2
+ module Herodotus
3
+ VERSION = '1.0.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ require 'logger'
2
+ require_relative 'herodotus/string'
3
+ require_relative 'herodotus/herodotus_logger'
4
+
5
+ module DVLA
6
+ module Herodotus
7
+ CONFIG_ATTRIBUTES = %i(system_name pid merge).freeze
8
+
9
+ def self.configure
10
+ @config ||= Struct.new(*CONFIG_ATTRIBUTES).new
11
+ yield(@config) if block_given?
12
+ @config
13
+ end
14
+
15
+ def self.config
16
+ @config || configure
17
+ end
18
+
19
+ def self.logger
20
+ logger = HerodotusLogger.new($stdout)
21
+ logger.system_name = "#{config.system_name} " unless config.system_name.nil?
22
+ logger.requires_pid = config.pid
23
+ logger.merge = config.merge
24
+ logger.register_default_correlation_id
25
+ logger.merge_correlation_ids if config.merge
26
+ logger
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dvla-herodotus
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Driver and Vehicle Licensing Agency (DVLA)
8
+ - George Bell
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2023-05-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.8'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.8'
28
+ description:
29
+ email:
30
+ - george.bell.contractor@dvla.gov.uk
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".idea/.gitignore"
36
+ - Gemfile
37
+ - README.md
38
+ - dvla-herodotus.gemspec
39
+ - lib/dvla/herodotus.rb
40
+ - lib/dvla/herodotus/herodotus_logger.rb
41
+ - lib/dvla/herodotus/string.rb
42
+ - lib/dvla/herodotus/version.rb
43
+ homepage:
44
+ licenses: []
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.3.7
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Provides a lightweight logger with a common format
65
+ test_files: []