Class: SemanticLogger::Appender::Loki

Inherits:
Http show all
Defined in:
lib/semantic_logger/appender/loki.rb

Constant Summary collapse

INGESTION_PATH =
"loki/api/v1/push".freeze

Instance Attribute Summary

Attributes inherited from Http

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

Attributes inherited from Subscriber

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

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Http

#reopen

Methods inherited from Subscriber

#close, #console_output?, #default_formatter, #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: nil, formatter: SemanticLogger::Formatters::Loki.new, header: {"Content-Type" => "application/json"}, path: INGESTION_PATH, **args, &block) ⇒ Loki

Create Grafana Loki appender.

Parameters:

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 send as a dimension.
  Default: SemanticLogger.host

application: [String]
  Name of this application to send as a dimension.
  Default: SemanticLogger.application

url: [String]
  Define the loki instance URL.
  Example: https://logs-prod-999.grafana.net
  Default: nil


37
38
39
40
41
42
43
44
45
# File 'lib/semantic_logger/appender/loki.rb', line 37

def initialize(url: nil,
               formatter: SemanticLogger::Formatters::Loki.new,
               header: {"Content-Type" => "application/json"},
               path: INGESTION_PATH,
               **args,
               &block)

  super(url: "#{url}/#{path}", formatter: formatter, header: header, **args, &block)
end

Instance Method Details

#batch(logs) ⇒ Object

Logs in batches



55
56
57
58
59
# File 'lib/semantic_logger/appender/loki.rb', line 55

def batch(logs)
  message = formatter.batch(logs, self)
  logger.trace(message)
  post(message)
end

#log(log) ⇒ Object



47
48
49
50
51
52
# File 'lib/semantic_logger/appender/loki.rb', line 47

def log(log)
  message = formatter.call(log, self)
  puts message
  logger.trace(message)
  post(message)
end