Class: Yell::Adapters::Base
- Inherits:
-
Monitor
- Object
- Monitor
- Yell::Adapters::Base
- Includes:
- Helpers::Base, Helpers::Level
- Defined in:
- lib/yell/adapters/base.rb
Overview
This class provides the basic interface for all allowed operations on any adapter implementation. Other adapters should inherit from it for the methods used by the Logger.
Writing your own adapter is really simple. Inherit from the base class and use the ‘setup`, `write` and `close` methods. Yell requires the `write` method to be specified (`setup` and `close` are optional).
The following example shows how to define a basic Adapter to format and print log events to STDOUT:
class PutsAdapter < Yell::Adapters::Base
include Yell::Formatter::Helpers
setup do ||
self.format = [:format]
end
write do |event|
= format.call(event)
STDOUT.puts
end
end
After the Adapter has been written, we need to register it to Yell:
Yell::Adapters.register :puts, PutsAdapter
Now, we can use it like so:
logger = Yell.new :puts
logger.info "Hello World!"
Direct Known Subclasses
Class Method Summary collapse
-
.close(&block) ⇒ Object
Define your close method with this helper.
-
.open(&block) ⇒ Object
Define your open method with this helper.
-
.setup(&block) ⇒ Object
Setup your adapter with this helper method.
-
.write(&block) ⇒ Object
Define your write method with this helper.
Instance Method Summary collapse
-
#close ⇒ Object
Close the adapter (stream, connection, etc).
-
#initialize(options = {}, &block) ⇒ Base
constructor
Initializes a new Adapter.
-
#inspect ⇒ Object
Get a pretty string representation of the adapter, including.
-
#write(event) ⇒ Object
The main method for calling the adapter.
Methods included from Helpers::Level
Constructor Details
#initialize(options = {}, &block) ⇒ Base
Initializes a new Adapter.
You should not overload the constructor, use #setup instead.
138 139 140 141 142 143 144 145 146 |
# File 'lib/yell/adapters/base.rb', line 138 def initialize( = {}, &block ) super() # init the monitor superclass reset! setup!() # eval the given block block.arity > 0 ? block.call(self) : instance_eval(&block) if block_given? end |
Class Method Details
.close(&block) ⇒ Object
Define your close method with this helper.
84 85 86 |
# File 'lib/yell/adapters/base.rb', line 84 def close( &block ) compile!(:close!, &block) end |
.open(&block) ⇒ Object
Define your open method with this helper.
74 75 76 |
# File 'lib/yell/adapters/base.rb', line 74 def open( &block ) compile!(:open!, &block) end |
.setup(&block) ⇒ Object
Setup your adapter with this helper method.
54 55 56 |
# File 'lib/yell/adapters/base.rb', line 54 def setup( &block ) compile!(:setup!, &block) end |
.write(&block) ⇒ Object
Define your write method with this helper.
64 65 66 |
# File 'lib/yell/adapters/base.rb', line 64 def write( &block ) compile!(:write!, &block) end |
Instance Method Details
#close ⇒ Object
Close the adapter (stream, connection, etc).
Adapter classes should provide their own implementation of this method.
165 166 167 |
# File 'lib/yell/adapters/base.rb', line 165 def close close! end |
#inspect ⇒ Object
Get a pretty string representation of the adapter, including
170 171 172 173 |
# File 'lib/yell/adapters/base.rb', line 170 def inspect inspection = inspectables.map { |m| "#{m}: #{send(m).inspect}" } "#<#{self.class.name} #{inspection * ', '}>" end |
#write(event) ⇒ Object
The main method for calling the adapter.
The method receives the log ‘event` and determines whether to actually write or not.
152 153 154 155 156 157 158 159 |
# File 'lib/yell/adapters/base.rb', line 152 def write( event ) synchronize { write!(event) } if write?(event) rescue Exception => e # make sure the adapter is closed and re-raise the exception synchronize { close } raise(e) end |