Module: SemanticLogger::Appender

Defined in:
lib/semantic_logger/appender.rb,
lib/semantic_logger/appender/io.rb,
lib/semantic_logger/appender/tcp.rb,
lib/semantic_logger/appender/udp.rb,
lib/semantic_logger/appender/file.rb,
lib/semantic_logger/appender/http.rb,
lib/semantic_logger/appender/loki.rb,
lib/semantic_logger/appender/async.rb,
lib/semantic_logger/appender/kafka.rb,
lib/semantic_logger/appender/sentry.rb,
lib/semantic_logger/appender/splunk.rb,
lib/semantic_logger/appender/syslog.rb,
lib/semantic_logger/appender/bugsnag.rb,
lib/semantic_logger/appender/graylog.rb,
lib/semantic_logger/appender/mongodb.rb,
lib/semantic_logger/appender/wrapper.rb,
lib/semantic_logger/appender/rabbitmq.rb,
lib/semantic_logger/appender/new_relic.rb,
lib/semantic_logger/appender/opensearch.rb,
lib/semantic_logger/appender/honeybadger.rb,
lib/semantic_logger/appender/sentry_ruby.rb,
lib/semantic_logger/appender/splunk_http.rb,
lib/semantic_logger/appender/text_output.rb,
lib/semantic_logger/appender/elasticsearch.rb,
lib/semantic_logger/appender/new_relic_logs.rb,
lib/semantic_logger/appender/open_telemetry.rb,
lib/semantic_logger/appender/cloudwatch_logs.rb,
lib/semantic_logger/appender/elasticsearch_base.rb,
lib/semantic_logger/appender/elasticsearch_http.rb,
lib/semantic_logger/appender/tcp_client_options.rb,
lib/semantic_logger/appender/honeybadger_insights.rb

Defined Under Namespace

Modules: TcpClientOptions, TextOutput Classes: Async, Bugsnag, CloudwatchLogs, Elasticsearch, ElasticsearchBase, ElasticsearchHttp, File, Graylog, Honeybadger, HoneybadgerInsights, Http, IO, Kafka, Loki, MongoDB, NewRelic, NewRelicLogs, OpenTelemetry, Opensearch, Rabbitmq, Sentry, SentryRuby, Splunk, SplunkHttp, Syslog, Tcp, Udp, Wrapper

Class Method Summary collapse

Class Method Details

.factory(async: false, batch: nil, max_queue_size: 10_000, lag_check_interval: 1_000, lag_threshold_s: 30, batch_size: 300, batch_seconds: 5, non_blocking: false, dropped_message_report_seconds: 30, async_max_retries: 100, **args) ⇒ Object

Returns [SemanticLogger::Subscriber] appender for the supplied options

Raises ArgumentError when the appender is to be batched, with batch: true or by default, see Subscriber#batch_by_default?, and it says why it cannot be, see Subscriber#batch_refusal. When it fails after .build, it first closes the appender that .build says it created, see .close_created.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/semantic_logger/appender.rb', line 41

def self.factory(async: false, batch: nil,
                 max_queue_size: 10_000, lag_check_interval: 1_000, lag_threshold_s: 30,
                 batch_size: 300, batch_seconds: 5,
                 non_blocking: false, dropped_message_report_seconds: 30,
                 async_max_retries: 100,
                 **args,
                 &)
  appender, created = build(**args, &)

  # If appender implements #batch, then it should use the batch proxy by default,
  # unless the appender opts out of batching by default (e.g. the HTTP appender).
  batch    = true if batch.nil? && appender.respond_to?(:batch) && appender.batch_by_default?

  if batch == true
    refusal = appender.batch_refusal
    raise(ArgumentError, "add_appender cannot batch #{appender.class}: #{refusal}") if refusal

    Appender::Async.new(
      appender:                       appender,
      batch:                          true,
      max_queue_size:                 max_queue_size,
      lag_threshold_s:                lag_threshold_s,
      batch_size:                     batch_size,
      batch_seconds:                  batch_seconds,
      non_blocking:                   non_blocking,
      dropped_message_report_seconds: dropped_message_report_seconds,
      async_max_retries:              async_max_retries
    )
  elsif async == true
    Appender::Async.new(
      appender:                       appender,
      max_queue_size:                 max_queue_size,
      lag_check_interval:             lag_check_interval,
      lag_threshold_s:                lag_threshold_s,
      non_blocking:                   non_blocking,
      dropped_message_report_seconds: dropped_message_report_seconds,
      async_max_retries:              async_max_retries
    )
  else
    appender
  end
rescue Exception
  close_created(appender) if created
  raise
end

.internal_logger(logger) ⇒ Object

Returns [SemanticLogger::Base] the supplied logger, ready to be an internal logger: the one that Semantic Logger reports its own problems to (see Processor.logger=).

An internal logger is named after whatever reports to it, logs at :trace, and takes an exception after the message, as every Semantic Logger appender does. So an appender, such as the default IO appender, or any other logger that logs at :trace, is returned as it is. Any other logger, such as a Ruby or Rails Logger, does none of that, so it is wrapped in a Wrapper appender, which writes each report to it as text, with the exception. Its own level then decides which reports it writes, apart from the :trace reports, which are below its lowest level, :debug, so are left out.

Returns nil for nil or false, which restores the default internal logger.

Raises ArgumentError for a SemanticLogger::Logger: it logs back into the processor, so the report of an appender that fails would go to that appender, fail again, and be reported again.



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/semantic_logger/appender.rb', line 160

def self.internal_logger(logger)
  return unless logger

  if logger.is_a?(SemanticLogger::Logger)
    raise(ArgumentError,
          "A SemanticLogger::Logger cannot be the internal logger, since its reports of a failing " \
          "appender would be logged to that appender. Use an appender, such as " \
          "SemanticLogger::Appender::IO.new($stderr), or a Ruby Logger.")
  end
  return logger if logger.respond_to?(:trace)

  SemanticLogger::Appender::Wrapper.new(logger: logger, level: :debug)
end