class RTP::Logging::ClassMethods::ProxyLogger

We use our own ProxyLogger to achieve the features wanted for RTP logging, e.g. using RTP as progname for messages logged within the RTP module (for both the Standard logger as well as the Rails logger), while still allowing a custom progname to be used when the logger is called outside the RTP module.

Public Class Methods

new(target) click to toggle source

Creating the ProxyLogger instance.

@param [Logger] target a logger instance (e.g. Standard Logger or ActiveSupport::BufferedLogger)

# File lib/rtp-connect/logging.rb, line 57
def initialize(target)
  @target = target
end

Public Instance Methods

method_missing(method_name, *args) { || ... } click to toggle source

Catches missing methods.

In our case, the methods of interest are the typical logger methods, i.e. log, info, fatal, error, debug, where the arguments/block are redirected to the logger in a specific way so that our stated logger features are achieved (this behaviour depends on the logger (Rails vs Standard) and in the case of Standard logger, whether or not a block is given).

@example Inside the RTP module or an external class with 'include RTP::Logging':

logger.info "message"

@example Calling from outside the RTP module:

RTP.logger.info "message"
# File lib/rtp-connect/logging.rb, line 76
def method_missing(method_name, *args, &block)
  if method_name.to_s =~ /(log|debug|info|warn|error|fatal)/
    # Rails uses it's own buffered logger which does not
    # work with progname + block as the standard logger does:
    if defined?(Rails)
      @target.send(method_name, "RTP: #{args.first}")
    elsif block_given?
      @target.send(method_name, *args) { yield }
    else
      @target.send(method_name, "RTP") { args.first }
    end
  else
    @target.send(method_name, *args, &block)
  end
end