Class: Furnish::Logger
- Inherits:
-
Object
- Object
- Furnish::Logger
- Defined in:
- lib/furnish/logger.rb
Overview
Furnish::Logger is a thread safe, auto-flushing, IO-delegating logger with numeric level control.
See Furnish::Logger::Mixins for functionality you can add to your provisioners to deal with loggers easily.
Example:
# debug level is 0
logger = Furnish::Logger.new($stderr, 0)
# IO methods are sent straight to the IO object, synchronized by a
# mutex:
logger.puts "foo"
logger.print "foo"
# if_debug is a way to scope log writes:
# this will never run because debug level is 0
logger.if_debug(1) do
# self is the IO object here
puts "foo"
end
logger.if_debug(0) do # this will run
puts "foo"
end
logger.debug_level = 2
# if_debug's parameter merely must equal or be less than the debug
# level to process.
logger.if_debug(1) do # will run
puts "bar"
end
Defined Under Namespace
Modules: Mixins
Instance Attribute Summary collapse
-
#debug_level ⇒ Object
Set the debug level - adjustable after creation.
-
#io ⇒ Object
readonly
The IO object.
Instance Method Summary collapse
-
#if_debug(level = 1, else_block = nil, &block) ⇒ Object
Runs the block if the level is equal to or lesser than the Furnish::Logger#debug_level.
-
#initialize(logger_io = $stderr, debug_level = 0) ⇒ Logger
constructor
Create a new Furnish::Logger.
-
#method_missing(sym, *args) ⇒ Object
Delegates to the Furnish::Logger#io if possible.
Constructor Details
#initialize(logger_io = $stderr, debug_level = 0) ⇒ Logger
Create a new Furnish::Logger. Takes an IO object and an Integer debug level. See Furnish::Logger class documentation for more information.
71 72 73 74 75 76 |
# File 'lib/furnish/logger.rb', line 71 def initialize(logger_io=$stderr, debug_level=0) @write_mutex = Mutex.new @io = logger_io @io.sync = true @debug_level = debug_level end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
Delegates to the Furnish::Logger#io if possible. If not possible, raises a NoMethodError. All calls are synchronized over the logger’s mutex.
109 110 111 112 113 114 115 |
# File 'lib/furnish/logger.rb', line 109 def method_missing(sym, *args) raise NoMethodError, "#{io.inspect} has no method #{sym}" unless io.respond_to?(sym) run = lambda { io.__send__(sym, *args) } @write_mutex.synchronize { run.call } rescue ThreadError run.call end |
Instance Attribute Details
#debug_level ⇒ Object
Set the debug level - adjustable after creation.
59 60 61 |
# File 'lib/furnish/logger.rb', line 59 def debug_level @debug_level end |
#io ⇒ Object (readonly)
The IO object. Probably best to not mess with this attribute directly, most methods will be proxied to it.
65 66 67 |
# File 'lib/furnish/logger.rb', line 65 def io @io end |
Instance Method Details
#if_debug(level = 1, else_block = nil, &block) ⇒ Object
Runs the block if the level is equal to or lesser than the Furnish::Logger#debug_level. The default debug level is 1.
The block runs in the context of the Furnish::Logger#io object, that is, ‘self` is the IO object.
If an additional proc is applied, will run that if the debug block would not fire, effectively creating an else. Generally an anti-pattern, but is useful in a few situations.
if_debug is synchronized over the logger’s mutex.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/furnish/logger.rb', line 91 def if_debug(level=1, else_block=nil, &block) run = lambda do if debug_level >= level and block io.instance_eval(&block) elsif else_block io.instance_eval(&else_block) end end @write_mutex.synchronize { run.call } rescue ThreadError run.call end |