Class: SemanticLogger::Appender::ElasticsearchHttp

Inherits:
Http
  • Object
show all
Defined in:
lib/semantic_logger/appender/elasticsearch_http.rb

Constant Summary

Constants inherited from Http

Http::MAX_LOGGED_RESPONSE_BODY, Http::NOT_SENT_ERRORS, Http::URL_FORMAT_HINT

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Http

#compress, #continue_timeout, #header, #http, #open_timeout, #path, #port, #proxy_url, #query, #read_timeout, #server, #ssl_options, #url, #username

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Instance Method Summary collapse

Methods inherited from Http

#batch, #batch_all_or_nothing?, #batch_by_default?, #close, #fails_any_batch?, #inspect_details, #message_error?, #not_sent?, #redacted_url, #reopen, #wrote_nothing?

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #close, #console_output?, #console_stream, #default_formatter, #fails_any_batch?, #flush, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?

Constructor Details

#initialize(index: "semantic_logger", type: nil, url: "http://localhost:9200", **http_args) ⇒ ElasticsearchHttp

Create Elasticsearch appender over persistent HTTP(S)

Parameters:

index: [String]
Prefix of the index to store the logs in Elasticsearch.
The final index appends the date so that indexes are used per day.
  I.e. The final index will look like 'semantic_logger-YYYY.MM.DD'
Default: 'semantic_logger'

type: [String]
Deprecated and ignored. Document `_type` has been unused since
Elasticsearch 7. Passing it logs a deprecation warning; it will be
removed in v6.

level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: none (:trace), so it writes every entry that a logger emits; each logger
filters by its own level.

formatter: [Object|Proc|Symbol|Hash]
An instance of a class that implements #call, or a Proc to be used to format
the output from this appender
Default: Use the built-in formatter (See: #call)

filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied.
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
      The Proc must return true or false.

host: [String]
Name of this host to appear in log messages.
Default: SemanticLogger.host

application: [String]
Name of this application to appear in log messages.
Default: SemanticLogger.application


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/semantic_logger/appender/elasticsearch_http.rb', line 56

def initialize(index: "semantic_logger",
               type: nil,
               url: "http://localhost:9200",
               **http_args,
               &)
  if type
    Deprecation.warn(
      "The Elasticsearch/OpenSearch appender `type:` parameter is deprecated and will be removed in v6. " \
      "Document `_type` has been unused since Elasticsearch 7 and is ignored by OpenSearch."
    )
  end

  @index = index
  super(url: url, **http_args, &)

  @request_path = "#{@path.end_with?('/') ? @path : "#{@path}/"}#{@index}-%Y.%m.%d"
  @logging_path = "#{@request_path}/_doc"
end

Instance Attribute Details

#index ⇒ Object

Returns the value of attribute index.



17
18
19
# File 'lib/semantic_logger/appender/elasticsearch_http.rb', line 17

def index
  @index
end

Instance Method Details

#batch_refusal ⇒ Object

Elasticsearch indexes a batch of documents only through its bulk API, which the :elasticsearch appender uses. The #batch inherited from Appender::Http posts the batch as a JSON array to the url's own path, such as /, which Elasticsearch rejects, so add_appender refuses to batch it.

Answers only while that #batch is the one in use (see Subscriber#batch_defined_by?), so that a subclass with a #batch of its own, such as one that posts the batch to the bulk API, is batched as before. One whose #batch calls super still posts a JSON array, so it defines its own #batch_refusal.



93
94
95
96
97
98
99
# File 'lib/semantic_logger/appender/elasticsearch_http.rb', line 93

def batch_refusal
  return unless batch_defined_by?(Http)

  "Elasticsearch indexes a batch of entries only through its bulk API, which this appender does not " \
    "use. Use appender: :elasticsearch to send the entries in batches, or add this appender without " \
    "batch: true."
end

#delete_all(date = Date.today) ⇒ Object

Deletes all log data captured for a day.



81
82
83
# File 'lib/semantic_logger/appender/elasticsearch_http.rb', line 81

def delete_all(date = Date.today)
  delete(date.strftime(@request_path))
end

#log(log) ⇒ Object

Log to the index for today.



76
77
78
# File 'lib/semantic_logger/appender/elasticsearch_http.rb', line 76

def log(log)
  post(format_log(log), log.time.strftime(@logging_path))
end