Class: Unilogger::Logger
- Inherits:
-
Object
- Object
- Unilogger::Logger
- 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
-
#emitters ⇒ Object
readonly
Returns the value of attribute emitters.
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
- #<<(message) ⇒ Object
-
#as_json ⇒ Object
each.
- #as_json_helper(it) ⇒ Object
- #emit(details, message, options) ⇒ Object
-
#initialize(level = DEBUG, emitters = []) ⇒ Logger
constructor
A new instance of Logger.
- #to_json ⇒ Object
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
#emitters ⇒ Object (readonly)
Returns the value of attribute emitters.
14 15 16 |
# File 'lib/unilogger/logger.rb', line 14 def emitters @emitters end |
#level ⇒ Object
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 <<( ) details = { :pri_sym => :INFO, :pri_num => Logger::Severity::INFO, :pid => Process.pid, :time => Time.now } self.emit( details, , nil ) end |
#as_json ⇒ Object
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, , ) @emitters.each { |e| e.emit( details, , ) } end |
#to_json ⇒ Object
86 87 88 |
# File 'lib/unilogger/logger.rb', line 86 def to_json as_json.to_json end |