Class: Fluent::Plugin::CsvFormatter

Inherits:
Formatter show all
Includes:
Fluent::PluginHelper::Mixin
Defined in:
lib/fluent/plugin/formatter_csv.rb

Direct Known Subclasses

Compat::TextFormatter::CsvFormatter

Constant Summary

Constants inherited from Formatter

Formatter::PARSER_TYPES

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from Base

#under_plugin_development

Instance Method Summary collapse

Methods included from Fluent::PluginHelper::Mixin

included

Methods inherited from Formatter

#formatter_type

Methods included from TimeMixin::Formatter

included, #time_formatter_create

Methods included from OwnedByMixin

#log, #owner, #owner=

Methods inherited from Base

#acquire_worker_lock, #after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #called_in_test?, #close, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #get_lock_path, #has_router?, #initialize, #inspect, #multi_workers_ready?, #plugin_root_dir, #reloadable_plugin?, #shutdown, #shutdown?, #start, #started?, #stop, #stopped?, #string_safe_encoding, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, #configure_proxy_generate, #configured_section_create, included, #initialize, lookup_type, register_type

Constructor Details

This class inherits a constructor from Fluent::Plugin::Base

Instance Method Details

#configure(conf) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fluent/plugin/formatter_csv.rb', line 38

def configure(conf)
  super

  @fields = fields.select{|f| !f.empty? }
  raise ConfigError, "empty value is specified in fields parameter" if @fields.empty?

  if @fields.any? { |f| record_accessor_nested?(f) }
    @accessors = @fields.map { |f| record_accessor_create(f) }
    mformat = method(:format_with_nested_fields)
    singleton_class.module_eval do
      define_method(:format, mformat)
    end
  end

  @generate_opts = {col_sep: @delimiter, force_quotes: @force_quotes, headers: @fields,
                    row_sep: @add_newline ? :auto : "".force_encoding(Encoding::ASCII_8BIT)}
  # Cache CSV object per thread to avoid internal state sharing
  @cache = {}
end

#format(tag, time, record) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin/formatter_csv.rb', line 58

def format(tag, time, record)
  csv = (@cache[Thread.current] ||= CSV.new("".force_encoding(Encoding::ASCII_8BIT), **@generate_opts))
  line = (csv << record).string.dup
  # Need manual cleanup because CSV writer doesn't provide such method.
  csv.rewind
  csv.truncate(0)
  line
end

#format_with_nested_fields(tag, time, record) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/fluent/plugin/formatter_csv.rb', line 67

def format_with_nested_fields(tag, time, record)
  csv = (@cache[Thread.current] ||= CSV.new("".force_encoding(Encoding::ASCII_8BIT), **@generate_opts))
  values = @accessors.map { |a| a.call(record) }
  line = (csv << values).string.dup
  # Need manual cleanup because CSV writer doesn't provide such method.
  csv.rewind
  csv.truncate(0)
  line
end