Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('<system-name>', config:, output_path: -> { "log.txt" })
NewTool.new(logger: LOG.spawn_child_logger(system_name: 'new gem'))
```

---
### Strings

Expand Down
10 changes: 10 additions & 0 deletions lib/dvla/herodotus/herodotus_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/dvla/herodotus/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module DVLA
module Herodotus
VERSION = '2.3.1'.freeze
VERSION = '2.3.2'.freeze
end
end
54 changes: 54 additions & 0 deletions spec/dvla/herodotus/herodotus_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -286,4 +291,53 @@
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
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
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