Class: RTKIT::Logging::ClassMethods::ProxyLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rtkit/logging.rb

Overview

We use our own ProxyLogger to achieve the features wanted for RTKIT logging, e.g. using RTKIT as progname for messages logged within the RTKIT 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 RTKIT module.

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ ProxyLogger

Creating the ProxyLogger instance.

Parameters

  • target – A Logger instance (e.g. Standard Logger or ActiveSupport::BufferedLogger).



58
59
60
# File 'lib/rtkit/logging.rb', line 58

def initialize(target)
  @target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

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).

Examples

# Inside the RTKIT module or an external class with 'include RTKIT::Logging':
logger.info "message"

# Calling from outside the RTKIT module:
RTKIT.logger.info "message"


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rtkit/logging.rb', line 78

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, "RTKIT: #{args.first}")
    elsif block_given?
      @target.send(method_name, *args) { yield }
    else
      @target.send(method_name, "RTKIT") { args.first }
    end
  else
    @target.send(method_name, *args, &block)
  end
end