Class: Emissary::Logger

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

Constant Summary collapse

LOG_SYSLOG =
0x01
LOG_STDOUT =
0x02
LOG_STDERR =
0x03
EMERGENCY =
Syslog::Constants::LOG_EMERG
EMERG =
Syslog::Constants::LOG_EMERG
ALERT =
Syslog::Constants::LOG_ALERT
FATAL =
Syslog::Constants::LOG_CRIT
CRITICAL =
Syslog::Constants::LOG_CRIT
CRIT =
Syslog::Constants::LOG_CRIT
ERROR =
Syslog::Constants::LOG_ERR
ERR =
Syslog::Constants::LOG_ERR
WARNING =
Syslog::Constants::LOG_WARNING
WARN =
Syslog::Constants::LOG_WARNING
NOTICE =
Syslog::Constants::LOG_NOTICE
INFO =
Syslog::Constants::LOG_INFO
DEBUG =
Syslog::Constants::LOG_DEBUG
CONSTANT_ID_MAP =
{
  EMERGENCY => [ :emergency, :emerg ],
  ALERT     => [ :alert ],
  CRITICAL  => [ :fatal, :critical, :crit ],
  ERROR     => [ :error, :err ],
  WARNING   => [ :warning, :warn ],
  NOTICE    => [ :notice ],
  INFO      => [ :info ],
  DEBUG     => [ :debug ]
}
CONSTANT_NAME_MAP =
CONSTANT_ID_MAP.inject({}) do |mappings,(id,names)|
  names.each { |name| mappings.merge!({ name => id, name.to_s.upcase => id, name.to_s.downcase => id }) }
  mappings
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, log_level, name = nil) ⇒ Logger

Returns a new instance of Logger.



69
70
71
72
73
74
# File 'lib/emissary/logger.rb', line 69

def initialize mode, log_level, name = nil
	@mode  = mode || LOG_STDERR
	@level = log_level || INFO
    @name  = name
    @mutex = Mutex.new
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



61
62
63
# File 'lib/emissary/logger.rb', line 61

def level
  @level
end

#modeObject

Returns the value of attribute mode.



61
62
63
# File 'lib/emissary/logger.rb', line 61

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/emissary/logger.rb', line 61

def name
  @name
end

Class Method Details

.instance(mode = LOG_STDERR, log_level = NOTICE, name = nil) ⇒ Object



63
64
65
# File 'lib/emissary/logger.rb', line 63

def self.instance(mode = LOG_STDERR, log_level = NOTICE, name = nil)
	@@logger ||= Emissary::Logger.new mode, log_level
end

Instance Method Details

#level_to_sObject



88
89
90
# File 'lib/emissary/logger.rb', line 88

def level_to_s
  level_to_sym.to_s.upcase
end

#level_to_symObject



84
85
86
# File 'lib/emissary/logger.rb', line 84

def level_to_sym
  CONSTANT_ID_MAP[@level].first
end

#log(log_level, message, *args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/emissary/logger.rb', line 105

def log(log_level, message, *args)
	case @mode
		when Logger::LOG_SYSLOG
        messages = "#{CONSTANT_ID_MAP[log_level].first.to_s.upcase}: #{message}".gsub(/\t/, ' '*8).split(/\n/)
        # some syslog daemons have a hard time with newlines, so split into multiple lines
			messages.each do |message|
          syslogger { |s| s.log(log_level, message, *args) }
			end
		when Logger::LOG_STDOUT
			$stdout << sprintf(message << "\n", *args) unless not loggable? log_level
		when Logger::LOG_STDERR
			$stderr << sprintf(message << "\n", *args) unless not loggable? log_level
	end
	
	self
end

#loggable?(log_level) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/emissary/logger.rb', line 92

def loggable?(log_level)
	log_level <= normalize(@level)
end

#normalize(log_level) ⇒ Object



78
79
80
81
82
# File 'lib/emissary/logger.rb', line 78

def normalize log_level
  return log_level unless not log_level.kind_of? Fixnum
  return CONSTANT_NAME_MAP[log_level.to_s.downcase] rescue INFO
  return INFO
end

#sysloggerObject



96
97
98
99
100
101
102
103
# File 'lib/emissary/logger.rb', line 96

def syslogger
  @mutex.synchronize {
    Syslog.open(name, Syslog::LOG_PID | Syslog::LOG_NDELAY, Syslog::LOG_DAEMON) do |s|
      s.LOG_UPTO(level)
      yield s
    end
  }
end