Module: Pliny::Log

Included in:
Pliny
Defined in:
lib/pliny/log.rb,
lib/template/spec/spec_support/log.rb

Instance Method Summary collapse

Instance Method Details

#context(data, &block) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/pliny/log.rb', line 42

def context(data, &block)
  old = local_context
  self.local_context = old.merge(data)
  res = block.call
ensure
  self.local_context = old
  res
end

#default_contextObject



55
56
57
# File 'lib/pliny/log.rb', line 55

def default_context
  @default_context || {}
end

#default_context=(default_context) ⇒ Object



51
52
53
# File 'lib/pliny/log.rb', line 51

def default_context=(default_context)
  @default_context = default_context
end

#log(_data, &block) ⇒ Object



3
4
5
# File 'lib/pliny/log.rb', line 3

def log(data, &block)
  log_to_stream(stdout || $stdout, merge_log_contexts(data), &block)
end

#log_exception(e, data = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pliny/log.rb', line 15

def log_exception(e, data = {})
  exception_id = e.object_id

  # Log backtrace in reverse order for easier digestion.
  if e.backtrace
    e.backtrace.reverse.each do |backtrace|
      log_to_stream(stderr || $stderr, merge_log_contexts(
        exception_id: exception_id,
        backtrace:    backtrace
      ))
    end
  end

  # then log the exception message last so that it's as close to the end of
  # a log trace as possible
  data.merge!(
    exception:    true,
    class:        e.class.name,
    message:      e.message,
    exception_id: exception_id
  )

  data[:status] = e.status if e.respond_to?(:status)

  log_to_stream(stderr || $stderr, merge_log_contexts(data))
end

#log_scrubberObject



82
83
84
# File 'lib/pliny/log.rb', line 82

def log_scrubber
  @scrubber
end

#log_scrubber=(scrubber) ⇒ Object



75
76
77
78
79
80
# File 'lib/pliny/log.rb', line 75

def log_scrubber=(scrubber)
  if scrubber && !scrubber.respond_to?(:call)
    raise(ArgumentError, "Must respond to 'call'")
  end
  @scrubber = scrubber
end

#log_with_default_context(data, &block) ⇒ Object



7
8
9
# File 'lib/pliny/log.rb', line 7

def log_with_default_context(data, &block)
  log_to_stream(stdout || $stdout, default_context.merge(data), &block)
end

#log_without_context(data, &block) ⇒ Object



11
12
13
# File 'lib/pliny/log.rb', line 11

def log_without_context(data, &block)
  log_to_stream(stdout || $stdout, data, &block)
end

#stderrObject



71
72
73
# File 'lib/pliny/log.rb', line 71

def stderr
  @stderr
end

#stderr=(stream) ⇒ Object



67
68
69
# File 'lib/pliny/log.rb', line 67

def stderr=(stream)
  @stderr = stream
end

#stdoutObject



63
64
65
# File 'lib/pliny/log.rb', line 63

def stdout
  @stdout
end

#stdout=(stream) ⇒ Object



59
60
61
# File 'lib/pliny/log.rb', line 59

def stdout=(stream)
  @stdout = stream
end