Class: RailsLokiExporter::InterceptingLogger

Inherits:
ActiveSupport::Logger
  • Object
show all
Defined in:
lib/rails_loki_exporter/intercepting_logger.rb

Constant Summary collapse

SEVERITY_NAMES =
%w(DEBUG INFO WARN ERROR FATAL ANY).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(intercept_logs: false) ⇒ InterceptingLogger

Returns a new instance of InterceptingLogger.



13
14
15
16
17
18
# File 'lib/rails_loki_exporter/intercepting_logger.rb', line 13

def initialize(intercept_logs: false)
  @intercept_logs = intercept_logs
  @log = ""
  super(STDOUT)
  self.level = Logger::DEBUG
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/rails_loki_exporter/intercepting_logger.rb', line 9

def client
  @client
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_loki_exporter/intercepting_logger.rb', line 20

def add(severity, message = nil, progname = nil, &block)
  severity_name = severity_name(severity)
  log_message = message
  if log_message.nil?
    if block_given?
      log_message = yield
    else
      log_message = progname
      progname = @progname
    end
  end

  if @intercept_logs && !log_message.nil?
      formatted_message = format_message(severity_name, Time.now, progname, log_message)
      client.send_log(formatted_message) if client
  end

  super(severity, message, progname, &block)
end

#broadcast_to(console) ⇒ Object



40
41
42
# File 'lib/rails_loki_exporter/intercepting_logger.rb', line 40

def broadcast_to(console)
  client.send_log(@log) if client
end