Class: Logr::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/logr/entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, event = nil, metrics = [], message = nil) ⇒ Entry

Returns a new instance of Entry.



9
10
11
12
13
14
# File 'lib/logr/entry.rb', line 9

def initialize(logger, event=nil, metrics=[], message=nil)
  @logger = logger
  @event = event
  @metrics = metrics
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/logr/entry.rb', line 7

def message
  @message
end

#metricsObject (readonly)

Returns the value of attribute metrics.



7
8
9
# File 'lib/logr/entry.rb', line 7

def metrics
  @metrics
end

Instance Method Details

#debug(message = nil, &block) ⇒ Object



44
45
46
# File 'lib/logr/entry.rb', line 44

def debug(message=nil, &block)
  add(:debug, message, &block)
end

#error(message = nil, &block) ⇒ Object



56
57
58
# File 'lib/logr/entry.rb', line 56

def error(message=nil, &block)
  add(:error, message, &block)
end

#event(name = nil, tags = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/logr/entry.rb', line 16

def event(name=nil, tags={})
  if name.nil?
    @event
  else
    Entry.new(@logger, Event.new(name, tags), @metrics)
  end
end

#fatal(message = nil, &block) ⇒ Object



60
61
62
# File 'lib/logr/entry.rb', line 60

def fatal(message=nil, &block)
  add(:fatal, message, &block)
end

#info(message = nil, &block) ⇒ Object



48
49
50
# File 'lib/logr/entry.rb', line 48

def info(message=nil, &block)
  add(:info, message, &block)
end

#metric(name, value, type: "counter") ⇒ Object



31
32
33
34
# File 'lib/logr/entry.rb', line 31

def metric(name, value, type: "counter")
  metric = Metric.new(name, value, type)
  Entry.new(@logger, @event, @metrics + [metric])
end

#monitored(title, text) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/logr/entry.rb', line 36

def monitored(title, text)
  if @event.nil?
    raise 'No event to monitor. Please call #event first.'
  end
  event = @event.with(monitored: true, title: title, text: text)
  Entry.new(@logger, event, @metrics)
end

#to_hashObject



64
65
66
67
68
69
70
71
# File 'lib/logr/entry.rb', line 64

def to_hash
  result = {}
  result[:event] = @event.to_hash if @event
  result[:metrics] = @metrics.map(&:to_hash) if @metrics.any?
  result[:message] = @message if @message

  result
end

#warn(message = nil, &block) ⇒ Object



52
53
54
# File 'lib/logr/entry.rb', line 52

def warn(message=nil, &block)
  add(:warn, message, &block)
end

#with(tags = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/logr/entry.rb', line 24

def with(tags={})
  if @event.nil?
    raise 'No event to append to. Please call #event first.'
  end
  Entry.new(@logger, @event.with(tags), @metrics)
end