Module: Que::Utils::Logging

Included in:
Que
Defined in:
lib/que/utils/logging.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#internal_loggerObject

Returns the value of attribute internal_logger.



8
9
10
# File 'lib/que/utils/logging.rb', line 8

def internal_logger
  @internal_logger
end

#log_formatterObject



63
64
65
# File 'lib/que/utils/logging.rb', line 63

def log_formatter
  @log_formatter ||= JSON.method(:dump)
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/que/utils/logging.rb', line 8

def logger
  @logger
end

Instance Method Details

#get_logger(internal: false) ⇒ Object



57
58
59
60
61
# File 'lib/que/utils/logging.rb', line 57

def get_logger(internal: false)
  if l = internal ? internal_logger : logger
    l.respond_to?(:call) ? l.call : l
  end
end

#internal_log(event, object = nil) ⇒ Object

Logging method used specifically to instrument Que’s internals. There’s usually not an internal logger set up, so this method is generally a no- op unless the specs are running or someone turns on internal logging so we can debug an issue.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/que/utils/logging.rb', line 35

def internal_log(event, object = nil)
  if l = get_logger(internal: true)
    data = _default_log_data

    data[:internal_event] = Que.assert(Symbol, event)
    data[:object_id]      = object.object_id if object
    data[:t]              = Time.now.utc.iso8601(6)

    additional = Que.assert(Hash, yield)

    # Make sure that none of our log contents accidentally overwrite our
    # default data contents.
    expected_length = data.length + additional.length
    data.merge!(additional)
    Que.assert(expected_length == data.length) do
      "Bad internal logging keys in: #{additional.keys.inspect}"
    end

    l.info(JSON.dump(data))
  end
end

#log(event:, level: :info, **extra) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/que/utils/logging.rb', line 11

def log(event:, level: :info, **extra)
  data = _default_log_data
  data[:event] = Que.assert(Symbol, event)
  data.merge!(extra)

  if l = get_logger
    begin
      if output = log_formatter.call(data)
        l.send level, output
      end
    rescue => e
      msg =
        "Error raised from Que.log_formatter proc:" +
        " #{e.class}: #{e.message}\n#{e.backtrace}"

      l.error(msg)
    end
  end
end