Class: Emissary::Logger
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
-
#level ⇒ Object
Returns the value of attribute level.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(mode, log_level, name = nil) ⇒ Logger
constructor
A new instance of Logger.
- #level_to_s ⇒ Object
- #level_to_sym ⇒ Object
- #log(log_level, message, *args) ⇒ Object
- #loggable?(log_level) ⇒ Boolean
- #normalize(log_level) ⇒ Object
- #syslogger ⇒ Object
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
#level ⇒ Object
Returns the value of attribute level.
61 62 63 |
# File 'lib/emissary/logger.rb', line 61 def level @level end |
#mode ⇒ Object
Returns the value of attribute mode.
61 62 63 |
# File 'lib/emissary/logger.rb', line 61 def mode @mode end |
#name ⇒ Object
Returns the value of attribute name.
61 62 63 |
# File 'lib/emissary/logger.rb', line 61 def name @name end |
Class Method Details
Instance Method Details
#level_to_s ⇒ Object
88 89 90 |
# File 'lib/emissary/logger.rb', line 88 def level_to_s level_to_sym.to_s.upcase end |
#level_to_sym ⇒ Object
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, , *args) case @mode when Logger::LOG_SYSLOG = "#{CONSTANT_ID_MAP[log_level].first.to_s.upcase}: #{}".gsub(/\t/, ' '*8).split(/\n/) # some syslog daemons have a hard time with newlines, so split into multiple lines .each do || syslogger { |s| s.log(log_level, , *args) } end when Logger::LOG_STDOUT $stdout << sprintf( << "\n", *args) unless not loggable? log_level when Logger::LOG_STDERR $stderr << sprintf( << "\n", *args) unless not loggable? log_level end self end |
#loggable?(log_level) ⇒ 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 |
#syslogger ⇒ Object
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 |