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.



34
35
36
37
38
# File 'lib/fluent/plugin/out_file_alternative.rb', line 34

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

Instance Method Details

#configure(conf) ⇒ Object



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
78
79
# File 'lib/fluent/plugin/out_file_alternative.rb', line 40

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



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
131
132
# File 'lib/fluent/plugin/out_file_alternative.rb', line 98

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



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

def record_to_string(record)
  record.to_json
end

#shutdownObject



86
87
88
89
# File 'lib/fluent/plugin/out_file_alternative.rb', line 86

def shutdown
  super
  # destroy
end

#startObject



81
82
83
84
# File 'lib/fluent/plugin/out_file_alternative.rb', line 81

def start
  super
  # init
end

#write(chunk) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/fluent/plugin/out_file_alternative.rb', line 134

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)
      FileUtils.chmod @dir_mode.to_i(8), File.dirname(p) unless File.directory?(p)
    }

    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