lib/semantic_logger/appender/async.rb in semantic_logger-4.2.0 vs lib/semantic_logger/appender/async.rb in semantic_logger-4.2.1

- old
+ new

@@ -20,13 +20,10 @@ def_delegator :@appender, :logger # Appender proxy to allow an existing appender to run asynchronously in a separate thread. # # Parameters: - # name: [String] - # Name to use for the log thread and the log name when logging any errors from this appender. - # # max_queue_size: [Integer] # The maximum number of log messages to hold on the queue before blocking attempts to add to the queue. # -1: The queue size is uncapped and will never block no matter how long the queue is. # Default: 10,000 # @@ -36,11 +33,10 @@ # # lag_check_interval: [Integer] # Number of messages to process before checking for slow logging. # Default: 1,000 def initialize(appender:, - name: appender.class.name, max_queue_size: 10_000, lag_check_interval: 1_000, lag_threshold_s: 30) @appender = appender @@ -64,17 +60,17 @@ # Returns [Thread] the worker thread. # # Starts the worker thread if not running. def thread - return @thread if @thread && @thread.alive? + return @thread if @thread&.alive? @thread = Thread.new { process } end # Returns true if the worker thread is active def active? - @thread && @thread.alive? + @thread&.alive? end # Add log message for processing. def log(log) queue << log @@ -97,30 +93,42 @@ # Separate thread for batching up log messages before writing. def process # This thread is designed to never go down unless the main thread terminates # or the appender is closed. Thread.current.name = logger.name - logger.trace "Async: Appender thread active" + logger.trace 'Async: Appender thread active' begin process_messages rescue StandardError => exception # This block may be called after the file handles have been released by Ruby - logger.error('Async: Restarting due to exception', exception) rescue nil + begin + logger.error('Async: Restarting due to exception', exception) + rescue StandardError + nil + end retry rescue Exception => exception # This block may be called after the file handles have been released by Ruby - logger.error('Async: Stopping due to fatal exception', exception) rescue nil + begin + logger.error('Async: Stopping due to fatal exception', exception) + rescue StandardError + nil + end ensure @thread = nil # This block may be called after the file handles have been released by Ruby - logger.trace('Async: Thread has stopped') rescue nil + begin + logger.trace('Async: Thread has stopped') + rescue StandardError + nil + end end end def process_messages count = 0 - while message = queue.pop + while (message = queue.pop) if message.is_a?(Log) appender.log(message) count += 1 # Check every few log messages whether this appender thread is falling behind if count > lag_check_interval @@ -148,13 +156,14 @@ end true end def check_lag(log) - if (diff = Time.now - log.time) > lag_threshold_s - logger.warn "Async: Appender thread has fallen behind by #{diff} seconds with #{queue.size} messages queued up. Consider reducing the log level or changing the appenders" - end + diff = Time.now - log.time + return unless diff > lag_threshold_s + + logger.warn "Async: Appender thread has fallen behind by #{diff} seconds with #{queue.size} messages queued up. Consider reducing the log level or changing the appenders" end # Submit command and wait for reply def submit_request(command) return false unless active? @@ -163,17 +172,16 @@ msg = "Async: Queued log messages: #{queue_size}, running command: #{command}" if queue_size > 1_000 logger.warn msg elsif queue_size > 100 logger.info msg - elsif queue_size > 0 + elsif queue_size.positive? logger.trace msg end reply_queue = Queue.new queue << {command: command, reply_queue: reply_queue} reply_queue.pop end - end end end