Class: Logg::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/logg/core.rb

Overview

A Dispatcher is a logger core implementation, providing logger’s definition and output methods. It’s not intented to be used directly but through the Er mixin, within a class.

Defined Under Namespace

Classes: Render

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

The Dispatcher default behavior relies on #method_missing. It sets both the message and a namespace, then auto-sends the order to output.



85
86
87
88
89
# File 'lib/logg/core.rb', line 85

def method_missing(meth, *args, &block)
  @namespace = meth.to_s
  @message   = (args.first.to_s == 'debug') ? nil : args.first.to_s
  self.send :output!
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



80
81
82
# File 'lib/logg/core.rb', line 80

def message
  @message
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



80
81
82
# File 'lib/logg/core.rb', line 80

def namespace
  @namespace
end

Instance Method Details

#as(method, &block) ⇒ Object

Define a custom logger, using a template. The template may be defined within the block as a (multi-line) string, or one may reference a file.

# do whatever you want with data or anything else, for instance,
send mails, tweet, then…

Inline templates (defined within the block) make use of #render_inline (indentation broken for the sake of example readibility):

logger.as(:custom) do |response|
  tpl = <<-TPL
    %h2 Query log report
    %span
      Statu:
      = data.status
    %span
      Response:
      = data.body
    %br/
  TPL
  puts render_inline(tpl, :as => :haml, :data => response)
end

With an external template, one should use the #render helper to, well, render the template file. The extension will be used to infer the proper rendering engine. If not provided or when a custom extension is used, one may declare the template syntax.

logger.as(:custom) do |data|
  # do whatever you want with data or anything else, then…
  out = render('my/template.erb', :data => data)
  # one may then use out to send mails, log to file, tweet…
end

logger.as(:custom) do |data|
  render('my/template', :as => :erb, :data => data)
end

See #render and #render_inline for more details.

TODO: memoize the Render instance somehow? Or find another trick to execute the block.

Raises:

  • (ArgumentError)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/logg/core.rb', line 138

def as(method, &block)
  raise ArgumentError, 'Missing mandatory block' unless block_given?

  method  = method.to_sym

  # Define the guard at class-level, if not already defined.
  if !eigenclass.respond_to?(method)
    eigenclass.send(:define_method, method) do |*args|
      Render.new.instance_exec(*args, &block)
    end
  end

  # Define the guard at instance-level by overriding #initialize, if not
  # already defined.
  eigenclass.send(:define_method, :new) do
    o = super
    if !o.respond_to?(method)
      o.send(:define_method, method) do |*args|
        Render.new.instance_exec(*args, &block)
      end
    end
    o
  end
end

#eigenclassObject



91
92
93
# File 'lib/logg/core.rb', line 91

def eigenclass
  class << self; self; end
end