Class: Unilogger::Logger

Inherits:
Object
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/unilogger/logger.rb

Overview

Adds pid and timestamp to each log entry. Implements IO API (<<) which is equivalent to info() Implements Logger API (debug, info, warn, error, fatal)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = DEBUG, emitters = []) ⇒ Logger

Returns a new instance of Logger.



16
17
18
19
# File 'lib/unilogger/logger.rb', line 16

def initialize( level = DEBUG, emitters = [] )
  @level    = level
  @emitters = emitters.dup
end

Instance Attribute Details

#emittersObject (readonly)

Returns the value of attribute emitters.



14
15
16
# File 'lib/unilogger/logger.rb', line 14

def emitters
  @emitters
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

Instance Method Details

#<<(message) ⇒ Object



25
26
27
28
# File 'lib/unilogger/logger.rb', line 25

def <<( message )
  details = { :pri_sym => :INFO, :pri_num => Logger::Severity::INFO, :pid => Process.pid, :time => Time.now }
  self.emit( details, message, nil )
end

#as_jsonObject

each



68
69
70
# File 'lib/unilogger/logger.rb', line 68

def as_json
  { :level => @level, :emitters => as_json_helper(@emitters) }
end

#as_json_helper(it) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/unilogger/logger.rb', line 72

def as_json_helper( it )
  if it.nil? then
    it
  elsif it.respond_to?(:as_json) then
    it.as_json
  elsif it.kind_of?(Hash) then
    it.inject({}) { |a,i| a[i.first] = as_json_helper(i.last); a }
  elsif it.kind_of?(Array) || it.kind_of?(Enumerable)
    it.map { |i| as_json_helper(i) }
  else
    it
  end
end

#emit(details, message, options) ⇒ Object



21
22
23
# File 'lib/unilogger/logger.rb', line 21

def emit( details, message, options )
  @emitters.each { |e| e.emit( details, message, options ) }
end

#to_jsonObject



86
87
88
# File 'lib/unilogger/logger.rb', line 86

def to_json
  as_json.to_json
end