dvla-herodotus 2.2.1 → 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: 52b672b4b047bc03d1b603eb57ce54e8710d2f349531b0ed7ff586dfc74ec006
4
- data.tar.gz: 8fd5c49734e7293aa0f0331860843fd032d2728ca25b1d24747514e584b6a6b8
3
+ metadata.gz: a7ab39a7a9de4f2f315d5ce951f3a65ad947c04a889d140c8ab70dbdc64e76b3
4
+ data.tar.gz: c722a9497a06673983412e4f02dacb6daf073c1461f9eedad0b1f6e80b9ea04d
5
5
  SHA512:
6
- metadata.gz: e6a86fb9b81c961a73495e68068a7ea6b4b6c8aefd9c5e0b44855430b2738ddf829df971affc1a01b11a66a346b10648f62e4b538682c13efb8b8fa83ed080ac
7
- data.tar.gz: d409d7a8f18584ac281f780c9ed94b603ed3de3af4e3a81ea15fffcfa72a95944691fd28b7642acf669f3faf1f196aabbaf7fed6742b2add922a59415fd0d290
6
+ metadata.gz: 35163204de97ea6c7c44799e81bf45918dd9018de639f849a33bcdd5be83be246cf099f251b5588c9e8160c40f06448b6870819888d8cd6e997442b203f365bf
7
+ data.tar.gz: 5cee93b36fca277bae58494570f35250a29b9f2947c0c9cd794dfd20e38ea9850930fdb26a55cf324f6df1f14279cc4adc449e193c84b36b9cc1bfee9c28cb5b
data/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
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
+
4
7
  ## [2.2.1] - 2025-09-15
5
8
  - Fixed issue with ANSI exit codes breaking on string interpolation
6
9
  - Added strip_colour method to String which we now call when sending logs to file
data/README.md CHANGED
@@ -62,6 +62,26 @@ This would result in logs in the following format:
62
62
 
63
63
  `[SystemName CurrentDate CurrentTime CorrelationId PID] Level : -- Message`
64
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
+
65
85
  ### Syncing logs
66
86
 
67
87
  Herodotus allows you to Sync correlation_ids between instantiated HerodotusLogger objects.
@@ -82,6 +102,7 @@ You can call `new_scenario` with the identifier just before each scenario to cre
82
102
  logger.new_scenario('Scenario Id')
83
103
  ```
84
104
 
105
+ ---
85
106
  ### Strings
86
107
 
87
108
  Also included is a series of additional methods on `String` that allow you to modify the colour and style of logs.
@@ -94,14 +115,14 @@ You can stack multiple method calls to add additional styling and use string int
94
115
 
95
116
  #### Available String Methods
96
117
 
97
- | Type | Examples |
98
- |------|----------|
99
- | Text Styles | **bold** <span style="opacity:0.6">dim</span> *italic* <u>underline</u> |
100
- | Colors | <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> |
101
- | Bright Colors | <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> |
102
- | Background Colors | <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> |
103
- | Bright Background Colors | <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> |
104
- | Utility | strip_colour reverse_colour |
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 |
105
126
 
106
127
  #### To handle differences in spelling the following methods have been given aliases:
107
128
  | Alias | Original |
@@ -112,6 +133,8 @@ You can stack multiple method calls to add additional styling and use string int
112
133
  | reverse_color | reverse_colour |
113
134
  | strip_color | strip_colour |
114
135
 
136
+ ---
137
+
115
138
  ## Development
116
139
 
117
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 }
@@ -1,5 +1,5 @@
1
1
  module DVLA
2
2
  module Herodotus
3
- VERSION = '2.2.1'.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.1
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-15 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