From 55d63355b3d7e7feaba84e69a30a53ba0a28ea66 Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Thu, 24 Oct 2019 14:10:10 +0900 Subject: [PATCH] Merge pull request #2663 from fluent/logrotate-windows Support logrotate on Windows correctly. Fix #2446 Signed-off-by: Masahiro Nakagawa --- lib/fluent/command/fluentd.rb | 10 ++++++++++ lib/fluent/supervisor.rb | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/fluent/command/fluentd.rb b/lib/fluent/command/fluentd.rb index 93e2c35cfe..debef108dd 100644 --- a/lib/fluent/command/fluentd.rb +++ b/lib/fluent/command/fluentd.rb @@ -311,6 +311,16 @@ 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 491edd7f89..ea3478856b 100644 --- a/lib/fluent/supervisor.rb +++ b/lib/fluent/supervisor.rb @@ -353,13 +353,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