Class: SemanticLogger::Appender::ElasticsearchBase
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- SemanticLogger::Appender::ElasticsearchBase
- Defined in:
- lib/semantic_logger/appender/elasticsearch_base.rb
Overview
Abstract base appender for Elasticsearch-compatible search engines.
Implements the shared bulk-indexing pipeline used by both the Elasticsearch and OpenSearch appenders. Subclasses only need to supply the backing client class.
This class is internal: applications add an appender via
SemanticLogger.add_appender(appender: :elasticsearch, ...) or
appender: :opensearch, never by referencing this class directly.
Direct Known Subclasses
Constant Summary
Constants inherited from Subscriber
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
-
#client ⇒ Object
Returns the value of attribute client.
-
#client_args ⇒ Object
Returns the value of attribute client_args.
-
#date_pattern ⇒ Object
Returns the value of attribute date_pattern.
-
#flush_interval ⇒ Object
Returns the value of attribute flush_interval.
-
#index ⇒ Object
Returns the value of attribute index.
-
#timeout_interval ⇒ Object
Returns the value of attribute timeout_interval.
-
#url ⇒ Object
Returns the value of attribute url.
Attributes inherited from Subscriber
#application, #environment, #formatter, #host, #logger, #metrics
Instance Method Summary collapse
- #batch(logs) ⇒ Object
-
#batch_all_or_nothing? ⇒ Boolean
The whole batch is formatted into a single bulk request before anything is sent.
-
#initialize(url: "http://localhost:9200", index: "semantic_logger", date_pattern: "%Y.%m.%d", type: nil, level: nil, formatter: nil, filter: nil, application: nil, environment: nil, host: nil, data_stream: false, metrics: false, **client_args) ⇒ ElasticsearchBase
constructor
Create an Elasticsearch-compatible appender over persistent HTTP(S).
-
#inspect_details ⇒ Object
The url without its credentials, and the index.
-
#log(log) ⇒ Object
Log to the index for today, or to the data stream.
-
#reopen ⇒ Object
The url is left to the client gem to parse, since it also takes a comma separated list of urls, an Array of them, or hosts without a scheme.
Methods inherited from Subscriber
#batch_by_default?, #batch_refusal, #close, #console_output?, #console_stream, #fails_any_batch?, #flush, #inspect, #message_error?, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?
Constructor Details
#initialize(url: "http://localhost:9200", index: "semantic_logger", date_pattern: "%Y.%m.%d", type: nil, level: nil, formatter: nil, filter: nil, application: nil, environment: nil, host: nil, data_stream: false, metrics: false, **client_args) ⇒ ElasticsearchBase
Create an Elasticsearch-compatible appender over persistent HTTP(S).
Parameters:
index: [String]
Prefix of the index to store the logs in.
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'
date_pattern: [String]
The time format used to generate the full index name. Useful
if you want monthly indexes ('%Y.%m') or weekly ('%Y.%W').
Default: '%Y.%m.%d'
data_stream: [true|false]
Write to the data stream named by `index:`, with `create` actions,
rather than to a daily index: `date_pattern:` is not used, and the
default formatter writes the time under `@timestamp`.
Default: false
type: [String]
Deprecated and ignored. Document `_type` has been unused since
Elasticsearch 7 and is ignored by OpenSearch. 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. It returns the document, as a Hash, or as a
String of JSON.
Default: :raw, with an ISO 8601 time under `timestamp`, or `@timestamp` with
`data_stream: true`.
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
metrics: [true|false]
Whether to also index metric-only entries: a metric without a message or an exception.
Default: false
Client Parameters (passed through to the backing client):
url: [String]
Fully qualified address to the service.
Default: 'http://localhost:9200'
hosts: [String|Hash|Array]
Single host passed as a String or Hash, or multiple hosts
passed as an Array. A Hash takes the keys `host`, `port`,
`scheme`, `user`, `password`, and `path`.
Note:
:url above is ignored when supplying this option.
A `host:` passed to the appender itself, outside the Hash, is
not a server: it is the host name written in each log entry,
see `host:` above.
resurrect_after [Float]
After how many seconds a dead connection should be tried again.
reload_connections [true|false|Integer]
Reload connections after X requests.
Default: false
randomize_hosts [true|false]
Shuffle connections on initialization and reload.
Default: false
sniffer_timeout [Integer]
Timeout for reloading connections in seconds.
Default: 1
retry_on_failure [true|false|Integer]
Retry X times when request fails before raising and exception.
Default: false
retry_on_status [Array<Number>]
Retry when specific status codes are returned.
reload_on_failure [true|false]
Reload connections after failure.
Default: false
request_timeout [Integer]
The request timeout to be passed to transport in options.
adapter [Symbol]
A specific adapter for Faraday (e.g. `:patron`)
transport_options [Hash]
Options to be passed to the `Faraday::Connection` constructor.
transport_class [Constant]
A specific transport class to use, will be initialized by
the client and passed hosts and all arguments.
transport [Object]
A specific transport instance.
serializer_class [Constant]
A specific serializer class to use, will be initialized by
the transport and passed the transport instance.
selector
An instance of a connection selector strategy.
send_get_body_as [String]
Specify the HTTP method to use for GET requests with a body.
Default: 'GET'
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 152 def initialize(url: "http://localhost:9200", index: "semantic_logger", date_pattern: "%Y.%m.%d", type: nil, level: nil, formatter: nil, filter: nil, application: nil, environment: nil, host: nil, data_stream: false, metrics: false, **client_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 @url = url @index = index @date_pattern = date_pattern @client_args = client_args.dup @client_args[:url] = url if url && !client_args[:hosts] @client_args[:logger] = logger @data_stream = data_stream super(level: level, formatter: formatter, filter: filter, application: application, environment: environment, host: host, metrics: metrics, &) reopen end |
Instance Attribute Details
#batch_size ⇒ Object
Returns the value of attribute batch_size.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def batch_size @batch_size end |
#client ⇒ Object
Returns the value of attribute client.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def client @client end |
#client_args ⇒ Object
Returns the value of attribute client_args.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def client_args @client_args end |
#date_pattern ⇒ Object
Returns the value of attribute date_pattern.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def date_pattern @date_pattern end |
#flush_interval ⇒ Object
Returns the value of attribute flush_interval.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def flush_interval @flush_interval end |
#index ⇒ Object
Returns the value of attribute index.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def index @index end |
#timeout_interval ⇒ Object
Returns the value of attribute timeout_interval.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def timeout_interval @timeout_interval end |
#url ⇒ Object
Returns the value of attribute url.
27 28 29 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 27 def url @url end |
Instance Method Details
#batch(logs) ⇒ Object
198 199 200 201 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 198 def batch(logs) bulk_write(logs.map { |log| bulk_entry(log) }) true end |
#batch_all_or_nothing? ⇒ Boolean
The whole batch is formatted into a single bulk request before anything is sent.
204 205 206 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 204 def batch_all_or_nothing? true end |
#inspect_details ⇒ Object
The url without its credentials, and the index. See Subscriber#inspect.
209 210 211 212 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 209 def inspect_details url_detail = @client_args.key?(:url) ? "url=#{Utils.redacted_url(url).inspect} " : "" "#{url_detail}index=#{index.inspect}" end |
#log(log) ⇒ Object
Log to the index for today, or to the data stream.
193 194 195 196 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 193 def log(log) bulk_write([bulk_entry(log)]) true end |
#reopen ⇒ Object
The url is left to the client gem to parse, since it also takes a comma separated list of urls, an Array of them, or hosts without a scheme.
188 189 190 |
# File 'lib/semantic_logger/appender/elasticsearch_base.rb', line 188 def reopen @client = Utils.without_url_in_errors(option: ":url or :hosts") { client_class.new(@client_args) } end |