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.



45
46
47
48
49
# File 'lib/fluent/plugin/out_file_alternative.rb', line 45

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

Instance Method Details

#configure(conf) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object

def format(tag, time, record) end



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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) ⇒ Object



102
103
104
# File 'lib/fluent/plugin/out_file_alternative.rb', line 102

def record_to_string(record)
  record.to_json
end

#shutdownObject



97
98
99
100
# File 'lib/fluent/plugin/out_file_alternative.rb', line 97

def shutdown
  super
  # destroy
end

#startObject



92
93
94
95
# File 'lib/fluent/plugin/out_file_alternative.rb', line 92

def start
  super
  # init
end

#write(chunk) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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