Module: PortAuthority::Logger

Extended by:
Logger
Included in:
Logger
Defined in:
lib/port-authority/logger.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &_block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/port-authority/logger.rb', line 23

def method_missing(name, *args, &_block)
  if name
    return log(name.to_sym, args[0])
  else
    fail(NoMethodError, "Unknown Logger method '#{name}'", caller)
  end
end

Instance Method Details

#debug(message) ⇒ Object



19
20
21
# File 'lib/port-authority/logger.rb', line 19

def debug(message)
  log :debug, message if @debug
end

#debug!Object



15
16
17
# File 'lib/port-authority/logger.rb', line 15

def debug!
  @debug = !@debug
end

#init!(s) ⇒ Object



9
10
11
12
13
# File 'lib/port-authority/logger.rb', line 9

def init!(s)
  @_s = s
  @debug = Config.debug
  Syslog.open($0, Syslog::LOG_PID, Syslog::LOG_DAEMON) if Config.syslog
end

#log(lvl, msg) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/port-authority/logger.rb', line 31

def log(lvl, msg)
  if Config.syslog
    case lvl
    when :debug
      l = Syslog::LOG_DEBUG
    when :info
      l = Syslog::LOG_INFO
    when :notice
      l = Syslog::LOG_NOTICE
    when :error
      l = Syslog::LOG_ERR
    when :alert
      l = Syslog::LOG_ALERT
    end
    @_s.synchronize do
      Syslog.log(l, "(%s) %s", Thread.current[:name], msg.to_s)
    end
  else
    @_s.synchronize do
      $stdout.puts [Time.now.to_s, sprintf('%-6.6s', lvl.to_s.upcase), "(#{Thread.current[:name]})", msg.to_s].join(' ')
      $stdout.flush
    end
  end
end