Class: SemanticLogger::QueueProcessor::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/queue_processor/reporter.rb

Overview

Internal: reports what happens on the worker thread of a queue processor, its progress and its problems, to the internal logger of its appender.

It knows that logger, which is the appender's to choose, and makes sure that a report neither stops the worker thread nor raises into a calling thread, whatever the logger. It counts the log messages dropped because the queue was full, and those discarded because they could not be written, and reports each count at most once per interval.

Instance Method Summary collapse

Constructor Details

#initialize(appender) ⇒ Reporter

Returns a new instance of Reporter.



15
16
17
18
19
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 15

def initialize(appender)
  @appender = appender
  @dropped  = ThrottledCounter.new
  @failed   = ThrottledCounter.new(reported_at: nil)
end

Instance Method Details

#dropped(max_queue_size, stopped:, interval:) ⇒ Object

Record a log message dropped because the queue was full, reporting the running count at most once every interval seconds. The report gives the reason for the latest drop: a queue too small for the volume of messages, or, when stopped, a worker thread that has stopped for good, so that nothing empties the queue, however large.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 54

def dropped(max_queue_size, stopped:, interval:)
  @dropped.record(interval: interval) do |count, seconds|
    advice =
      if stopped
        "The worker thread has stopped after failing to write (see async_max_retries), so nothing " \
          "empties the queue until SemanticLogger.reopen(force: true) starts it again"
      else
        "The queue is full (max_queue_size: #{max_queue_size}). " \
          "Consider reducing the log level, increasing max_queue_size, or changing the appenders"
      end
    log(:warn, "Async: Dropped #{count} log messages in the last #{seconds.round} seconds. #{advice}")
  end
end

#dropped_count ⇒ Object

Returns [Integer] the number of log messages dropped because the queue was full.



25
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 25

def dropped_count = @dropped.total

#failed(logs, exception, interval:) ⇒ Object

Record log messages discarded because they could not be written, whether their content or the destination was at fault, if any. The first is reported straight away, with the error. After that the count is reported, with the latest error, at most once every interval seconds.



89
90
91
92
93
94
95
96
97
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 89

def failed(logs, exception, interval:)
  return if logs.empty?

  @failed.record(logs.size, interval: interval) do |count, seconds|
    period = seconds ? " in the last #{seconds.round} seconds" : ""
    log(:error, "Async: Discarded #{count} log message(s)#{period} that could not be written, " \
                "most recently from #{logs.first.name}", exception)
  end
end

#failed_count ⇒ Object

Returns [Integer] the number of log messages discarded because they could not be written, whether their content or the destination was at fault (see Subscriber#message_error?).



29
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 29

def failed_count = @failed.total

#lag(entry, threshold, queued) ⇒ Object

Report that the worker thread has fallen behind, when the log message it took off the queue was logged more than threshold seconds ago, with the number of messages still queued.



70
71
72
73
74
75
76
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 70

def lag(entry, threshold, queued)
  diff = Time.now - entry.time
  return unless diff > threshold

  log(:warn, "Async: Appender thread has fallen behind by #{diff} seconds with #{queued} messages " \
             "queued up. Consider reducing the log level or changing the appenders")
end

#log(level, message, exception = nil) ⇒ Object

Log to the internal logger, ignoring any StandardError. These calls may run after Ruby has released file handles during shutdown, and the internal logger may not take them, such as a Ruby Logger, which has no #trace, returned by a custom appender's own #logger.

Such a logger also takes only the message. So the exception is passed only when there is one, and is added to the message instead for a logger that does not take it (see #takes_exception?).



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 37

def log(level, message, exception = nil)
  internal = logger
  return internal.public_send(level, message) unless exception
  return internal.public_send(level, message, exception) if takes_exception?(internal, level)

  internal.public_send(
    level,
    Utils.join_texts(message, " -- ", Utils.exception_name(exception), ": ", Utils.exception_message(exception))
  )
rescue StandardError
  nil
end

#logger ⇒ Object

Returns the internal logger of the appender, which it can replace at any time.



22
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 22

def logger = @appender.logger

#queued(count, command) ⇒ Object

Report the number of log messages queued ahead of a command, such as a flush, that is being submitted: at :warn for more than 1,000, at :info for more than 100, otherwise at :trace, and not at all for none.



81
82
83
84
# File 'lib/semantic_logger/queue_processor/reporter.rb', line 81

def queued(count, command)
  level = (count > 1_000 && :warn) || (count > 100 && :info) || (count.positive? && :trace)
  log(level, "Async: Queued log messages: #{count}, running command: #{command}") if level
end