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



34
35
36
37
38
39
40
41
# File 'lib/pliny/log.rb', line 34

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



47
48
49
# File 'lib/pliny/log.rb', line 47

def default_context
  @default_context || {}
end

#default_context=(default_context) ⇒ Object



43
44
45
# File 'lib/pliny/log.rb', line 43

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pliny/log.rb', line 7

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

#stderrObject



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

def stderr
  @stderr
end

#stderr=(stream) ⇒ Object



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

def stderr=(stream)
  @stderr = stream
end

#stdoutObject



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

def stdout
  @stdout
end

#stdout=(stream) ⇒ Object



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

def stdout=(stream)
  @stdout = stream
end