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.

40
LEVELS =

Our custom log levels, in order of severity

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

The mutex lock for synchronizing IO writing.

Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout) ⇒ Logger

Create a new logger object.



49
50
51
52
# File 'lib/omnibus/logger.rb', line 49

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

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io



40
41
42
# File 'lib/omnibus/logger.rb', line 40

def io
  @io
end

#levelObject

Returns the value of attribute level



41
42
43
# File 'lib/omnibus/logger.rb', line 41

def level
  @level
end

Instance Method Details

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

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



100
101
102
103
104
105
# File 'lib/omnibus/logger.rb', line 100

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)


68
69
70
71
# File 'lib/omnibus/logger.rb', line 68

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

#inspectString

The detailed string representation of this object.



121
122
123
# File 'lib/omnibus/logger.rb', line 121

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

#live_stream(level = :debug) ⇒ LiveStream

The live stream for this logger.



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

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.



112
113
114
# File 'lib/omnibus/logger.rb', line 112

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