lib/logstash/outputs/file.rb in logstash-output-file-0.1.3 vs lib/logstash/outputs/file.rb in logstash-output-file-0.1.4

- old
+ new

@@ -74,18 +74,27 @@ @stale_cleanup_interval = 10 end # def register private def validate_path - root_directory = @path.split(File::SEPARATOR).select { |item| !item.empty? }.shift - if (root_directory =~ FIELD_REF) != nil @logger.error("File: The starting part of the path should not be dynamic.", :path => @path) raise LogStash::ConfigurationError.new("The starting part of the path should not be dynamic.") end end + private + def root_directory + parts = @path.split(File::SEPARATOR).select { |item| !item.empty? } + if Gem.win_platform? + # First part is the drive letter + parts[1] + else + parts.first + end + end + public def receive(event) return unless output?(event) file_output_path = generate_filepath(event) @@ -97,10 +106,24 @@ output = format_message(event) write_event(file_output_path, output) end # def receive + public + def teardown + @logger.debug("Teardown: closing files") + @files.each do |path, fd| + begin + fd.close + @logger.debug("Closed file #{path}", :fd => fd) + rescue Exception => e + @logger.error("Exception while flushing and closing files.", :exception => e) + end + end + finished + end + private def inside_file_root?(log_path) target_file = File.expand_path(log_path) return target_file.start_with?("#{@file_root.to_s}/") end @@ -127,46 +150,36 @@ private def path_with_field_ref? path =~ FIELD_REF end + private def format_message(event) if @message_format event.sprintf(@message_format) else event.to_json end end + private def extract_file_root parts = File.expand_path(path).split(File::SEPARATOR) parts.take_while { |part| part !~ FIELD_REF }.join(File::SEPARATOR) end - def teardown - @logger.debug("Teardown: closing files") - @files.each do |path, fd| - begin - fd.close - @logger.debug("Closed file #{path}", :fd => fd) - rescue Exception => e - @logger.error("Exception while flushing and closing files.", :exception => e) - end - end - finished - end - private def flush(fd) if flush_interval > 0 flush_pending_files else fd.flush end end # every flush_interval seconds or so (triggered by events, but if there are no events there's no point flushing files anyway) + private def flush_pending_files return unless Time.now - @last_flush_cycle >= flush_interval @logger.debug("Starting flush cycle") @files.each do |path, fd| @logger.debug("Flushing file", :path => path, :fd => fd) @@ -174,10 +187,11 @@ end @last_flush_cycle = Time.now end # every 10 seconds or so (triggered by events, but if there are no events there's no point closing files anyway) + private def close_stale_files now = Time.now return unless now - @last_stale_cleanup_cycle >= @stale_cleanup_interval @logger.info("Starting stale files cleanup cycle", :files => @files) inactive_files = @files.select { |path, fd| not fd.active } @@ -190,9 +204,10 @@ # mark all files as inactive, a call to write will mark them as active again @files.each { |path, fd| fd.active = false } @last_stale_cleanup_cycle = now end + private def open(path) return @files[path] if @files.include?(path) and not @files[path].nil? @logger.info("Opening file", :path => path)