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

io[R]
level[R]

Public Class Methods

new(io = $stdout) click to toggle source

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(severity, progname) { || ... } click to toggle source

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
deprecated(progname, &block) click to toggle source

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
inspect() click to toggle source

The detailed string representation of this object.

@return [String]

# File lib/omnibus/logger.rb, line 125
def inspect
  "#<#{self.class.name} level: #{@level}>"
end
level=(level) click to toggle source

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
live_stream(level = :debug) click to toggle source

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
to_s() click to toggle source

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_message(severity, progname, message) click to toggle source

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_severity(severity) click to toggle source

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