dvla-herodotus 2.2.0 → 2.3.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: 2eb770ebff3f1d698f1498ae9d09f6ce70735fe4a0b4e8fb48047db6fffa9497
4
- data.tar.gz: 3b32cbac5261b3f04831b455f0bdc1e12d7579ba3006b32fedf7a0214b6c1219
3
+ metadata.gz: a7ab39a7a9de4f2f315d5ce951f3a65ad947c04a889d140c8ab70dbdc64e76b3
4
+ data.tar.gz: c722a9497a06673983412e4f02dacb6daf073c1461f9eedad0b1f6e80b9ea04d
5
5
  SHA512:
6
- metadata.gz: e79ed58e9d0b3bbce41cc04c61ba6ffac86faee95ef257bc1be5f38c14cd9c2606c3452b13fb4f2aae5411137827aa29e70e4a2b038b8f8bf18f74dcc4f95793
7
- data.tar.gz: 9fceaf98cfc77253cf2f772367698486a250ca3659d4b5d0399ab7d18b4b95274951c1e7cda5820e3f8019e8941f34f1554208bbc5d0fdda85bd3a5ffa085547
6
+ metadata.gz: 35163204de97ea6c7c44799e81bf45918dd9018de639f849a33bcdd5be83be246cf099f251b5588c9e8160c40f06448b6870819888d8cd6e997442b203f365bf
7
+ data.tar.gz: 5cee93b36fca277bae58494570f35250a29b9f2947c0c9cd794dfd20e38ea9850930fdb26a55cf324f6df1f14279cc4adc449e193c84b36b9cc1bfee9c28cb5b
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## [2.3.0] - 2025-10-05
5
+ - Config block can now accept prefix colour options. Can be applied to the whole prefix or configure individual components.
6
+
7
+ ## [2.2.1] - 2025-09-15
8
+ - Fixed issue with ANSI exit codes breaking on string interpolation
9
+ - Added strip_colour method to String which we now call when sending logs to file
10
+ - Added colours/styles: yellow, bg_yellow, bg_white, bg_bright_red, bg_bright_green, bg_bright_blue, bg_bright_magenta, bg_bright_cyan, dim
11
+ - Removed colours/styles: bright_black (that's just gray), bright_yellow (that's just yellow), blink (inconsistent/unsupported)
12
+ - Added a changelog
13
+
14
+
15
+ ## [2.2.0] - 2025-09-08
16
+ - Added colourise method to handle colour exist codes
17
+ - Added alias for colour/color
18
+ - Added alias for brown/yellow
data/README.md CHANGED
@@ -29,7 +29,9 @@ You can get a logger by calling the following once Herodotus is installed:
29
29
  logger = DVLA::Herodotus.logger('<system-name>')
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
+ 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.
33
+
34
+ **Note:** Log messages are stripped of colour codes before being saved to file.
33
35
 
34
36
  ```ruby
35
37
  logger = DVLA::Herodotus.logger('<system-name>', output_path: 'logs.txt')
@@ -60,6 +62,26 @@ This would result in logs in the following format:
60
62
 
61
63
  `[SystemName CurrentDate CurrentTime CorrelationId PID] Level : -- Message`
62
64
 
65
+ #### Prefix Colourisation
66
+ You can colourise different parts of the log prefix by providing a hash to style each component. It accepts strings, symbols or arrays of either:
67
+
68
+ ```ruby
69
+ config = DVLA::Herodotus.config do |config|
70
+ config.prefix_colour = {
71
+ system: %w[blue bold],
72
+ date: 'green',
73
+ time: :yellow,
74
+ correlation: %w[magenta italic],
75
+ pid: %w[cyan],
76
+ level: %i[red bold],
77
+ separator: %w[white],
78
+ overall: %w[underline]
79
+ }
80
+ end
81
+ ```
82
+ Each key is optional, and you can simply use the `overall` key to style the whole prefix.
83
+ ---
84
+
63
85
  ### Syncing logs
64
86
 
65
87
  Herodotus allows you to Sync correlation_ids between instantiated HerodotusLogger objects.
@@ -80,47 +102,39 @@ You can call `new_scenario` with the identifier just before each scenario to cre
80
102
  logger.new_scenario('Scenario Id')
81
103
  ```
82
104
 
105
+ ---
83
106
  ### Strings
84
107
 
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:
108
+ Also included is a series of additional methods on `String` that allow you to modify the colour and style of logs.
109
+ You can stack multiple method calls to add additional styling and use string interpolation to style different parts of the string
110
+
86
111
 
87
112
  ```ruby
88
- example_string = 'Multicoloured String'.blue.bg_red.bold
113
+ example_string = "#{'H'.red}#{'E'.bright_red}#{'R'.yellow}#{'O'.green}#{'D'.blue}#{'O'.bright_blue}#{'T'.magenta}#{'U'.bright_magenta}#{'S'.cyan}".bold.reverse_colour
89
114
  ```
90
115
 
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 |
116
+ #### Available String Methods
117
+
118
+ | Type | Examples |
119
+ |---------------------------|----------|
120
+ | Text Styles | **bold** <span style="opacity:0.6">dim</span> *italic* <u>underline</u> |
121
+ | Colours | <span style="color:black">black</span> <span style="color:red">red</span> <span style="color:green">green</span> <span style="color:#B8860B">brown</span> <span style="color:#ffff00">yellow</span> <span style="color:blue">blue</span> <span style="color:magenta">magenta</span> <span style="color:cyan">cyan</span> <span style="color:grey">gray</span> <span style="color:white">white</span> |
122
+ | Bright Colours | <span style="color:#ff5555">bright_red</span> <span style="color:#55ff55">bright_green</span> <span style="color:#5555ff">bright_blue</span> <span style="color:#ff55ff">bright_magenta</span> <span style="color:#55ffff">bright_cyan</span> |
123
+ | Background Colours | <span style="background:black;color:white">bg_black</span> <span style="background:red;color:white">bg_red</span> <span style="background:green;color:white">bg_green</span> <span style="background:#B8860B;color:white">bg_brown</span> <span style="background:#ffff00;color:black">bg_yellow</span> <span style="background:blue;color:white">bg_blue</span> <span style="background:magenta;color:white">bg_magenta</span> <span style="background:cyan;color:black">bg_cyan</span> <span style="background:grey;color:white">bg_gray</span> <span style="background:white;color:black">bg_white</span> |
124
+ | Bright Background Colours | <span style="background:#ff5555;color:white">bg_bright_red</span> <span style="background:#55ff55;color:black">bg_bright_green</span> <span style="background:#5555ff;color:white">bg_bright_blue</span> <span style="background:#ff55ff;color:white">bg_bright_magenta</span> <span style="background:#55ffff;color:black">bg_bright_cyan</span> |
125
+ | Utility | strip_colour reverse_colour |
126
+
127
+ #### To handle differences in spelling the following methods have been given aliases:
128
+ | Alias | Original |
129
+ |---------------|----------------|
130
+ | bg_grey | bg_gray |
131
+ | colorize | colourise |
132
+ | grey | gray |
133
+ | reverse_color | reverse_colour |
134
+ | strip_color | strip_colour |
135
+
136
+ ---
123
137
 
124
138
  ## Development
125
139
 
126
- 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
140
+ 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
@@ -3,7 +3,7 @@ require 'securerandom'
3
3
  module DVLA
4
4
  module Herodotus
5
5
  class HerodotusLogger < Logger
6
- attr_accessor :system_name, :correlation_id, :main, :display_pid, :scenario_id
6
+ attr_accessor :system_name, :correlation_id, :main, :display_pid, :scenario_id, :prefix_colour
7
7
 
8
8
  # Initializes the logger
9
9
  # Sets a default correlation_id and creates the formatter
@@ -15,7 +15,8 @@ module DVLA
15
15
  @system_name = system_name
16
16
  @main = config[:main]
17
17
  @display_pid = config[:display_pid]
18
-
18
+ @prefix_colour = config[:prefix_colour] || {}
19
+ validate_colour_config if @prefix_colour.any?
19
20
  @correlation_id = SecureRandom.uuid[0, 8]
20
21
  set_formatter
21
22
 
@@ -65,19 +66,75 @@ module DVLA
65
66
  end
66
67
 
67
68
  # 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
+ # Needs to be called each time correlation_id is changed after initialization in-order for the changes to take effect.
69
70
  def set_formatter
70
71
  self.formatter = proc do |severity, _datetime, _progname, msg|
71
- "[#{@system_name} " \
72
- "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} " \
73
- "#{@correlation_id}" \
74
- "#{' '.concat(Process.pid.to_s) if @display_pid}] " \
75
- "#{severity} -- : #{msg}\n"
72
+ now = Time.now
73
+ components = {
74
+ system: @system_name,
75
+ date: now.strftime('%Y-%m-%d'),
76
+ time: now.strftime('%H:%M:%S'),
77
+ correlation: @correlation_id,
78
+ pid: @display_pid ? Process.pid.to_s : nil,
79
+ level: severity,
80
+ separator: '-- :',
81
+ }
82
+
83
+ prefix = @prefix_colour.any? ? build_prefix_with_colour(components) : build_prefix(components)
84
+ "#{prefix}#{msg}\n"
76
85
  end
77
86
  end
78
87
 
79
88
  private
80
89
 
90
+ VALID_COLOUR_METHODS = %i[
91
+ white black red green brown yellow blue magenta cyan gray grey
92
+ bright_red bright_green bright_blue bright_magenta bright_cyan
93
+ bg_black bg_red bg_green bg_brown bg_yellow bg_blue bg_magenta bg_cyan bg_gray bg_grey bg_white
94
+ bg_bright_red bg_bright_green bg_bright_blue bg_bright_magenta bg_bright_cyan
95
+ bold dim italic underline reverse_colour reverse_color
96
+ ].freeze
97
+
98
+ VALID_PREFIX_KEYS = %i[system date time correlation pid level separator overall].freeze
99
+
100
+ def validate_colour_config
101
+ raise ArgumentError, 'Invalid prefix colour config' unless @prefix_colour.is_a?(Hash) && @prefix_colour.keys.all? { |key| VALID_PREFIX_KEYS.include?(key) }
102
+
103
+ @prefix_colour.each_value do |value|
104
+ raise ArgumentError, 'Colour values must be strings or symbols' unless valid_colour_type?(value)
105
+ raise ArgumentError, 'Invalid colours in prefix colour config' unless Array(value).map(&:to_sym).all? { |c| VALID_COLOUR_METHODS.include?(c) }
106
+ end
107
+ end
108
+
109
+ def valid_colour_type?(value)
110
+ value.is_a?(String) || value.is_a?(Symbol) || (value.is_a?(Array) && value.all? { |v| v.is_a?(String) || v.is_a?(Symbol) })
111
+ end
112
+
113
+ def apply_colours(text, colour_spec)
114
+ return text unless colour_spec
115
+
116
+ colour_spec.reduce(text) { |str, method| str.public_send(method) }
117
+ end
118
+
119
+ def build_prefix_with_colour(components)
120
+ system = apply_colours(components[:system], @prefix_colour[:system])
121
+ date = apply_colours(components[:date], @prefix_colour[:date])
122
+ time = apply_colours(components[:time], @prefix_colour[:time])
123
+ correlation = apply_colours(components[:correlation], @prefix_colour[:correlation])
124
+ pid = components[:pid] && apply_colours(components[:pid], @prefix_colour[:pid])
125
+ level = apply_colours(components[:level], @prefix_colour[:level])
126
+ separator = apply_colours(components[:separator], @prefix_colour[:separator])
127
+
128
+ bracket_content = [system, date, time, correlation, pid].compact.join(' ')
129
+ result = "[#{bracket_content}] #{level} #{separator} "
130
+ apply_colours(result, @prefix_colour[:overall]) || result
131
+ end
132
+
133
+ def build_prefix(components)
134
+ bracket_content = [components[:system], components[:date], components[:time], components[:correlation], components[:pid]].compact.join(' ')
135
+ "[#{bracket_content}] #{components[:level]} #{components[:separator]} "
136
+ end
137
+
81
138
  def set_proc_writer_scenario
82
139
  if @logdev.dev.is_a?(DVLA::Herodotus::MultiWriter) && @logdev.dev.targets.any?(DVLA::Herodotus::ProcWriter)
83
140
  proc_writers = @logdev.dev.targets.select { |t| t.is_a? DVLA::Herodotus::ProcWriter }
@@ -8,7 +8,14 @@ module DVLA
8
8
  end
9
9
 
10
10
  def write(*args)
11
- @targets.each { |t| t.write(*args) }
11
+ @targets.each do |target|
12
+ # If we're writing to a file we remove the colour
13
+ if target != $stdout && args[0].respond_to?(:strip_colour)
14
+ target.write(args[0].strip_colour, *args[1..])
15
+ else
16
+ target.write(*args)
17
+ end
18
+ end
12
19
  end
13
20
 
14
21
  def close
@@ -1,43 +1,91 @@
1
1
  class String
2
2
  def colourise(code, reset_code = 39)
3
- "\e[#{code}m#{self}\e[#{reset_code}m"
3
+ # Making sure we align the correct reset codes
4
+ if self.include?("\e[#{reset_code}m")
5
+ result = self.gsub("\e[#{reset_code}m", "\e[#{reset_code}m\e[#{code}m")
6
+ "\e[#{code}m#{result}\e[#{reset_code}m"
7
+ else
8
+ "\e[#{code}m#{self}\e[#{reset_code}m"
9
+ end
4
10
  end
5
- alias colorize colourise
11
+
12
+ def white = colourise(97)
6
13
 
7
14
  def black = colourise(30)
15
+
8
16
  def red = colourise(31)
17
+
9
18
  def green = colourise(32)
19
+
10
20
  def brown = colourise(33)
11
- alias yellow brown
21
+
22
+ def yellow = colourise(93)
12
23
 
13
24
  def blue = colourise(34)
25
+
14
26
  def magenta = colourise(35)
27
+
15
28
  def cyan = colourise(36)
29
+
16
30
  def gray = colourise(37)
17
- alias grey gray
18
31
 
19
- def bright_black = colourise(90)
20
32
  def bright_red = colourise(91)
33
+
21
34
  def bright_green = colourise(92)
22
- def bright_yellow = colourise(93)
35
+
23
36
  def bright_blue = colourise(94)
37
+
24
38
  def bright_magenta = colourise(95)
39
+
25
40
  def bright_cyan = colourise(96)
26
- def white = colourise(97)
27
41
 
28
42
  def bg_black = colourise(40, 49)
43
+
29
44
  def bg_red = colourise(41, 49)
45
+
30
46
  def bg_green = colourise(42, 49)
47
+
31
48
  def bg_brown = colourise(43, 49)
49
+
50
+ def bg_yellow = colourise(103, 49)
51
+
32
52
  def bg_blue = colourise(44, 49)
53
+
33
54
  def bg_magenta = colourise(45, 49)
55
+
34
56
  def bg_cyan = colourise(46, 49)
57
+
35
58
  def bg_gray = colourise(47, 49)
36
59
 
60
+ def bg_white = colourise(107, 49)
61
+
62
+ def bg_bright_red = colourise(101, 49)
63
+
64
+ def bg_bright_green = colourise(102, 49)
65
+
66
+ def bg_bright_blue = colourise(104, 49)
67
+
68
+ def bg_bright_magenta = colourise(105, 49)
69
+
70
+ def bg_bright_cyan = colourise(106, 49)
71
+
37
72
  def bold = colourise(1, 22)
73
+
74
+ def dim = colourise(2, 22)
75
+
38
76
  def italic = colourise(3, 23)
77
+
39
78
  def underline = colourise(4, 24)
40
- def blink = colourise(5, 25)
41
- def reverse_color = colourise(7, 27)
42
- alias reverse_colour reverse_color
79
+
80
+ def reverse_colour = colourise(7, 27)
81
+
82
+ def strip_colour
83
+ gsub(/\e\[[0-9;]*m/, '')
84
+ end
85
+
86
+ alias bg_grey bg_gray
87
+ alias colorize colourise
88
+ alias grey gray
89
+ alias reverse_color reverse_colour
90
+ alias strip_color strip_colour
43
91
  end
@@ -1,5 +1,5 @@
1
1
  module DVLA
2
2
  module Herodotus
3
- VERSION = '2.2.0'.freeze
3
+ VERSION = '2.3.0'.freeze
4
4
  end
5
5
  end
@@ -10,7 +10,7 @@ module DVLA
10
10
  attr_accessor :main_logger
11
11
  end
12
12
 
13
- CONFIG_ATTRIBUTES = %i[display_pid main].freeze
13
+ CONFIG_ATTRIBUTES = %i[display_pid main prefix_colour].freeze
14
14
 
15
15
  def self.config
16
16
  config ||= Struct.new(*CONFIG_ATTRIBUTES, keyword_init: true).new
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: 2.2.0
4
+ version: 2.3.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: 2025-09-08 00:00:00.000000000 Z
12
+ date: 2025-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dvla-lint
@@ -52,6 +52,7 @@ files:
52
52
  - ".gitignore"
53
53
  - ".rubocop.yml"
54
54
  - ".ruby-version"
55
+ - CHANGELOG.md
55
56
  - Gemfile
56
57
  - LICENSE
57
58
  - README.md