Class: Yell::Adapters::Base

Inherits:
Monitor
  • Object
show all
Includes:
Level::Helpers
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 |options|
    self.format = options[:format]
  end

  write do |event|
    message = format.format(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!"

Direct Known Subclasses

Io

Instance Attribute Summary

Attributes included from Level::Helpers

#level

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Base

Initializes a new Adapter.

You should not overload the constructor, use #setup instead.



137
138
139
140
141
142
# File 'lib/yell/adapters/base.rb', line 137

def initialize( options = {}, &block )
  super() # init the monitor superclass

  setup!(options)
  block.call(self) if block
end

Class Method Details

.close(&block) ⇒ Object

Define your close method with this helper.

Examples:

Closing a file handle

close do
  @stream.close
end


83
84
85
# File 'lib/yell/adapters/base.rb', line 83

def close( &block )
  compile!( :close!, &block )
end

.open(&block) ⇒ Object

Define your open method with this helper.

Examples:

Open a file handle

open do
  @stream = ::File.open( 'test.log', ::File::WRONLY|::File::APPEND|::File::CREAT )
end


73
74
75
# File 'lib/yell/adapters/base.rb', line 73

def open( &block )
  compile!( :open!, &block )
end

.setup(&block) ⇒ Object

Setup your adapter with this helper method.

Examples:

setup do |options|
  @file_handle = File.new( '/dev/null', 'w' )
end


53
54
55
# File 'lib/yell/adapters/base.rb', line 53

def setup( &block )
  compile!( :setup!, &block )
end

.write(&block) ⇒ Object

Define your write method with this helper.

Examples:

Printing messages to file

write do |event|
  @file_handle.puts event.message
end


63
64
65
# File 'lib/yell/adapters/base.rb', line 63

def write( &block )
  compile!( :write!, &block )
end

Instance Method Details

#closeObject

Close the adapter (stream, connection, etc).

Adapter classes should provide their own implementation of this method.



161
162
163
# File 'lib/yell/adapters/base.rb', line 161

def close
  close!
end

#inspectString

Get a pretty string representation of the adapter, including the inspectable attributes.

Examples:

Inspect the formatter.

adapter.inspect

Returns:

  • (String)

    The inspection string.



172
173
174
175
# File 'lib/yell/adapters/base.rb', line 172

def inspect
  inspection = inspectables.inject( [] ) { |r, c| r << "#{c}: #{send(c).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.



148
149
150
151
152
153
154
155
# File 'lib/yell/adapters/base.rb', line 148

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