diff --git a/lib/logstash/outputs/file.rb b/lib/logstash/outputs/file.rb index a65b9bb..3c53470 100644 --- a/lib/logstash/outputs/file.rb +++ b/lib/logstash/outputs/file.rb @@ -69,6 +69,16 @@ class LogStash::Outputs::File < LogStash::Outputs::Base # Setting it to -1 uses default OS value. # Example: `"file_mode" => 0640` config :file_mode, :validate => :number, :default => -1 + + # Number of rotation files to use. + # 0 or negative values results in no rotation files + config :rotate, :validate => :number, :default => 0 + + # When the filesize exceeds the specified amount of megabytes, a file rotation + # is triggered on the next write attempt. + # Negative or zero disables file rotation + config :rotate_size, :validate => :number, :default => -1 + default :codec, "json_lines" @@ -158,13 +168,35 @@ def write_event(event, data) elsif !@create_if_deleted && deleted?(file_output_path) file_output_path = @failure_path end + + if rotate_size > 0 && rotate > 0 + rotate_files(file_output_path) + end + @logger.debug("File, writing event to file.", :filename => file_output_path) fd = open(file_output_path) - # TODO(sissel): Check if we should rotate the file. + + # TODO(sissel): Check if we should rotate the file. fd.write(data) flush(fd) end + private + def rotate_files(path) + if File.exists?(path) && (File.size(path)/1000000.0) > rotate_size + close() + i = rotate + while i > 0 + i-=1 + if File.exist?(path+".#{i}") + File.rename(path+".#{i}", path+".#{i+1}") + end + end + File.rename(path, path+".#{i+1}") + logger.info("File: filesize exceeded maximum, rotated files.") + end + end + private def generate_filepath(event) event.sprintf(@path)