From e92e1669f24f13d0dba42f1ba0475a144c69a2a5 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 18 Oct 2019 14:30:05 +0900 Subject: [PATCH 1/2] Support logrotate on Windows correctly In Windows multi-process environment, we cannot rename a file which is opened by another process. Then, we should do this: * Separate log file for each process on Windows Signed-off-by: Hiroshi Hatake --- lib/fluent/command/fluentd.rb | 11 +++++++++++ lib/fluent/supervisor.rb | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/fluent/command/fluentd.rb b/lib/fluent/command/fluentd.rb index 93e2c35cfe..71cceb7f1f 100644 --- a/lib/fluent/command/fluentd.rb +++ b/lib/fluent/command/fluentd.rb @@ -311,6 +311,17 @@ require 'fluent/supervisor' if opts[:supervise] + if Fluent.windows? + + if opts[:log_path] && opts[:log_path] != "-" + if opts[:log_rotate_age] || opts[:log_rotate_size] + require 'pathname' + + log_path = Pathname(opts[:log_path]).sub_ext("-supervisor#{Pathname(opts[:log_path]).extname}").to_s + opts[:log_path] = log_path + end + end + end Fluent::Supervisor.new(opts).run_supervisor else if opts[:standalone_worker] && opts[:workers] && opts[:workers] > 1 diff --git a/lib/fluent/supervisor.rb b/lib/fluent/supervisor.rb index 44715b55f7..35f443f0ac 100644 --- a/lib/fluent/supervisor.rb +++ b/lib/fluent/supervisor.rb @@ -354,13 +354,21 @@ def initialize(path, level, chuser, chgroup, opts, log_rotate_age: nil, log_rota @log_rotate_size = log_rotate_size end + def worker_id_suffixed_path(worker_id, path) + require 'pathname' + + Pathname(path).sub_ext("-#{worker_id}#{Pathname(path).extname}").to_s + end + def init(process_type, worker_id) @opts[:process_type] = process_type @opts[:worker_id] = worker_id if @path && @path != "-" @logdev = if @log_rotate_age || @log_rotate_size - Fluent::LogDeviceIO.new(@path, shift_age: @log_rotate_age, shift_size: @log_rotate_size) + Fluent::LogDeviceIO.new(Fluent.windows? ? + worker_id_suffixed_path(worker_id, @path) : @path, + shift_age: @log_rotate_age, shift_size: @log_rotate_size) else File.open(@path, "a") end From fa0348d52b5bd91fe4fc2420f6fb21d05c4d49a5 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 24 Oct 2019 11:41:47 +0900 Subject: [PATCH 2/2] Remove a needless newline Signed-off-by: Hiroshi Hatake --- lib/fluent/command/fluentd.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/fluent/command/fluentd.rb b/lib/fluent/command/fluentd.rb index 71cceb7f1f..debef108dd 100644 --- a/lib/fluent/command/fluentd.rb +++ b/lib/fluent/command/fluentd.rb @@ -312,7 +312,6 @@ require 'fluent/supervisor' if opts[:supervise] if Fluent.windows? - if opts[:log_path] && opts[:log_path] != "-" if opts[:log_rotate_age] || opts[:log_rotate_size] require 'pathname'