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: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

/.idea

Gemfile.lock
Gemfile.lock
/test-path
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.1] - 2025-11-21
- Fixed issue with output_path in create_logger, will now make a directory if it did not exist

## [2.3.0] - 2025-10-05
- Config block can now accept prefix colour options. Can be applied to the whole prefix or configure individual components.

Expand Down
10 changes: 10 additions & 0 deletions lib/dvla/herodotus.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'logger'
require 'fileutils'
require_relative 'herodotus/herodotus_logger'
require_relative 'herodotus/multi_writer'
require_relative 'herodotus/proc_writer'
Expand All @@ -25,9 +26,11 @@ def self.logger(system_name, config: self.config, output_path: nil)
private_class_method def self.create_logger(system_name, config, output_path)
if output_path
if output_path.is_a? String
ensure_directory_exists(output_path: output_path)
output_file = File.open(output_path, 'a')
return HerodotusLogger.new(system_name, MultiWriter.new(output_file, $stdout), config: config)
elsif output_path.is_a? Proc
ensure_directory_exists(output_path: output_path.call)
proc_writer = ProcWriter.new(output_path)
return HerodotusLogger.new(system_name, MultiWriter.new(proc_writer, $stdout), config: config)
else
Expand All @@ -36,5 +39,12 @@ def self.logger(system_name, config: self.config, output_path: nil)
end
HerodotusLogger.new(system_name, $stdout, config: config)
end

private_class_method def self.ensure_directory_exists(output_path:)
directory = File.split(output_path).first
unless File.directory?(directory)
FileUtils.mkdir_p(directory)
end
end
end
end
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.0'.freeze
VERSION = '2.3.1'.freeze
end
end
10 changes: 10 additions & 0 deletions spec/dvla/herodotus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
.to_stdout_from_any_process
end

it 'will create a directory for a string output_path if it does not exist' do
output_path = "test-path/#{rand(9999)}/log.txt"
expect { DVLA::Herodotus.logger('rspec', output_path: output_path) }.to_not raise_error
end

it 'will create a directory for a proc output_path if it does not exist' do
output_path = Proc.new { "test-path/#{rand(9999)}/log.txt" }
expect { DVLA::Herodotus.logger('rspec', output_path: output_path) }.to_not raise_error
end

it 'raises an error when an unexpected type is passed in as an output_path' do
unexpected_int = 123
expect { DVLA::Herodotus.logger('rspec', output_path: unexpected_int) }.to raise_error(ArgumentError, 'Unexpected output_path provided. Expecting either a string or a proc')
Expand Down