Class: EventMachine::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/em-logger/logger.rb,
lib/em-logger/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Logger

Returns a new instance of Logger.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/em-logger/logger.rb', line 20

def initialize(logger)
  @logger = logger
  @logger_queue = EM::Queue.new

  queue_processor = Proc.new do |log_message|
    @logger.add(log_message.severity, log_message.message, log_message.progname)
    EM.defer { @logger_queue.pop(&queue_processor) }
  end

  @logger_queue.pop(&queue_processor)

  EM.add_shutdown_hook { drain } if EM.reactor_running?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



82
83
84
85
# File 'lib/em-logger/logger.rb', line 82

def method_missing(method, *args, &block)
  return super unless @logger.respond_to?(method)
  @logger.send(method, *args, &block)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/em-logger/logger.rb', line 17

def logger
  @logger
end

#logger_queueObject (readonly)

Returns the value of attribute logger_queue.



18
19
20
# File 'lib/em-logger/logger.rb', line 18

def logger_queue
  @logger_queue
end

Instance Method Details

#<<(data) ⇒ Object



78
79
80
# File 'lib/em-logger/logger.rb', line 78

def <<(data)
  @logger_queue.push(LogMessage.new(nil,data))
end

#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/em-logger/logger.rb', line 34

def add(severity, message = nil, progname = nil, &block)
  return true if severity < @logger.level
  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      progname = @logger.progname
    end
  end
  @logger_queue.push(LogMessage.new(severity, message, progname))
end

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

Log a +DEBUG+ message.



49
50
51
# File 'lib/em-logger/logger.rb', line 49

def debug(progname = nil, &block)
  add(::Logger::DEBUG, nil, progname, &block)
end

#drainObject



91
92
93
94
95
96
97
98
# File 'lib/em-logger/logger.rb', line 91

def drain
  drain_processor = Proc.new do |log_message|
    @logger.add(log_message.severity, log_message.message, log_message.progname)
  end
  while not @logger_queue.empty? do
    @logger_queue.pop(&drain_processor)
  end
end

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

Log a +ERROR+ message.



64
65
66
# File 'lib/em-logger/logger.rb', line 64

def error(progname = nil, &block)
  add(::Logger::ERROR, nil, progname, &block)
end

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

Log a +FATAL+ message.



69
70
71
# File 'lib/em-logger/logger.rb', line 69

def fatal(progname = nil, &block)
  add(::Logger::FATAL, nil, progname, &block)
end

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

Log a +INFO+ message.



54
55
56
# File 'lib/em-logger/logger.rb', line 54

def info(progname = nil, &block)
  add(::Logger::INFO, nil, progname, &block)
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/em-logger/logger.rb', line 87

def respond_to?(method, include_private = false)
  @logger.respond_to?(method, include_private) || super(method, include_private)
end

#unknown(progname = nil, &block) ⇒ Object

Log an +UNKNOWN+ message.



74
75
76
# File 'lib/em-logger/logger.rb', line 74

def unknown(progname = nil, &block)
  add(::Logger::UNKNOWN, nil, progname, &block)
end

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

Log a +WARN+ message.



59
60
61
# File 'lib/em-logger/logger.rb', line 59

def warn(progname = nil, &block)
  add(::Logger::WARN, nil, progname, &block)
end