Class: Omnibus::Logger

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

Defined Under Namespace

Classes: LiveStream

Constant Summary collapse

LEFT =

The amount of padding on the left column.

Returns:

  • (Fixnum)
30
LEVELS =

Our custom log levels, in order of severity

Returns:

  • (Array)
%w{UNKNOWN INTERNAL DEBUG INFO WARN ERROR FATAL NOTHING}.freeze
MUTEX =

The mutex lock for synchronizing IO writing.

Returns:

  • (Mutex)
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout) ⇒ Logger

Create a new logger object.

Parameters:

  • io (IO) (defaults to: $stdout)

    the IO object to read/write



52
53
54
55
# File 'lib/omnibus/logger.rb', line 52

def initialize(io = $stdout)
  @io = io
  @level = LEVELS.index("WARN")
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



43
44
45
# File 'lib/omnibus/logger.rb', line 43

def io
  @io
end

#levelObject

Returns the value of attribute level.



44
45
46
# File 'lib/omnibus/logger.rb', line 44

def level
  @level
end

Instance Method Details

#add(severity, progname, &block) ⇒ Object

Add a message to the logger with the given severity and progname.



103
104
105
106
107
108
109
# 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) ⇒ Object

Print a deprecation warning. This actually outputs to WARN, but is prefixed with the string “DEPRECATED” first.

See Also:

  • Omnibus::Logger.(Logger(Logger#add)


71
72
73
74
# File 'lib/omnibus/logger.rb', line 71

def deprecated(progname, &block)
  meta = Proc.new { "DEPRECATED: #{yield}" }
  add(LEVELS.index("WARN"), progname, &meta)
end

#inspectString

The detailed string representation of this object.

Returns:

  • (String)


125
126
127
# File 'lib/omnibus/logger.rb', line 125

def inspect
  "#<#{self.class.name} level: #{@level}>"
end

#live_stream(level = :debug) ⇒ LiveStream

The live stream for this logger.

Parameters:

  • level (Symbol) (defaults to: :debug)

Returns:



95
96
97
98
# 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_sString

The string representation of this object.

Returns:

  • (String)


116
117
118
# File 'lib/omnibus/logger.rb', line 116

def to_s
  "#<#{self.class.name}>"
end