Fluentd
Search…
Plugin Helper: Formatter
The formatter plugin helper manages the lifecycle of the formatter plugin.
Here is an example:
1
require 'fluent/plugin/output'
2
3
module Fluent::Plugin
4
class ExampleOutput < Output
5
Fluent::Plugin.register_output('example', self)
6
7
# 1. Load formatter helper
8
helpers :formatter
9
10
# Omit `shutdown` and other plugin APIs
11
12
def configure(conf)
13
super
14
15
# 2. Call `formatter_create` to create object
16
@formatter = formatter_create(usage: 'awesome_formatter')
17
end
18
19
def format(tag, time, record)
20
# 3. Call `format` method to format `record`
21
@formatter.format(tag, time, record)
22
end
23
end
24
end
Copied!
For more details, see the following articles:

Methods

formatter_create(usage: "", type: nil, conf: nil, default_type: nil)

    usage: unique name required for multiple formatters
    type: formatter type
    conf: formatter plugin configuration
    default_type: default formatter type
Examples:
1
def configure(conf)
2
super
3
# Create formatter plugin instance using <format> section in `fluent.conf` during configure phase
4
@formatter = formatter_create
5
end
6
7
def format(tag, time, record)
8
@formatter.format(tag, time, record)
9
end
Copied!
JSON formatter example:
1
# Create JSON formatter
2
def configure(conf)
3
super
4
@json_formatter = formatter_create(usage: 'formatter_in_example_json', type: 'json')
5
end
6
7
def format(tag, time, record)
8
@json_formatter.format(tag, time, record)
9
end
Copied!
MsgPack formatter example:
1
# Create MsgPack formatter
2
def configure(conf)
3
super
4
@msgpack_formatter = formatter_create(usage: 'formatter_in_example_msgpack', type: 'msgpack')
5
end
6
7
def format(tag, time, record)
8
@msgpack_formatter.format(tag, time, record)
9
end
Copied!

Plugins using formatter

If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 4mo ago