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:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fluent/plugin/formatter_csv.rb', line 54

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)}
end

#csv_cacheable?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/fluent/plugin/formatter_csv.rb', line 38

def csv_cacheable?
  !!owner
end

#csv_for_threadObject



46
47
48
49
50
51
52
# File 'lib/fluent/plugin/formatter_csv.rb', line 46

def csv_for_thread
  if csv_cacheable?
    Thread.current[csv_thread_key] ||= CSV.new("".force_encoding(Encoding::ASCII_8BIT), **@generate_opts)
  else
    CSV.new("".force_encoding(Encoding::ASCII_8BIT), **@generate_opts)
  end
end

#csv_thread_keyObject



42
43
44
# File 'lib/fluent/plugin/formatter_csv.rb', line 42

def csv_thread_key
  csv_cacheable? ? "#{owner.plugin_id}_csv_formatter_#{@usage}_csv" : nil
end

#format(tag, time, record) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/fluent/plugin/formatter_csv.rb', line 72

def format(tag, time, record)
  csv = csv_for_thread
  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



81
82
83
84
85
86
87
88
89
# File 'lib/fluent/plugin/formatter_csv.rb', line 81

def format_with_nested_fields(tag, time, record)
  csv = csv_for_thread
  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