Class: Fluent::TagFileOutput

Inherits:
FileOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_tag_file.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



5
6
7
8
9
# File 'lib/fluent/plugin/out_tag_file.rb', line 5

def configure(conf)
  conf['buffer_path'] ||= File.join(conf['path'], 'buffer')

  super
end

#format(tag, time, record) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/fluent/plugin/out_tag_file.rb', line 11

def format(tag, time, record)
  tag_elems = tag.split('.')
  tag_elems.shift  # remove PREFIX
  dir = File.join(@path, *tag_elems)

  # @timef is assigned in FileOutput.configure
  time_str = @timef.format(time)

  [dir, "#{time_str}\t#{Yajl.dump(record)}\n"].to_msgpack
end

#write(chunk) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin/out_tag_file.rb', line 22

def write(chunk)
  case @compress
  when nil
    suffix = ''
  when :gz
    suffix = '.gz'
  end

  hash = {}
  chunk.msgpack_each do |(dir, data)|
    sym = dir.to_sym
    if hash.include?(sym)
      hash[sym] += data
    else
      hash[sym] = data
    end
  end

  hash.each do |dir, data|
    dir = File.join(dir.to_s, chunk.key)

    i = 0
    begin
      path = File.join(dir, "#{i}.log#{suffix}")
      i += 1
    end while File.exist?(path)
    FileUtils.mkdir_p dir

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