Class: DataLogger::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/datalogger/logger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component = 'app') ⇒ Logger

Returns a new instance of Logger.



17
18
19
20
21
# File 'lib/datalogger/logger.rb', line 17

def initialize(component='app')
  self.component = component
  self.sinks = []
  self.sinks << Sinks::STDOUT.new
end

Instance Attribute Details

#componentObject

Returns the value of attribute component.



3
4
5
# File 'lib/datalogger/logger.rb', line 3

def component
  @component
end

#sinksObject

Returns the value of attribute sinks.



3
4
5
# File 'lib/datalogger/logger.rb', line 3

def sinks
  @sinks
end

Class Method Details

.component=(component) ⇒ Object



9
10
11
# File 'lib/datalogger/logger.rb', line 9

def self.component=(component)
  global_logger.component = component
end

.global_loggerObject



5
6
7
# File 'lib/datalogger/logger.rb', line 5

def self.global_logger
  @global_logger ||= new
end

.log(data, &blk) ⇒ Object



13
14
15
# File 'lib/datalogger/logger.rb', line 13

def self.log(data, &blk)
  global_logger.log(data, &blk)
end

Instance Method Details

#log(data, &blk) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/datalogger/logger.rb', line 23

def log(data, &blk)
  if data.kind_of?(String)
    data = { message: data }
  end
   
  if blk
    ret = nil
    start = Time.now
    log_to_sinks(data.merge at: 'start')
    ret = yield
    log_to_sinks(data.merge at: 'finish', elapsed: (Time.now - start))
    ret
  else
    log_to_sinks(data)
    nil
  end
end

#log_to_sinks(data) ⇒ Object



41
42
43
44
45
# File 'lib/datalogger/logger.rb', line 41

def log_to_sinks(data)
  self.sinks.each do |sink|
    sink.log(self.component, data)
  end
end