class Fluent::ParameterizedPathOutput

Constants

DEFAULT_DIR_PERMISSION
DEFAULT_FILE_PERMISSION
SUPPORTED_COMPRESS

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_parameterized_path.rb, line 23
def configure(conf)
  super

  raise ConfigError, "'path_prefix' parameter is required" unless @path_prefix
  raise ConfigError, "'path_key' parameter is required" unless @path_key

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

  @suffix = case @compress
            when nil
              ''
            when :gz
              '.gz'
            end
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_parameterized_path.rb, line 40
def format(tag, time, record)
  log.warn("Undefined key: #{@path_key}") unless record.key?(@path_key)

  path = record[@path_key]
  dup = record.dup
  dup.delete(@path_key)

  data = @formatter.format(tag, time, dup)
  [path, data].to_msgpack
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_parameterized_path.rb, line 51
def write(chunk)
  paths = {}
  chunk.msgpack_each do |(path, data)|
    path = generate_path(chunk.key, path)
    if paths.key?(path)
      paths[path] << data
    else
      paths[path] = data
    end
  end

  paths.each do |path, data|
    FileUtils.mkdir_p(File.dirname(path), mode: DEFAULT_DIR_PERMISSION)

    case @compress
    when nil
      File.open(path, 'ab', DEFAULT_FILE_PERMISSION) { |f| f.write(data) }
    when :gz
      File.open(path, 'ab', DEFAULT_FILE_PERMISSION) do |f|
        Zlib::GzipWriter.wrap(f) { |gz| gz.write(data) }
      end
    end
  end

  paths.keys # for test
end

Private Instance Methods

generate_path(time_string, path) click to toggle source
# File lib/fluent/plugin/out_parameterized_path.rb, line 80
def generate_path(time_string, path)
  path_head = ''
  path_tail = ''

  if pos = path.index('*')
    path_head = path[0, pos]
    path_tail = path[pos + 1..-1]
  else
    path_head = path + '.'
    path_tail = '.log'
  end

  path = nil
  if @append
    path = "#{@path_prefix}/#{path_head}#{time_string}#{path_tail}#{@suffix}"
  else
    i = 0
    begin
      path = "#{@path_prefix}/#{path_head}#{time_string}_#{i}#{path_tail}#{@suffix}"
      i += 1
    end while File.exist?(path)
  end

  path
end