Class: Urbivore::MessagePacket

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/urbivore/message_packet.rb

Constant Summary

Constants included from Interface

Interface::STANDARD_LEVELS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interface

#add, #append_message, #available_levels, #current_levels, #default_level, #default_levels, #default_logger, #generate_message_object, #levels_by_minimum, #levels_by_subset

Constructor Details

#initialize(options = {}) ⇒ MessagePacket

Returns a new instance of MessagePacket.



19
20
21
22
23
24
25
26
# File 'lib/urbivore/message_packet.rb', line 19

def initialize(options = {})
  @logger          = options[:default_level] || self.class.logger || default_logger
  @default_level   = options[:default_level]
  @message_store   = {}
  unless @logger.ancestors.include?(Urbivore::Logger)
    raise Urbivore::Exceptions::TypeError.new("MessagePacket expected a Logger")
  end
end

Instance Attribute Details

#loggerObject (readonly)

instance methods



17
18
19
# File 'lib/urbivore/message_packet.rb', line 17

def logger
  @logger
end

Class Method Details

.logger(logger = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/urbivore/message_packet.rb', line 6

def logger(logger = nil)
  if logger
    @logger = logger
  else
    @logger
  end
end

Instance Method Details

#message_collector(message_object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/urbivore/message_packet.rb', line 28

def message_collector(message_object)
  # it may not matter... but keying these in a hash by level 
  # seems like better design than putting them in a big pile
  # and using #select---if you generate a massive amount of debugging
  # info you don't want iterate over all of it just to generate a production
  # log message, right?
  @message_store[level] ||= []
  # shit like this needs to be made threadsafe
  @message_store[level] << message_object
end

#messages(requested = nil) ⇒ Object



39
40
41
42
# File 'lib/urbivore/message_packet.rb', line 39

def messages(requested = nil)
  # pull all desired levels and collate
  current_levels(requested).map { |level| @levels[level] }.flatten.sort
end

#submitObject

send self to the logging machine



45
46
47
48
49
50
51
52
# File 'lib/urbivore/message_packet.rb', line 45

def submit
  if @submitted 
    false
  else
    logger.submit(self)
    @submitted = true
  end
end

#submit!Object

as above, but raise exception when trying to submit an already submitted log



56
57
58
59
60
61
62
# File 'lib/urbivore/message_packet.rb', line 56

def submit!
  if @submitted
    raise Urbivore::Exceptions::RuntimeError.new("MessagePacket already submitted")
  else
    submit
  end
end