class Fluent::FileAlternativeOutput

Constants

SUPPORTED_COMPRESS

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_file_alternative.rb, line 45
def initialize
  super
  require 'time'
  require 'zlib'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_file_alternative.rb, line 51
def configure(conf)
  if conf['path']
    if conf['path'].index('%S')
      conf['time_slice_format'] = '%Y%m%d%H%M%S'
    elsif conf['path'].index('%M')
      conf['time_slice_format'] = '%Y%m%d%H%M'
    elsif conf['path'].index('%H')
      conf['time_slice_format'] = '%Y%m%d%H'
    end
  end
  if pos = (conf['path'] || '').index('*')
    @path_prefix = conf['path'][0,pos]
    @path_suffix = conf['path'][pos+1..-1]
    conf['buffer_path'] ||= "#{conf['path']}"
  elsif (conf['path'] || '%Y') !~ /%Y|%m|%d|%H|%M|%S/
    if conf['path'] =~ /\.log\Z/
      @path_prefix = conf['path'][0..-4]
      @path_suffix = ".log"
    else
      @path_prefix = conf['path'] + "."
      @path_suffix = ".log"
    end
    conf['buffer_path'] ||= "#{conf['path']}.*"
  elsif (conf['path'] || '') =~ /%Y|%m|%d|%H|%M|%S/
    conf['buffer_path'] ||= conf['path'].gsub('%Y','yyyy').gsub('%m','mm').gsub('%d','dd').gsub('%H','HH').gsub('%M','MM').gsub('%S','SS')
  end

  super

  unless @path.index('/') == 0
    raise Fluent::ConfigError, "Path on filesystem MUST starts with '/', but '#{@path}'"
  end

  if @symlink_path
    unless @symlink_path.index('/') == 0
      raise Fluent::ConfigError, "Symlink path on filesystem MUST starts with '/', but '#{@symlink_path}'"
    end
    @buffer.symlink_path = @symlink_path
  end
end
path_format(chunk_key) click to toggle source

def format(tag, time, record) end

# File lib/fluent/plugin/out_file_alternative.rb, line 109
def path_format(chunk_key)
  suffix = case @compress
           when :gz
             '.gz'
           else
             ''
           end
  if @path_prefix and @path_suffix
    if @compress
      i = 0
      begin
        path = "#{@path_prefix}#{chunk_key}_#{i}#{@path_suffix}#{suffix}"
        i += 1
      end while File.exist?(path)
      path
    else
      "#{@path_prefix}#{chunk_key}#{@path_suffix}#{suffix}"
    end
  else
    if @compress
      path_base = Time.strptime(chunk_key, @time_slice_format).strftime(@path)
      path = path_base + suffix
      if File.exist?(path)
        i = 0
        begin
          path = "#{path_base}.#{i}#{suffix}"
          i += 1
        end while File.exist?(path)
      end
      path
    else
      Time.strptime(chunk_key, @time_slice_format).strftime(@path)
    end
  end
end
record_to_string(record) click to toggle source
# File lib/fluent/plugin/out_file_alternative.rb, line 102
def record_to_string(record)
  record.to_json
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_file_alternative.rb, line 97
def shutdown
  super
  # destroy
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_file_alternative.rb, line 92
def start
  super
  # init
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_file_alternative.rb, line 145
def write(chunk)
  path = path_format(chunk.key)

  begin
    require 'pathname'

    Pathname.new(path).descend {|p|
      FileUtils.mkdir_p( File.dirname(p)) unless File.directory?(p)
      if @set_dir_mode
        FileUtils.chmod @dir_mode.to_i(8), File.dirname(p) unless File.directory?(p)
      end
    }

    case @compress
    when :gz
      Zlib::GzipWriter.open(path) {|f|
        chunk.write_to(f)
      }
    else
      File.open(path, "a") {|f|
        chunk.write_to(f)
      }
    end
  rescue
    log.error "failed to write data: path #{path}"
    raise
  end
  path
end