Module: Ramaze::Logging

Included in:
Analogger, Informer, Knotify, LogHub, SpecLogger, Xosd
Defined in:
lib/ramaze/log/logging.rb

Overview

This module provides a basic skeleton for your own loggers to be compatible. The minimal usage is like this:

class MyLogger
  include Logging

  def log(tag, *args)
    p tag => args
  end
end

Instance Method Summary collapse

Instance Method Details

#debug(*objects) ⇒ Object Also known as: <<

inspects objects if they are no strings. Tag is :debug



45
46
47
# File 'lib/ramaze/log/logging.rb', line 45

def debug(*objects)
  tag_log(:debug, :inspect, *objects)
end

#debug?Boolean

stub for WEBrick

Returns:

  • (Boolean)


84
85
86
# File 'lib/ramaze/log/logging.rb', line 84

def debug?
  false
end

#dev(*objects) ⇒ Object

inspects objects if they are no strings. Tag is :dev



51
52
53
# File 'lib/ramaze/log/logging.rb', line 51

def dev(*objects)
  tag_log(:dev, :inspect, *objects)
end

#error(ex) ⇒ Object

Takes either an Exception or just a String, formats backtraces to be a bit more readable and passes all of this on to tag_log :error



60
61
62
63
64
65
66
67
68
69
# File 'lib/ramaze/log/logging.rb', line 60

def error(ex)
  if ex.respond_to?(:exception)
    message = [ex.backtrace].flatten[0..Global.backtrace_size]
    message.map{|m| m.to_s.gsub(/^#{Regexp.escape(Dir.pwd)}/, '.') }
    message.unshift(ex.inspect)
  else
    message = ex.to_s
  end
  tag_log(:error, :to_s, *message)
end

#info(*objects) ⇒ Object

Converts everything given to strings and passes them on with :info



33
34
35
# File 'lib/ramaze/log/logging.rb', line 33

def info(*objects)
  tag_log(:info, :to_s, *objects)
end

#log(*args) ⇒ Object

raises



73
74
75
# File 'lib/ramaze/log/logging.rb', line 73

def log(*args)
  raise "#log should be implemented by an instance including this module (#{self})"
end

#shutdownObject

nothing



79
80
# File 'lib/ramaze/log/logging.rb', line 79

def shutdown
end

#tag_log(tag, meth, *msgs) ⇒ Object

Takes the tag (:warn|:debug|:error|:info) and the name of a method to be called upon elements of msgs that don’t respond to :to_str Goes on and sends the tag and transformed messages each to the #log method. If you include this module you have to define #log or it will raise.



24
25
26
27
28
29
# File 'lib/ramaze/log/logging.rb', line 24

def tag_log(tag, meth, *msgs)
  msgs.each do |msg|
    string = (msg.respond_to?(:to_str) ? msg : msg.send(meth))
    log(tag, string)
  end
end

#warn(*objects) ⇒ Object

Converts everything given to strings and passes them on with :warn



39
40
41
# File 'lib/ramaze/log/logging.rb', line 39

def warn(*objects)
  tag_log(:warn, :to_s, *objects)
end