Class | Yell::Adapters::Base |
In: |
lib/yell/adapters/base.rb
|
Parent: | Monitor |
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 {Yell::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 |options| self.format = options[:format] end write do |event| message = format.call(event) STDOUT.puts message 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!"
Define your close method with this helper.
@example Closing a file handle
close do @stream.close end
Define your open method with this helper.
@example Open a file handle
open do @stream = ::File.open( 'test.log', ::File::WRONLY|::File::APPEND|::File::CREAT ) end
Setup your adapter with this helper method.
@example
setup do |options| @file_handle = File.new( '/dev/null', 'w' ) end
Define your write method with this helper.
@example Printing messages to file
write do |event| @file_handle.puts event.message end
Close the adapter (stream, connection, etc).
Adapter classes should provide their own implementation of this method.
The main method for calling the adapter.
The method receives the log `event` and determines whether to actually write or not.