Class: Fluent::FileOutput

Inherits:
TimeSlicedOutput show all
Defined in:
lib/fluent/plugin/out_file.rb

Constant Summary collapse

SUPPORTED_COMPRESS =
{
  'gz' => :gz,
  'gzip' => :gz,
}

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from TimeSlicedOutput

#localtime, #time_slicer

Attributes inherited from Output

#router

Attributes included from PluginLoggerMixin

#log

Instance Method Summary collapse

Methods inherited from TimeSlicedOutput

#emit, #enqueue_buffer

Methods inherited from BufferedOutput

#before_shutdown, #calc_retry_wait, #emit, #enqueue_buffer, #flush_secondary, #force_flush, #format_stream, #shutdown, #start, #submit_flush, #try_flush, #write_abort

Methods inherited from Output

#shutdown, #start

Methods included from PluginLoggerMixin

included

Methods included from PluginId

#plugin_id

Methods included from Configurable

#config, included, lookup_type, register_type

Constructor Details

#initializeFileOutput

Returns a new instance of FileOutput.



49
50
51
52
53
54
# File 'lib/fluent/plugin/out_file.rb', line 49

def initialize
  require 'zlib'
  require 'time'
  require 'fluent/plugin/file_util'
  super
end

Instance Method Details

#configure(conf) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/out_file.rb', line 56

def configure(conf)
  if path = conf['path']
    @path = path
  end
  unless @path
    raise ConfigError, "'path' parameter is required on file output"
  end

  if pos = @path.index('*')
    @path_prefix = @path[0,pos]
    @path_suffix = @path[pos+1..-1]
    conf['buffer_path'] ||= "#{@path}"
  else
    @path_prefix = @path+"."
    @path_suffix = ".log"
    conf['buffer_path'] ||= "#{@path}.*"
  end

  test_path = generate_path(Time.now.strftime(@time_slice_format))
  unless ::Fluent::FileUtil.writable_p?(test_path)
    raise ConfigError, "out_file: `#{test_path}` is not writable"
  end

  super

  @formatter = Plugin.new_formatter(@format)
  @formatter.configure(conf)

  @buffer.symlink_path = @symlink_path if @symlink_path
end

#format(tag, time, record) ⇒ Object



87
88
89
# File 'lib/fluent/plugin/out_file.rb', line 87

def format(tag, time, record)
  @formatter.format(tag, time, record)
end

#secondary_init(primary) ⇒ Object



111
112
113
# File 'lib/fluent/plugin/out_file.rb', line 111

def secondary_init(primary)
  # don't warn even if primary.class is not FileOutput
end

#write(chunk) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fluent/plugin/out_file.rb', line 91

def write(chunk)
  path = generate_path(chunk.key)
  FileUtils.mkdir_p File.dirname(path), mode: DEFAULT_DIR_PERMISSION

  case @compress
  when nil
    File.open(path, "a", DEFAULT_FILE_PERMISSION) {|f|
      chunk.write_to(f)
    }
  when :gz
    File.open(path, "a", DEFAULT_FILE_PERMISSION) {|f|
      gz = Zlib::GzipWriter.new(f)
      chunk.write_to(gz)
      gz.close
    }
  end

  return path  # for test
end