@@ -31,10 +31,21 @@ class Plugin::RecordModifierFilter < Plugin::Filter
31
31
This option is exclusive with `remove_keys`.
32
32
DESC
33
33
34
- config_param :replace , :bool , default : false ,
35
- desc : <<-DESC
36
- Boolean flag to enable replace function. Default is false.
37
- DESC
34
+ config_section :replace , param_name : :replaces , multi : true do
35
+ desc "The field name to which the regular expression is applied"
36
+ config_param :key , :string
37
+ desc "The regular expression"
38
+ config_param :expression do |value |
39
+ if value . start_with? ( "/" ) && value . end_with? ( "/" )
40
+ Regexp . compile ( value [ 1 ..-2 ] )
41
+ else
42
+ $log. warn ( "You should use \" pattern /#{ value } /\" instead of \" pattern #{ value } \" " )
43
+ Regexp . compile ( value )
44
+ end
45
+ end
46
+ desc "The replacement string"
47
+ config_param :replace , :string
48
+ end
38
49
39
50
def configure ( conf )
40
51
super
@@ -71,20 +82,6 @@ def configure(conf)
71
82
end
72
83
end
73
84
74
- @replace_keys = Array . new
75
- if @replace
76
- conf . elements . select { |element | element . name == 'replace' } . each do |element |
77
- expr = if element [ 'expression' ] [ 0 ] == "/" && element [ 'expression' ] [ -1 ] == "/"
78
- element [ 'expression' ] [ 1 ..-2 ]
79
- else
80
- element [ 'expression' ]
81
- end
82
- @replace_keys . push ( "key" => element [ 'key' ] ,
83
- "expression" => Regexp . new ( expr ) ,
84
- "replace" => element [ 'replace' ] )
85
- end
86
- end
87
-
88
85
if @remove_keys and @whitelist_keys
89
86
raise Fluent ::ConfigError , "remove_keys and whitelist_keys are exclusive with each other."
90
87
elsif @remove_keys
@@ -116,9 +113,9 @@ def filter(tag, time, record)
116
113
record = modified
117
114
end
118
115
119
- if @replace && @replace_keys
120
- @replace_keys . each { |rep |
121
- record [ rep [ ' key' ] ] = record [ rep [ ' key' ] ] . gsub ( rep [ ' expression' ] , rep [ ' replace' ] ) if record . include? ( rep [ ' key' ] ) && rep [ ' expression' ] . match ( record [ rep [ ' key' ] ] )
116
+ if @replaces . any?
117
+ @replaces . each { |replace |
118
+ record [ replace . key ] = record [ replace . key ] . gsub ( replace . expression , replace . replace ) if record . include? ( replace . key ) && replace . expression . match ( record [ replace . key ] )
122
119
}
123
120
end
124
121
0 commit comments