Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file rotation #43

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/logstash/outputs/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down