Class: Logger

Inherits:
Object show all
Defined in:
lib/glue/logger.rb

Overview

A simple wrapper arround the Ruby logger. Mainly for compatibility purposes.

Constant Summary collapse

TRACE_STYLES =

:nodoc:

{}
@@global_logger =

Override the logger with your custom version.

Logger.new(STDERR)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.debug(str) ⇒ Object



94
95
96
# File 'lib/glue/logger.rb', line 94

def self.debug(str)
	@@global_logger.debug(str)
end

.error(str) ⇒ Object



98
99
100
# File 'lib/glue/logger.rb', line 98

def self.error(str)
	@@global_logger.error(str)
end

.getObject



82
83
84
# File 'lib/glue/logger.rb', line 82

def self.get
	@@global_logger
end

.info(str) ⇒ Object



90
91
92
# File 'lib/glue/logger.rb', line 90

def self.info(str)
	@@global_logger.info(str)
end

.set(logger) ⇒ Object



78
79
80
# File 'lib/glue/logger.rb', line 78

def self.set(logger)
	@@global_logger = logger
end

.trace(expr, style = :p) ⇒ Object

– Saddly have to duplicate the code to make Binding.of_caller work. ++



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/glue/logger.rb', line 106

def self.trace(expr, style=:p)
  unless expr.respond_to? :to_str
    warn "trace: Can't evaluate the given value: #{caller.first}"
  else
	require 'extensions/binding'

    Binding.of_caller do |b|
      value = b.eval(expr.to_str)
      formatter = TRACE_STYLES[style] || :inspect
      case formatter
      when :pp then require 'pp'
      when :y, :yaml, :to_yaml then require 'yaml'
      end
      value_s = value.send(formatter)
      message = "#{expr} = #{value_s}"
      lines = message.split(/\n/)
      indent = "   "
      debug(lines.shift)            					
      lines.each do |line|
        debug(indent + line)
      end
    end
  end
end

.warn(str) ⇒ Object



86
87
88
# File 'lib/glue/logger.rb', line 86

def self.warn(str)
	@@global_logger.warn(str)
end

Instance Method Details

#develObject



11
# File 'lib/glue/logger.rb', line 11

alias_method :devel, :debug

#fineObject



12
# File 'lib/glue/logger.rb', line 12

alias_method :fine, :debug

#trace(expr, style = :p) ⇒ Object

Prints a trace message to DEBUGLOG (at debug level).

Useful for emitting the value of variables, etc. Use like this:

x = y = 5
trace 'x'        # -> 'x = 5'
trace 'x ** y'   # -> 'x ** y = 3125'

If you have a more complicated value, like an array of hashes, then you’ll probably want to use an alternative output format. For instance:

trace 'value', :yaml

Valid output format values (the style parameter) are:

:p :inspect
:pp                     (pretty-print, using 'pp' library)
:s :to_s
:y :yaml :to_yaml       (using the 'yaml' library')

The default is :p.

CREDITS:

This code comes straight from the dev-utils Gem. Author: Gavin Sinclair <[email protected]>



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/glue/logger.rb', line 42

def trace(expr, style=:p)
  unless expr.respond_to? :to_str
    warn "trace: Can't evaluate the given value: #{caller.first}"
  else
	require 'extensions/binding'

    Binding.of_caller do |b|
      value = b.eval(expr.to_str)
      formatter = TRACE_STYLES[style] || :inspect
      case formatter
      when :pp then require 'pp'
      when :y, :yaml, :to_yaml then require 'yaml'
      end
      value_s = value.send(formatter)
      message = "#{expr} = #{value_s}"
      lines = message.split(/\n/)
      indent = "   "
      debug(lines.shift)            					
      lines.each do |line|
        debug(indent + line)
      end
    end
  end
end