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
# File 'lib/pliny/log.rb', line 42

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

#default_contextObject



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

def default_context
  @default_context || {}
end

#default_context=(default_context) ⇒ Object



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

def default_context=(default_context)
  @default_context = default_context
end

#log(_data, &block) ⇒ Object



5
6
7
# File 'lib/pliny/log.rb', line 5

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

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



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 17

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

  # Log backtrace in reverse order for easier digestion.
  e.backtrace&.reverse&.each do |backtrace|
    log_to_stream(stderr || $stderr, merge_log_contexts(
      exception_id: exception_id,
      backtrace: backtrace,
    ),)
  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



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

def log_scrubber
  @scrubber
end

#log_scrubber=(scrubber) ⇒ Object



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

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



9
10
11
# File 'lib/pliny/log.rb', line 9

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

#log_without_context(data, &block) ⇒ Object



13
14
15
# File 'lib/pliny/log.rb', line 13

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

#stderrObject



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

def stderr
  @stderr
end

#stderr=(stream) ⇒ Object



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

def stderr=(stream)
  @stderr = stream
end

#stdoutObject



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

def stdout
  @stdout
end

#stdout=(stream) ⇒ Object



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

def stdout=(stream)
  @stdout = stream
end