From a82a22caf3d240ec3e0eee7e1780e3858067f9eb Mon Sep 17 00:00:00 2001 From: nialljames Date: Tue, 23 Dec 2025 10:28:35 +0000 Subject: [PATCH 1/2] implement spawn_child_logger method --- CHANGELOG.md | 3 ++ README.md | 16 +++++++ lib/dvla/herodotus/herodotus_logger.rb | 10 +++++ lib/dvla/herodotus/version.rb | 2 +- spec/dvla/herodotus/herodotus_logger_spec.rb | 47 ++++++++++++++++++++ 5 files changed, 77 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7b0f2e..63e58ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All notable changes to this project will be documented in this file. +## [2.3.2] - 2025-12-23 +- Add 'spawn_child_logger' method to allow other processes to inherit the current logger's configuration + ## [2.3.1] - 2025-11-21 - Fixed issue with output_path in create_logger, will now make a directory if it did not exist diff --git a/README.md b/README.md index 7460581..23c3c4e 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,22 @@ You can call `new_scenario` with the identifier just before each scenario to cre logger.new_scenario('Scenario Id') ``` +### spawn_child_logger method +You can call `spawn_child_logger` with a new system_name which will retain the current logger's config. +This enables you to pass loggers to other tools and maintain the same colourisation and output_path. + +```ruby +config = DVLA::Herodotus.config do |configuration| + configuration.display_pid = false + configuration.main = true + configuration.prefix_colour = { + overall: %w[blue bold], + } +end +LOG = DVLA::Herodotus.logger('', config:, output_path: -> { "log.txt" }) +NewTool.new(logger: LOG.spawn_child_logger(system_name: 'new gem')) +``` + --- ### Strings diff --git a/lib/dvla/herodotus/herodotus_logger.rb b/lib/dvla/herodotus/herodotus_logger.rb index 76d9634..298578b 100644 --- a/lib/dvla/herodotus/herodotus_logger.rb +++ b/lib/dvla/herodotus/herodotus_logger.rb @@ -58,6 +58,16 @@ def sync_correlation_ids end end + # Creates a new logger with a different system name but inherits config and output path + def spawn_child_logger(system_name:) + config = DVLA::Herodotus.config do |c| + c.display_pid = @display_pid + c.main = false + c.prefix_colour = @prefix_colour + end + HerodotusLogger.new(system_name, @logdev.dev, config: config) + end + %i[debug info warn error fatal].each do |log_level| define_method log_level do |progname = nil, &block| set_proc_writer_scenario diff --git a/lib/dvla/herodotus/version.rb b/lib/dvla/herodotus/version.rb index 9f1b699..300c6ec 100644 --- a/lib/dvla/herodotus/version.rb +++ b/lib/dvla/herodotus/version.rb @@ -1,5 +1,5 @@ module DVLA module Herodotus - VERSION = '2.3.1'.freeze + VERSION = '2.3.2'.freeze end end diff --git a/spec/dvla/herodotus/herodotus_logger_spec.rb b/spec/dvla/herodotus/herodotus_logger_spec.rb index a989279..ac7ce41 100644 --- a/spec/dvla/herodotus/herodotus_logger_spec.rb +++ b/spec/dvla/herodotus/herodotus_logger_spec.rb @@ -2,6 +2,11 @@ RSpec.describe DVLA::Herodotus::HerodotusLogger do let(:logger) { DVLA::Herodotus.logger('rspec') } + let(:logger_with_colour) do + logger = DVLA::Herodotus.logger('colour-rspec') + logger.instance_variable_set(:@prefix_colour, { system: %w[red] }) + logger + end after(:each) do DVLA::Herodotus.main_logger = nil @@ -286,4 +291,46 @@ expect { logger.info('test') }.to output(expected_output).to_stdout_from_any_process end end + + context '#spawn_child_logger' do + it 'should create a child logger with different system name' do + child_logger = logger.spawn_child_logger(system_name: 'child-rspec') + expect(child_logger.system_name).to eq('child-rspec') + end + + it 'should inherit display_pid config from parent' do + logger_with_pid = DVLA::Herodotus.logger('parent-rspec', config: DVLA::Herodotus.config { |c| c.display_pid = true }) + child_logger = logger_with_pid.spawn_child_logger(system_name: 'child-rspec') + expect(child_logger.display_pid).to eq(true) + end + + it 'should set main to false regardless of parent' do + main_logger = DVLA::Herodotus.logger('main-parent-rspec', config: DVLA::Herodotus.config { |c| c.main = true }) + child_logger = main_logger.spawn_child_logger(system_name: 'child-rspec') + expect(child_logger.main).to eq(false) + end + + it 'should share the same output device as parent' do + child_logger = logger.spawn_child_logger(system_name: 'child-rspec') + expect(child_logger.instance_variable_get(:@logdev).dev).to eq(logger.instance_variable_get(:@logdev).dev) + end + + it 'should inherit prefix_colour from parent when set' do + allow(Time).to receive(:now).and_return(Time.new(2022)) + allow(SecureRandom).to receive(:uuid).and_return('123e4567-e89b-12d3-a456-426614174000') + + child_logger = logger_with_colour.spawn_child_logger(system_name: 'child-rspec') + expect { child_logger.info('test') }.to output("[\e[31mchild-rspec\e[39m 2022-01-01 00:00:00 123e4567] INFO -- : test\n") + .to_stdout_from_any_process + end + + it 'should not set prefix_colour when parent has none' do + allow(Time).to receive(:now).and_return(Time.new(2022)) + allow(SecureRandom).to receive(:uuid).and_return('123e4567-e89b-12d3-a456-426614174000') + + child_logger = logger.spawn_child_logger(system_name: 'child-rspec') + expect { child_logger.info('test') }.to output("[child-rspec 2022-01-01 00:00:00 123e4567] INFO -- : test\n") + .to_stdout_from_any_process + end + end end From a737a2ff9f65156ae5ce575fbfabbf09333d293b Mon Sep 17 00:00:00 2001 From: nialljames Date: Tue, 23 Dec 2025 10:51:46 +0000 Subject: [PATCH 2/2] simplify spec test --- spec/dvla/herodotus/herodotus_logger_spec.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spec/dvla/herodotus/herodotus_logger_spec.rb b/spec/dvla/herodotus/herodotus_logger_spec.rb index ac7ce41..25f5659 100644 --- a/spec/dvla/herodotus/herodotus_logger_spec.rb +++ b/spec/dvla/herodotus/herodotus_logger_spec.rb @@ -311,8 +311,15 @@ end it 'should share the same output device as parent' do - child_logger = logger.spawn_child_logger(system_name: 'child-rspec') - expect(child_logger.instance_variable_get(:@logdev).dev).to eq(logger.instance_variable_get(:@logdev).dev) + output_device = StringIO.new + parent_logger = DVLA::Herodotus::HerodotusLogger.new('parent-rspec', output_device) + child_logger = parent_logger.spawn_child_logger(system_name: 'child-rspec') + + parent_logger.info('parent message') + child_logger.info('child message') + + expect(output_device.string).to include('parent message') + expect(output_device.string).to include('child message') end it 'should inherit prefix_colour from parent when set' do