class Omnibus::Logger
Constants
- LEFT
The amount of padding on the left column.
@return [Fixnum]
- LEVELS
Our custom log levels, in order of severity
@return [Array]
- MUTEX
The mutex lock for synchronizing IO writing.
@return [Mutex]
Attributes
Public Class Methods
Create a new logger object.
@param [IO] io
the IO object to read/write
# File lib/omnibus/logger.rb, line 52 def initialize(io = $stdout) @io = io @level = LEVELS.index("WARN") end
Public Instance Methods
Add a message to the logger with the given severity and progname.
# File lib/omnibus/logger.rb, line 103 def add(severity, progname, &block) return true if io.nil? || severity < level message = format_message(severity, progname, yield) MUTEX.synchronize { io.write(message) } true end
Print a deprecation warning. This actually outputs to WARN
, but is prefixed with the string “DEPRECATED” first.
@see (Logger#add
)
# File lib/omnibus/logger.rb, line 71 def deprecated(progname, &block) meta = Proc.new { "DEPRECATED: #{yield}" } add(LEVELS.index("WARN"), progname, &meta) end
The detailed string representation of this object.
@return [String]
# File lib/omnibus/logger.rb, line 125 def inspect "#<#{self.class.name} level: #{@level}>" end
Set the log lever for this logger instance.
@example
logger.level = :info
@param [Symbol] level
# File lib/omnibus/logger.rb, line 84 def level=(level) @level = LEVELS.index(level.to_s.upcase) || -1 end
The live stream for this logger.
@param [Symbol] level
@return [LiveStream]
# File lib/omnibus/logger.rb, line 95 def live_stream(level = :debug) @live_streams ||= {} @live_streams[level.to_sym] ||= LiveStream.new(self, level) end
The string representation of this object.
@return [String]
# File lib/omnibus/logger.rb, line 116 def to_s "#<#{self.class.name}>" end
Private Instance Methods
Format the log message.
@return [String]
# File lib/omnibus/logger.rb, line 136 def format_message(severity, progname, message) if progname left = "[#{progname}] #{format_severity(severity)} | " else left = "#{format_severity(severity)} | " end "#{left.rjust(LEFT)}#{Time.now.iso8601} | #{message}\n" end
Format the log severity.
@return [String]
# File lib/omnibus/logger.rb, line 150 def format_severity(severity) if severity == 0 "_" else (LEVELS[severity] || "?")[0] end end