lib/semantic_logger/logger.rb in semantic_logger-3.2.1 vs lib/semantic_logger/logger.rb in semantic_logger-3.3.0
- old
+ new
@@ -42,16 +42,32 @@
end
# Flush all queued log entries disk, database, etc.
# All queued log messages are written and then each appender is flushed in turn
def self.flush
- return false unless appender_thread_active?
+ msg = "Flushing appenders with #{queue_size} log messages on the queue"
+ if queue_size > 1_000
+ logger.warn msg
+ elsif queue_size > 100
+ logger.info msg
+ elsif queue_size > 0
+ logger.trace msg
+ end
+ process_request(:flush)
+ end
- logger.trace "Flushing appenders with #{queue_size} log messages on the queue"
- reply_queue = Queue.new
- queue << {command: :flush, reply_queue: reply_queue}
- reply_queue.pop
+ # Close all appenders and flush any outstanding messages
+ def self.close
+ msg = "Closing appenders with #{queue_size} log messages on the queue"
+ if queue_size > 1_000
+ logger.warn msg
+ elsif queue_size > 100
+ logger.info msg
+ elsif queue_size > 0
+ logger.trace msg
+ end
+ process_request(:close)
end
@@lag_check_interval = 5000
@@lag_threshold_s = 30
@@ -71,14 +87,10 @@
# to determine if the appender thread is falling behind
def self.lag_threshold_s
@@lag_threshold_s
end
- def self.time_threshold_s=(time_threshold_s)
- @@lag_threshold_s = time_threshold_s
- end
-
# Allow the internal logger to be overridden from its default to STDERR
# Can be replaced with another Ruby logger or Rails logger, but never to
# SemanticLogger::Logger itself since it is for reporting problems
# while trying to log to the various appenders
def self.logger=(logger)
@@ -109,11 +121,11 @@
# Backward compatibility
options = options.is_a?(Hash) ? options.dup : {appender: options}
appender = block || options.delete(:appender)
# Convert symbolized metrics appender to an actual object
- appender = named_appender(appender, 'SemanticLogger::Appender::Metrics').new(options) if appender.is_a?(Symbol)
+ appender = SemanticLogger.constantize_symbol(appender, 'SemanticLogger::Metrics').new(options) if appender.is_a?(Symbol)
raise('When supplying a metrics appender, it must support the #call method') unless appender.is_a?(Proc) || appender.respond_to?(:call)
(@@metric_subscribers ||= Concurrent::Array.new) << appender
end
@@ -202,10 +214,24 @@
end
end
message[:reply_queue] << true if message[:reply_queue]
logger.trace 'Appender thread: All appenders flushed'
+ when :close
+ SemanticLogger.appenders.each do |appender|
+ begin
+ logger.trace "Appender thread: Closing appender: #{appender.name}"
+ appender.flush
+ appender.close
+ SemanticLogger.remove_appender(appender)
+ rescue Exception => exc
+ logger.error "Appender thread: Failed to close appender: #{appender.inspect}", exc
+ end
+ end
+
+ message[:reply_queue] << true if message[:reply_queue]
+ logger.trace 'Appender thread: All appenders flushed'
else
logger.warn "Appender thread: Ignoring unknown command: #{message[:command]}"
end
end
end
@@ -238,9 +264,18 @@
subscriber.call(log)
rescue Exception => exc
logger.error 'Exception calling metrics subscriber', exc
end
end
+ end
+
+ # Close all appenders and flush any outstanding messages
+ def self.process_request(command)
+ return false unless appender_thread_active?
+
+ reply_queue = Queue.new
+ queue << {command: command, reply_queue: reply_queue}
+ reply_queue.pop
end
end
end