Class: InfluxDB2::LoggingHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/influxdb2/client/utils/logging_helper.rb

Overview

Helper to easy use a logger across the library.

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ LoggingHelper

Returns a new instance of LoggingHelper.

Parameters:

  • logger (Logger)

    Logger used for logging. Disable logging by set to false.



25
26
27
# File 'lib/influxdb2/client/utils/logging_helper.rb', line 25

def initialize(logger)
  @logger = logger
end

Instance Method Details

#_headers(request, prefix) ⇒ Object



60
61
62
63
64
# File 'lib/influxdb2/client/utils/logging_helper.rb', line 60

def _headers(request, prefix)
  request.each do |k, v|
    log(:debug, "#{prefix} #{k}: #{k.casecmp('authorization').zero? ? '***' : v}")
  end
end

#after_request(http_version, code, message, headers, payload) ⇒ Object



54
55
56
57
58
# File 'lib/influxdb2/client/utils/logging_helper.rb', line 54

def after_request(http_version, code, message, headers, payload)
  log(:debug, "<- HTTP/#{http_version} #{code} #{message}")
  _headers(headers, '<-')
  log(:debug, "<- #{payload}") if payload
end

#before_request(uri, method, headers, payload) ⇒ Object



48
49
50
51
52
# File 'lib/influxdb2/client/utils/logging_helper.rb', line 48

def before_request(uri, method, headers, payload)
  log(:debug, "-> #{method} #{uri}")
  _headers(headers, '->')
  log(:debug, "-> #{payload}") if payload
end

#log(level, message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/influxdb2/client/utils/logging_helper.rb', line 29

def log(level, message)
  return unless @logger

  log_level = case level
              when :debug then
                Logger::DEBUG
              when :warn then
                Logger::WARN
              when :error then
                Logger::ERROR
              when :fatal then
                Logger::FATAL
              else
                Logger::INFO
              end

  @logger.add(log_level) { message }
end