From 833ac636945c62677e03c2a7915eb8f5b520750b Mon Sep 17 00:00:00 2001 From: David Butler Date: Tue, 9 Aug 2016 17:49:42 -0700 Subject: [PATCH] Flush log messages when exiting The current implementation of message buffering puts messages into a queue, and flushes them every five seconds. If the program exits, any messages in the buffer will be lost. This fixes the issue by automatically flushing the buffer when the program exits. This is done by registering an `at_exit` block which does a final flush, which will wait for all messages to be flushed. If an exception occurs, it will be logged and ignored. This option is configurable with the new option `buffer_flush_at_exit`, which now defaults to `true`. --- README.md | 1 + lib/logstash-logger/buffer.rb | 7 ++++++- lib/logstash-logger/device/connectable.rb | 10 +++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f0b9005..c09bb20 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ You can configure its behavior by passing the following options to LogStashLogge * :drop_messages_on_flush_error - Drop messages when there is a flush error. Defaults to false. * :drop_messages_on_full_buffer - Drop messages when the buffer is full. Defaults to true. * :sync - Flush buffer every time a message is received (blocking). Defaults to false. +* :buffer_flush_at_exit - Flush messages when exiting the program. Defaults to true. You can turn buffering off by setting `sync = true`. diff --git a/lib/logstash-logger/buffer.rb b/lib/logstash-logger/buffer.rb index eb5224f..d5a6d4d 100644 --- a/lib/logstash-logger/buffer.rb +++ b/lib/logstash-logger/buffer.rb @@ -95,9 +95,14 @@ def buffer_initialize(options={}) :has_on_flush_error => self.class.method_defined?(:on_flush_error), :has_on_full_buffer_receive => self.class.method_defined?(:on_full_buffer_receive), :drop_messages_on_flush_error => options.fetch(:drop_messages_on_flush_error, false), - :drop_messages_on_full_buffer => options.fetch(:drop_messages_on_full_buffer, false) + :drop_messages_on_full_buffer => options.fetch(:drop_messages_on_full_buffer, false), + :flush_at_exit => options.fetch(:flush_at_exit, false) } + if @buffer_config[:flush_at_exit] + at_exit { buffer_flush(final: true) } + end + reset_buffer end diff --git a/lib/logstash-logger/device/connectable.rb b/lib/logstash-logger/device/connectable.rb index f76604d..ea399cc 100644 --- a/lib/logstash-logger/device/connectable.rb +++ b/lib/logstash-logger/device/connectable.rb @@ -33,12 +33,20 @@ def initialize(opts = {}) true end + @buffer_flush_at_exit = + if opts.key?(:buffer_flush_at_exit) + opts.delete(:buffer_flush_at_exit) + else + true + end + buffer_initialize( max_items: @buffer_max_items, max_interval: @buffer_max_interval, autoflush: @sync, drop_messages_on_flush_error: @drop_messages_on_flush_error, - drop_messages_on_full_buffer: @drop_messages_on_full_buffer + drop_messages_on_full_buffer: @drop_messages_on_full_buffer, + flush_at_exit: @buffer_flush_at_exit ) end