Skip to content

Commit 35cb264

Browse files
committed
support custom null value
1 parent 0ffb0e9 commit 35cb264

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ And you can overwrite default formatting configurations on your plugin (values b
6161
config_set_default :time_format, nil # nil means ISO8601 '2012-07-13T19:29:49+09:00'
6262
config_set_default :remove_prefix, nil
6363
config_set_default :default_tag, nil
64+
config_set_default :null_value, 'NULL'
6465
6566
# ...
6667
end
@@ -80,13 +81,16 @@ Provided configurations are below:
8081
* remove_prefix
8182
* input tag 'test.foo' with 'remove_prefix test', output tag is 'foo'.
8283
* 'default\_tag' configuration is used when input tag is completely equal to 'remove\_prefix'
84+
* null_value
85+
* output value if value is null(nil). default is 'NULL'.
8386

8487
## AUTHOR / CONTRIBUTORS
8588

8689
* AUTHOR
8790
* TAGOMORI Satoshi <[email protected]>
8891
* CONTRIBUTORS
8992
* wolfg1969 https://github.com/wolfg1969
93+
* Shinya Okano <[email protected]>
9094

9195
## LICENSE
9296

lib/fluent/mixin/plaintextformatter.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module PlainTextFormatter
77
attr_accessor :output_include_time, :output_include_tag, :output_data_type
88
attr_accessor :add_newline, :field_separator
99
attr_accessor :remove_prefix, :default_tag
10+
attr_accessor :null_value
1011

1112
attr_accessor :suppress_log_broken_string
1213

@@ -46,6 +47,8 @@ def configure(conf)
4647
end
4748
end
4849

50+
@null_value = first_value( conf['null_value'], @null_value, 'NULL' )
51+
4952
# default timezone: utc
5053
if not conf.has_key?('localtime') and not conf.has_key?('utc')
5154
@localtime = false
@@ -85,7 +88,7 @@ def stringify_record(record)
8588
end
8689
else
8790
@custom_attributes.map{|attr|
88-
record[attr].nil? ? 'NULL' : record[attr].to_s
91+
record[attr].nil? ? @null_value : record[attr].to_s
8992
}.join(@f_separator)
9093
end
9194
end

test/mixin/test_plaintextformatter.rb

+29
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,33 @@ def test_format_boolean_attribute
289289
# format
290290
assert_equal "2012-07-13T07:05:05Z\ttest.a\ttrue\tfalse\n", p.format('test.a', 1342163105, r)
291291
end
292+
293+
def test_null_value_default
294+
p = create_plugin_instance(Fluent::TestAOutput, %[
295+
type testa
296+
output_include_time false
297+
output_include_tag false
298+
output_data_type attr:foo
299+
])
300+
r = {'foo' => nil}
301+
# stringify
302+
assert_equal "NULL", p.stringify_record(r)
303+
# format
304+
assert_equal "NULL\n", p.format('test.a', 1342163105, r)
305+
end
306+
307+
def test_null_value_custom
308+
p = create_plugin_instance(Fluent::TestAOutput, %[
309+
type testa
310+
output_include_time false
311+
output_include_tag false
312+
output_data_type attr:foo
313+
null_value \\N
314+
])
315+
r = {'foo' => nil}
316+
# stringify
317+
assert_equal "\\N", p.stringify_record(r)
318+
# format
319+
assert_equal "\\N\n", p.format('test.a', 1342163105, r)
320+
end
292321
end

0 commit comments

Comments
 (0)