Skip to content

Commit

Permalink
Merge pull request #2732 from ganmacs/create-dir-logfile
Browse files Browse the repository at this point in the history
LoggerInitializer updated to create directory when it doesn't exists
  • Loading branch information
repeatedly authored Dec 12, 2019
2 parents 381aafe + 2a8cc59 commit 3c6c64a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ def init(process_type, worker_id)
@opts[:worker_id] = worker_id

if @path && @path != "-"
unless File.exist?(@path)
FileUtils.mkdir_p(File.dirname(@path))
end

@logdev = if @log_rotate_age || @log_rotate_size
Fluent::LogDeviceIO.new(Fluent.windows? ?
worker_id_suffixed_path(worker_id, @path) : @path,
Expand Down
26 changes: 26 additions & 0 deletions test/test_logger_initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require_relative 'helper'
require 'fluent/supervisor'
require 'fileutils'

class LoggerInitializerTest < ::Test::Unit::TestCase
TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/tmp/logger_initializer#{ENV['TEST_ENV_NUMBER']}")

teardown do
begin
FileUtils.rm_rf(TMP_DIR)
rescue => _
end
end

test 'when path is given' do
path = File.join(TMP_DIR, 'fluent_with_path.log')

assert_false File.exist?(TMP_DIR)
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {})

assert_nothing_raised do
logger.init(:supervisor, 0)
end
assert_true File.exist?(TMP_DIR)
end
end

0 comments on commit 3c6c64a

Please sign in to comment.