Class: Fluent::FileAlternativeOutput

Inherits:
TimeSlicedOutput
  • Object
show all
Includes:
Mixin::PlainTextFormatter
Defined in:
lib/fluent/plugin/out_file_alternative.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initializeFileAlternativeOutput

Returns a new instance of FileAlternativeOutput.



32
33
34
35
36
# File 'lib/fluent/plugin/out_file_alternative.rb', line 32

def initialize
  super
  require 'time'
  require 'zlib'
end

Instance Method Details

#configure(conf) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fluent/plugin/out_file_alternative.rb', line 38

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) ⇒ Object

def format(tag, time, record) end



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fluent/plugin/out_file_alternative.rb', line 96

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) ⇒ Object



89
90
91
# File 'lib/fluent/plugin/out_file_alternative.rb', line 89

def record_to_string(record)
  record.to_json
end

#shutdownObject



84
85
86
87
# File 'lib/fluent/plugin/out_file_alternative.rb', line 84

def shutdown
  super
  # destroy
end

#startObject



79
80
81
82
# File 'lib/fluent/plugin/out_file_alternative.rb', line 79

def start
  super
  # init
end

#write(chunk) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fluent/plugin/out_file_alternative.rb', line 132

def write(chunk)
  path = path_format(chunk.key)

  begin
    FileUtils.mkdir_p File.dirname(path)

    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