diff --git a/.gitignore b/.gitignore index 98518ca..94d2237 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ /.idea -Gemfile.lock \ No newline at end of file +Gemfile.lock +/test-path \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index e9cdc0b..a7b0f2e 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.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. diff --git a/lib/dvla/herodotus.rb b/lib/dvla/herodotus.rb index 33c9d28..95a91a8 100644 --- a/lib/dvla/herodotus.rb +++ b/lib/dvla/herodotus.rb @@ -1,4 +1,5 @@ require 'logger' +require 'fileutils' require_relative 'herodotus/herodotus_logger' require_relative 'herodotus/multi_writer' require_relative 'herodotus/proc_writer' @@ -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 @@ -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 diff --git a/lib/dvla/herodotus/version.rb b/lib/dvla/herodotus/version.rb index 1b0197a..9f1b699 100644 --- a/lib/dvla/herodotus/version.rb +++ b/lib/dvla/herodotus/version.rb @@ -1,5 +1,5 @@ module DVLA module Herodotus - VERSION = '2.3.0'.freeze + VERSION = '2.3.1'.freeze end end diff --git a/spec/dvla/herodotus_spec.rb b/spec/dvla/herodotus_spec.rb index bc4324e..a83cbe6 100644 --- a/spec/dvla/herodotus_spec.rb +++ b/spec/dvla/herodotus_spec.rb @@ -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')