This filter helps automatically parse messages (or specific event fields) which are of the ‘foo=bar’ variety.
For example, if you have a log message which contains ‘ip=1.2.3.4 error=REFUSED’, you can parse those automatically by configuring:
filter {
kv { }
}
The above will result in a message of “ip=1.2.3.4 error=REFUSED” having the fields:
This is great for postfix, iptables, and other types of logs that tend towards ‘key=value’ syntax.
You can configure any arbitrary strings to split your data on,
in case your data is not structured using ‘=’ signs and whitespace.
For example, this filter can also be used to parse query parameters like
‘foo=bar&baz=fizz’ by setting the field_split
parameter to “&”.
filter {
kv {
add_field => ... # hash (optional), default: {}
add_tag => ... # array (optional), default: []
default_keys => ... # hash (optional), default: {}
exclude_keys => ... # array (optional), default: []
field_split => ... # string (optional), default: " "
include_keys => ... # array (optional), default: []
prefix => ... # string (optional), default: ""
remove_field => ... # array (optional), default: []
remove_tag => ... # array (optional), default: []
source => ... # string (optional), default: "message"
target => ... # string (optional)
trim => ... # string (optional)
trimkey => ... # string (optional)
value_split => ... # string (optional), default: "="
}
}
If this filter is successful, add any arbitrary fields to this event. Field names can be dynamic and include parts of the event using the %{field} Example:
filter {
kv {
add_field => { "foo_%{somefield}" => "Hello world, from %{host}" }
}
}
# You can also add multiple fields at once:
filter {
kv {
add_field => {
"foo_%{somefield}" => "Hello world, from %{host}"
"new_field" => "new_static_value"
}
}
}
If the event has field “somefield” == “hello” this filter, on success, would add field “foo_hello” if it is present, with the value above and the %{host} piece replaced with that value from the event. The second example would also add a hardcoded field.
If this filter is successful, add arbitrary tags to the event. Tags can be dynamic and include parts of the event using the %{field} syntax. Example:
filter {
kv {
add_tag => [ "foo_%{somefield}" ]
}
}
# You can also add multiple tags at once:
filter {
kv {
add_tag => [ "foo_%{somefield}", "taggedy_tag"]
}
}
If the event has field “somefield” == “hello” this filter, on success, would add a tag “foo_hello” (and the second example would of course add a “taggedy_tag” tag).
A hash specifying the default keys and their values which should be added to the event in case these keys do not exist in the source field being parsed.
filter {
kv {
default_keys => [ "from", "logstash@example.com",
"to", "default@dev.null" ]
}
}
An array specifying the parsed keys which should not be added to the event. By default no keys will be excluded.
For example, consider a source like “Hey, from=
Only handle events without all/any (controlled by exclude_any config option) of these tags. Optional.
A string of characters to use as delimiters for parsing out key-value pairs.
These characters form a regex character class and thus you must escape special regex characters like ‘[’ or ‘]’ using ‘'.
For example, to split out the args from a url query string such as ‘?pin=12345~0&d=123&e=foo@bar.com&oq=bobo&ss=12345’:
filter {
kv {
field_split => "&?"
}
}
The above splits on both “&” and “?” characters, giving you the following fields:
An array specifying the parsed keys which should be added to the event. By default all keys will be added.
For example, consider a source like “Hey, from=
A string to prepend to all of the extracted keys.
For example, to prepend arg_ to all keys:
filter { kv { prefix => "arg_" } }
If this filter is successful, remove arbitrary fields from this event. Fields names can be dynamic and include parts of the event using the %{field} Example:
filter {
kv {
remove_field => [ "foo_%{somefield}" ]
}
}
# You can also remove multiple fields at once:
filter {
kv {
remove_field => [ "foo_%{somefield}" "my_extraneous_field" ]
}
}
If the event has field “somefield” == “hello” this filter, on success, would remove the field with name “foo_hello” if it is present. The second example would remove an additional, non-dynamic field.
If this filter is successful, remove arbitrary tags from the event. Tags can be dynamic and include parts of the event using the %{field} syntax. Example:
filter {
kv {
remove_tag => [ "foo_%{somefield}" ]
}
}
# You can also remove multiple tags at once:
filter {
kv {
remove_tag => [ "foo_%{somefield}", "sad_unwanted_tag"]
}
}
If the event has field “somefield” == “hello” this filter, on success, would remove the tag “foo_hello” if it is present. The second example would remove a sad, unwanted tag as well.
The field to perform ‘key=value’ searching on
For example, to process the not_the_message
field:
filter { kv { source => "not_the_message" } }
Only handle events with all/any (controlled by include_any config option) of these tags. Optional.
The name of the container to put all of the key-value pairs into.
If this setting is omitted, fields will be written to the root of the event, as individual fields.
For example, to place all keys into the event field kv:
filter { kv { target => "kv" } }
A string of characters to trim from the value. This is useful if your values are wrapped in brackets or are terminated with commas (like postfix logs).
These characters form a regex character class and thus you must escape special regex characters like ‘[’ or ‘]’ using ‘'.
For example, to strip ‘<’, ‘>’, ‘[’, ‘]’ and ‘,’ characters from values:
filter {
kv {
trim => "<>\[\],"
}
}
A string of characters to trim from the key. This is useful if your keys are wrapped in brackets or start with space.
These characters form a regex character class and thus you must escape special regex characters like ‘[’ or ‘]’ using ‘'.
For example, to strip ‘<’ ‘>’ ‘[’ ’]’ and ‘,’ characters from keys:
filter {
kv {
trimkey => "<>\[\],"
}
}
Note that all of the specified routing options (type,tags.exclude_tags,include_fields,exclude_fields) must be met in order for the event to be handled by the filter. The type to act on. If a type is given, then this filter will only act on messages with the same type. See any input plugin’s “type” attribute for more. Optional.
A string of characters to use as delimiters for identifying key-value relations.
These characters form a regex character class and thus you must escape special regex characters like ‘[’ or ‘]’ using ‘'.
For example, to identify key-values such as ‘key1:value1 key2:value2’:
filter { kv { value_split => ":" } }