Class: Logging::Proxy

### Home Classes Methods

### In Files

- lib/logging/proxy.rb

### Parent

Object

### Methods

- ::new
- #method_missing

### Files

- History.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Logging
- Logging::Appender
- Logging::Appenders
- Logging::Appenders::Buffering
- Logging::Appenders::Email
- Logging::Appenders::File
- Logging::Appenders::Growl
- Logging::Appenders::IO
- Logging::Appenders::RollingFile
- Logging::Appenders::Stderr
- Logging::Appenders::Stdout
- Logging::Appenders::StringIo
- Logging::Appenders::Syslog
- Logging::ColorScheme
- Logging::Config
- Logging::Config::Configurator
- Logging::Config::Configurator::DSL
- Logging::Config::Configurator::Error
- Logging::Config::Configurator::TopLevelDSL
- Logging::Config::YamlConfigurator
- Logging::Config::YamlConfigurator::Error
- Logging::Layout
- Logging::Layouts
- Logging::Layouts::Basic
- Logging::Layouts::Parseable
- Logging::Layouts::Pattern
- Logging::Logger
- Logging::MappedDiagnosticContext
- Logging::NestedDiagnosticContext
- Logging::Proxy
- Logging::RailsCompat
- Logging::Repository
- Logging::Repository::Singleton
- Logging::RootLogger
- Logging::Stats
- Logging::Stats::Sampler
- Logging::Stats::Tracker
- RSpec
- RSpec::Core
- RSpec::Core::Configuration
- RSpec::LoggingHelper
- RSpec::LoggingHelper
- Spec
- Spec::Runner
- Spec::Runner::Configuration
- Object
- Object::LoggingHelper
- File
- FileUtils
- Hash
- Kernel
- Module
- ReentrantMutex
- String

No matching classes.

# Logging::Proxy

Defines a Proxy that will log all method calls on the proxied object. This class uses `method_missing` on a "blank slate" object to intercept all method calls. The method name being called and the arguments are all logged to the proxied object's logger instance. The log level and other settings for the proxied object are honored by the Proxy instance.

If you want, you can also supply your own `method_missing` code as a block to the constructor.

    Proxy.new(object) do |name, *args, &block|
      # code to be executed before the proxied method
      result = @object.send(name, *args, &block)
      # code to be executed after the proxied method
      result   # <-- always return the result
    end

The proxied object is available as the "@object" variable. The logger is available as the "@logger" variable.

### Public Class Methods

new( object, &block ) click to toggle source

Create a new proxy for the given object. If an optional block is given it will be called before the proxied method. This block will replace the `method_missing` implementation

    # File lib/logging/proxy.rb, line 35
    def initialize( object, &block )
      Kernel.raise ArgumentError, "Cannot proxy nil" if nil.equal? object

      @object = object
      @leader = @object.is_a?(Class) ? "#{@object.name}." : "#{@object.class.name}#"
      @logger = Logging.logger[object]

      if block
        eigenclass = class << self; self; end
        eigenclass.__send__(:define_method, :method_missing, &block)
      end
    end

### Public Instance Methods

method_missing( name, *args, &block ) click to toggle source

All hail the magic of method missing. Here is where we are going to log the method call and then forward to the proxied object. The return value from the proxied objet method call is passed back.

    # File lib/logging/proxy.rb, line 52
    def method_missing( name, *args, &block )
      @logger << "#@leader#{name}(#{args.inspect[1..-2]})\n"
      @object.send(name, *args, &block)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.