Class: SemanticLogger::Appender::Elasticsearch

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/appender/elasticsearch.rb

Instance Attribute Summary collapse

Attributes inherited from Subscriber

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

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #console_output?, #flush, #level, #should_log?

Methods inherited from Base

#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags

Constructor Details

#initialize(url: "http://localhost:9200", index: "semantic_logger", date_pattern: "%Y.%m.%d", type: "log", level: nil, formatter: nil, filter: nil, application: nil, environment: nil, host: nil, data_stream: false, **elasticsearch_args, &block) ⇒ Elasticsearch

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'

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'

type: [String]
  Document type to associate with logs when they are written.
  Deprecated in Elasticsearch 7.0.0.
  Default: 'log'

level: [:trace | :debug | :info | :warn | :error | :fatal]
  Override the log level for this appender.
  Default: SemanticLogger.default_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: :raw_json (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

Elasticsearch Parameters:

url: [String]
  Fully qualified address to the Elasticsearch service.
  Default: 'http://localhost:9200'

hosts: [String|Hash|Array]
  Single host passed as a String or Hash, or multiple hosts
  passed as an Array; `host` or `url` keys are also valid.
  Note:
    :url above is ignored when supplying this option.

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 [Elasticsearch::Transport::Transport::Connections::Selector::Base]
  An instance of selector strategy derived from `Elasticsearch::Transport::Transport::Connections::Selector::Base`.

send_get_body_as [String]
  Specify the HTTP method to use for GET requests with a body.
  Default: 'GET'


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/semantic_logger/appender/elasticsearch.rb', line 129

def initialize(url: "http://localhost:9200",
               index: "semantic_logger",
               date_pattern: "%Y.%m.%d",
               type: "log",
               level: nil,
               formatter: nil,
               filter: nil,
               application: nil,
               environment: nil,
               host: nil,
               data_stream: false,
               **elasticsearch_args,
               &block)

  @url                         = url
  @index                       = index
  @date_pattern                = date_pattern
  @type                        = type
  @elasticsearch_args          = elasticsearch_args.dup
  @elasticsearch_args[:url]    = url if url && !elasticsearch_args[:hosts]
  @elasticsearch_args[:logger] = logger
  @data_stream = data_stream

  super(level: level, formatter: formatter, filter: filter, application: application, environment: environment, host: host, metrics: false, &block)
  reopen
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



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

def batch_size
  @batch_size
end

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#date_patternObject

Returns the value of attribute date_pattern.



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

def date_pattern
  @date_pattern
end

#elasticsearch_argsObject

Returns the value of attribute elasticsearch_args.



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

def elasticsearch_args
  @elasticsearch_args
end

#flush_intervalObject

Returns the value of attribute flush_interval.



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

def flush_interval
  @flush_interval
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#timeout_intervalObject

Returns the value of attribute timeout_interval.



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

def timeout_interval
  @timeout_interval
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#batch(logs) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/semantic_logger/appender/elasticsearch.rb', line 167

def batch(logs)
  messages = []
  logs.each do |log|
    messages << bulk_index(log) << formatter.call(log, self)
  end

  write_to_elasticsearch(messages)
  true
end

#log(log) ⇒ Object

Log to the index for today



161
162
163
164
165
# File 'lib/semantic_logger/appender/elasticsearch.rb', line 161

def log(log)
  bulk_payload = formatter.call(log, self)
  write_to_elasticsearch([bulk_index(log), bulk_payload])
  true
end

#reopenObject



156
157
158
# File 'lib/semantic_logger/appender/elasticsearch.rb', line 156

def reopen
  @client = ::Elasticsearch::Client.new(@elasticsearch_args)
end