Skip to content

Commit 80b2320

Browse files
committed
Support timezone
Fix #73 Signed-off-by: Kenji Okimoto <okimoto@clear-code.com>
1 parent 0d38c38 commit 80b2320

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/fluent/plugin/grok.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def initialize(plugin, conf)
2727
@multiline_mode = false
2828
@conf = conf
2929
@plugin = plugin
30+
@time_format = nil
31+
@timezone = nil
3032
if @plugin.respond_to?(:firstline?)
3133
@multiline_mode = true
3234
end
@@ -39,6 +41,9 @@ def initialize(plugin, conf)
3941
if @plugin.respond_to?(:time_format)
4042
@time_format = @plugin.time_format
4143
end
44+
if @plugin.respond_to?(:timezon)
45+
@timezone = @plugin.timezone
46+
end
4247
end
4348

4449
def add_patterns_from_file(path)
@@ -105,6 +110,9 @@ def expand_pattern_expression_grok_section(conf)
105110
if conf["time_format"] || @time_format
106111
_conf["time_format"] = conf["time_format"] || @time_format
107112
end
113+
if conf["timezone"] || @timezone
114+
_conf["timezone"] = conf["timezone"] || @timezone
115+
end
108116
_conf["expression"] = regexp
109117
config = Fluent::Config::Element.new("parse", "", _conf, [])
110118
parser = Fluent::Plugin::RegexpParser.new

lib/fluent/plugin/parser_grok.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class GrokParser < Parser
2525
config_param :keep_time_key, :bool, default: false
2626
config_param :time_key, :string, default: "time"
2727
config_param :time_format, :string, default: nil
28+
config_param :timezone, :string, default: nil
2829
end
2930

3031
def initialize

test/test_grok_parser.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,21 @@ class GrokParserTest < ::Test::Unit::TestCase
374374
assert_equal(expected_record, record)
375375
end
376376
end
377+
378+
test "timezone" do
379+
d = create_driver(%[
380+
<grok>
381+
pattern %{TIMESTAMP_ISO8601:time} %{GREEDYDATA:message}
382+
time_key time
383+
time_format %Y-%m-%d %H:%M:%S
384+
timezone Europe/Berlin
385+
</grok>
386+
])
387+
d.instance.parse("2019-02-01 12:34:56 This is test") do |time, record|
388+
assert_equal(event_time("2019-02-01 12:34:56 +0100"), time)
389+
assert_equal({ "message" => "This is test" }, record)
390+
end
391+
end
377392
end
378393

379394
private

0 commit comments

Comments
 (0)