Class: SemanticLogger::Subscriber

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_logger/subscriber.rb

Defined Under Namespace

Classes: FormatError

Constant Summary collapse

MESSAGE_ERRORS =

Errors raised by writing a log message whose content cannot be written, such as a value that cannot be serialized. Excludes JSON::ParserError, which comes from parsing a response rather than from the message, except its subclass JSON::NestingError, which JSON.generate raises for a value nested too deeply.

[FormatError, JSON::GeneratorError, JSON::NestingError, EncodingError, FloatDomainError].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#application ⇒ Object

Allow application name to be set globally or on a per subscriber basis.



36
37
38
# File 'lib/semantic_logger/subscriber.rb', line 36

def application
  @application || SemanticLogger.application
end

#environment ⇒ Object

Allow environment name to be set globally or on a per subscriber basis.



41
42
43
# File 'lib/semantic_logger/subscriber.rb', line 41

def environment
  @environment || SemanticLogger.environment
end

#formatter ⇒ Object

Every appender has its own formatter



12
13
14
# File 'lib/semantic_logger/subscriber.rb', line 12

def formatter
  @formatter
end

#host ⇒ Object

Allow host name to be set globally or on a per subscriber basis.



46
47
48
# File 'lib/semantic_logger/subscriber.rb', line 46

def host
  @host || SemanticLogger.host
end

#logger ⇒ Object

Give each appender its own logger for logging. For example trace messages sent to services or errors when something fails.



52
53
54
55
56
57
58
59
# File 'lib/semantic_logger/subscriber.rb', line 52

def logger
  @logger ||=
    begin
      logger      = SemanticLogger::Processor.logger.clone
      logger.name = self.class.name
      logger
    end
end

#metrics=(value) ⇒ Object (writeonly)

Sets the attribute metrics

Parameters:

  • value —

    the value to set the attribute metrics to.



13
14
15
# File 'lib/semantic_logger/subscriber.rb', line 13

def metrics=(value)
  @metrics = value
end

Instance Method Details

#batch_all_or_nothing? ⇒ Boolean

Whether #batch writes none of the log messages when it raises a message error (see #message_error?), for example because it formats every message before sending any.

When true, a batch that fails with a message error is split up and each part retried, so that only the offending message is discarded. When false, retrying could write some messages twice, so the whole batch is discarded instead.

Returns:

  • (Boolean)


164
165
166
# File 'lib/semantic_logger/subscriber.rb', line 164

def batch_all_or_nothing?
  false
end

#batch_by_default? ⇒ Boolean

Whether an appender that implements #batch should be wrapped in a batch proxy automatically. Appenders that support batching but should only batch when explicitly requested (via batch: true) override this to return false.

Returns:

  • (Boolean)


110
111
112
# File 'lib/semantic_logger/subscriber.rb', line 110

def batch_by_default?
  true
end

#batch_refusal ⇒ Object

Returns [String] why this appender cannot be batched, and what to do instead, or nil when it can. add_appender asks it before it batches the appender, with batch: true or by default, see #batch_by_default?, and raises ArgumentError with it, after "add_appender cannot batch : ", rather than add an appender whose worker thread fails on every batch, and so writes nothing.

Nil, since a #batch that writes each log message, formatted by #format_log, works with any formatter. A class whose own #batch cannot write a batch, with some formatters or at all, answers for that #batch, and only while it is the #batch in use (see #batch_defined_by?), so that a subclass that replaces it is batched as before. Appender::Http does, since its #batch formats the whole batch with #format_batch, which needs a formatter that can, see #format_batch_refusal. A subclass whose #batch calls super keeps that need, so it answers too, usually with #format_batch_refusal. One that inherits a #batch that its destination rejects, whatever the formatter, such as Appender::ElasticsearchHttp, says why, and what to use instead.



127
128
129
# File 'lib/semantic_logger/subscriber.rb', line 127

def batch_refusal
  nil
end

#close ⇒ Object

A subscriber should implement close if it can.



21
22
23
# File 'lib/semantic_logger/subscriber.rb', line 21

def close
  # NOOP
end

#console_output? ⇒ Boolean

Whether this appender is logging to stdout or stderr.

Returns:

  • (Boolean)


83
84
85
# File 'lib/semantic_logger/subscriber.rb', line 83

def console_output?
  !console_stream.nil?
end

#console_stream ⇒ Object

The console stream this appender writes to, if any. Returns one of :stdout, :stderr, or nil when not writing to a console stream.



78
79
80
# File 'lib/semantic_logger/subscriber.rb', line 78

def console_stream
  nil
end

#default_formatter ⇒ Object

Returns [SemanticLogger::Formatters::Default] default formatter for this subscriber.



31
32
33
# File 'lib/semantic_logger/subscriber.rb', line 31

def default_formatter
  SemanticLogger::Formatters::Default.new
end

#fails_any_batch?(_exception) ⇒ Boolean

Whether the supplied exception, a message error (see #message_error?) that #batch raised, would fail any batch, whatever messages it holds, such as a request the destination refuses for its credentials or its url. Asked only about a message error, and only when #batch_all_or_nothing? is true, or #rejected_entry names a message.

When true, the whole batch is discarded at once, rather than split up and each part written again to find the messages at fault, which would be refused in turn, about two writes for each message. False, since an error caused by the content of a message is caused by some of the messages in the batch.

Returns:

  • (Boolean)


177
178
179
# File 'lib/semantic_logger/subscriber.rb', line 177

def fails_any_batch?(_exception)
  false
end

#flush ⇒ Object

A subscriber should implement flush if it can.



16
17
18
# File 'lib/semantic_logger/subscriber.rb', line 16

def flush
  # NOOP
end

#inspect ⇒ Object

Returns [String] the class and object address of this appender, like Kernel#inspect, followed by #inspect_details, but none of its instance variables.

Appenders hold credentials in their instance variables: passwords and urls holding them, api keys and tokens, request headers, private keys, and the options passed to a client gem. Printing SemanticLogger.appenders, for example in a console or an error report, must not show them.



94
95
96
97
98
# File 'lib/semantic_logger/subscriber.rb', line 94

def inspect
  address = Kernel.instance_method(:to_s).bind_call(self).delete_suffix(">")
  details = inspect_details
  details ? "#{address} #{details}>" : "#{address}>"
end

#inspect_details ⇒ Object

Returns [String] what #inspect shows about this appender, such as its url without its credentials, or nil for nothing. Only an appender knows which of its settings are safe to show, so each one overrides this.



103
104
105
# File 'lib/semantic_logger/subscriber.rb', line 103

def inspect_details
  nil
end

#log(log) ⇒ Object

Method called to log an event

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/semantic_logger/subscriber.rb', line 26

def log(log)
  raise NotImplementedError
end

#message_error?(exception) ⇒ Boolean

Whether the supplied exception, raised by #log or #batch, was caused by the content of a log message, or is one that writing the message again cannot fix, such as a request the destination refuses outright, rather than by the destination being unavailable for now.

Waiting cannot write such a message, so an asynchronous appender discards it and carries on straight away. On any other exception it backs off before carrying on, since the destination may be briefly unavailable, then writes the message (or the batch) again when #wrote_nothing? says that the write wrote none of it (see QueueProcessor::HeldWrite::MAX_RESENDS), or otherwise carries on with the next one, having discarded it.

Every error raised by the formatter is one (see #format_log). An appender whose client raises its own errors for bad content can override this to recognize them.

Returns:

  • (Boolean)


154
155
156
# File 'lib/semantic_logger/subscriber.rb', line 154

def message_error?(exception)
  MESSAGE_ERRORS.any? { |error_class| exception.is_a?(error_class) }
end

#not_sent?(_exception) ⇒ Boolean

Whether #log or #batch, when it raised the supplied exception, sent nothing to the destination, such as when it could not connect to it, so that the destination has not answered the write. Asked only once #wrote_nothing? has returned true for the exception.

When true, writing the message, or batch, again after such a failure does not count towards QueueProcessor::HeldWrite::MAX_RESENDS: the limit is for a message the destination answers with a failure every time, such as a request it answers with a 429 that never clears, while one that cannot reach the destination during an outage is still written once it is back, as long as the worker thread keeps trying (async_max_retries). When false, every failure counts.

False, since only the appender can tell. Appender::Http answers it from the error.

Returns:

  • (Boolean)


230
231
232
# File 'lib/semantic_logger/subscriber.rb', line 230

def not_sent?(_exception)
  false
end

#rejected_entry(_exception) ⇒ Object

Returns [Integer|nil] the position, counted from 0, of the log message in the batch that the destination rejected, when the supplied exception, a message error (see #message_error?) that #batch raised, says so, and the destination stored every message before that one. Nil when it cannot tell, as by default.

When an Integer, the messages before that one are counted as written, that one is discarded, or, when the error would fail any batch (see #fails_any_batch?), it and every message after it, and the messages after it are written again as a batch. So none is written twice, and none that the destination did not reject is discarded, without splitting the batch to find the message at fault. Appender::SplunkHttp answers it from the HTTP Event Collector's response.



191
192
193
# File 'lib/semantic_logger/subscriber.rb', line 191

def rejected_entry(_exception)
  nil
end

#should_log?(log) ⇒ Boolean

Whether this log entry meets the criteria to be logged by this appender.

Returns:

  • (Boolean)


72
73
74
# File 'lib/semantic_logger/subscriber.rb', line 72

def should_log?(log)
  super && (log.metric_only? ? metrics? : true)
end

#wrote_nothing?(_exception) ⇒ Boolean

Whether #log or #batch, when it raised the supplied exception, wrote none of the log messages it was given, so that writing them again cannot write any of them twice, and writing them again later may succeed. Asked only about an exception that is not a message error (see #message_error?), such as one raised because the destination is unavailable.

When true, an asynchronous appender writes the same message, or batch, again once it has backed off. Once the destination has answered it with a failure after it was written again QueueProcessor::HeldWrite::MAX_RESENDS times, the appender discards it and carries on with the next one, unless it gives up after async_max_retries first. A write that never reached the destination (see #not_sent?) is not counted. When false, since the write may have written some or all of them, such as a request whose answer timed out after the destination stored it, or a batch written a message at a time, or since writing them again is bound to fail the same way, they are discarded instead. A flush or close does not wait for them: while one is waiting, they are discarded either way.

A #log or #batch that writes the messages in more than one request must return false for an error from any request after the first, which the destination may follow having stored the messages of the earlier ones.

False, since only the appender can tell. Appender::Http answers it from the error.

Returns:

  • (Boolean)


215
216
217
# File 'lib/semantic_logger/subscriber.rb', line 215

def wrote_nothing?(_exception)
  false
end