Class: LogSyringe::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/log_syringe/dsl.rb

Overview

DSL that is exposed for defining logging

Instance Method Summary collapse

Constructor Details

#initialize(klass, &block) ⇒ DSL


5
6
7
8
9
10
# File 'lib/log_syringe/dsl.rb', line 5

def initialize(klass, &block)
  @klass = klass
  @logging_layer = Module.new
  instance_eval(&block)
  klass.prepend(@logging_layer)
end

Instance Method Details

#log_method(name) {|logger, instance, stats| ... } ⇒ Object

Wrap a given method with logging

Examples:

log_method(:some_method) do |logger, instance, stats|
  logger.info(
    "some_method called on #{instance} with args #{stats[:args]}. " \
    "runtime: #{stats[:runtime]}"
  )
end

Yield Parameters:

  • logger (Logger)

    The global logger instance

  • instance

    The instance the method was called on

  • stats (Hash)

    Information about the method call. It contains the following information: +:args [Array]+ the arguments the method was called with, +:error [Error]+ the error that was raised (if any), +:result+ the return value of the method (if no exception was raised), +:runtime+ the runtime for the method (if no exception was raised).


29
30
31
32
33
34
35
# File 'lib/log_syringe/dsl.rb', line 29

def log_method(name, &block)
  visibility = visibility(name)
  define_logging_method(
    name, method(:measure_runtime), method(:logging), &block
  )
  @logging_layer.send(visibility, name)
end