diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 6a226a544d..3d5a49c2b9 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -96,6 +96,8 @@ def initialize config_param :skip_refresh_on_startup, :bool, default: false desc 'Ignore repeated permission error logs' config_param :ignore_repeated_permission_error, :bool, default: false + desc 'Format path with the specified timezone' + config_param :path_timezone, :string, default: nil config_section :parse, required: false, multi: true, init: true, param_name: :parser_configs do config_argument :usage, :string, default: 'in_tail_parser' @@ -129,6 +131,11 @@ def configure(conf) if @paths.empty? raise Fluent::ConfigError, "tail: 'path' parameter is required on tail input" end + if @path_timezone + Fluent::Timezone.validate!(@path_timezone) + @path_formatters = @paths.map{|path| [path, Fluent::Timezone.formatter(@path_timezone, path)]}.to_h + @exclude_path_formatters = @exclude_path.map{|path| [path, Fluent::Timezone.formatter(@path_timezone, path)]}.to_h + end # TODO: Use plugin_root_dir and storage plugin to store positions if available if @pos_file @@ -225,17 +232,20 @@ def close end def expand_paths - date = Time.now + date = Fluent::EventTime.now paths = [] - @paths.each { |path| - path = date.strftime(path) + path = if @path_timezone + @path_formatters[path].call(date) + else + date.to_time.strftime(path) + end if path.include?('*') paths += Dir.glob(path).select { |p| begin is_file = !File.directory?(p) if File.readable?(p) && is_file - if @limit_recently_modified && File.mtime(p) < (date - @limit_recently_modified) + if @limit_recently_modified && File.mtime(p) < (date.to_time - @limit_recently_modified) false else true @@ -259,7 +269,14 @@ def expand_paths paths << path end } - excluded = @exclude_path.map { |path| path = date.strftime(path); path.include?('*') ? Dir.glob(path) : path }.flatten.uniq + excluded = @exclude_path.map { |path| + path = if @path_timezone + @exclude_path_formatters[path].call(date) + else + date.to_time.strftime(path) + end + path.include?('*') ? Dir.glob(path) : path + }.flatten.uniq paths - excluded end diff --git a/test/plugin/test_in_tail.rb b/test/plugin/test_in_tail.rb index d67cbdce3c..eb8c3b8493 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -969,6 +969,27 @@ def test_expand_paths assert_equal EX_PATHS - [EX_PATHS.last], plugin.expand_paths.sort end + def test_expand_paths_with_timezone + ['Asia/Taipei', '+08'].each do |tz_type| + taipei_config = EX_CONFIG + config_element("", "", {"path_timezone" => tz_type}) + plugin = create_driver(taipei_config, false).instance + + # Test exclude + exclude_config = taipei_config + config_element("", "", { "exclude_path" => %Q(["test/plugin/**/%Y%m%d-%H%M%S.log"]) }) + exclude_plugin = create_driver(exclude_config, false).instance + + with_timezone('utc') do + flexstub(Time) do |timeclass| + # env : 2010-01-01 19:04:05 (UTC), tail path : 2010-01-02 03:04:05 (Asia/Taipei) + timeclass.should_receive(:now).with_no_args.and_return(Time.new(2010, 1, 1, 19, 4, 5)) + + assert_equal EX_PATHS, plugin.expand_paths.sort + assert_equal EX_PATHS - [EX_PATHS.first], exclude_plugin.expand_paths.sort + end + end + end + end + def test_log_file_without_extension expected_files = [ 'test/plugin/data/log/bar',