| SIMPLE_FORMAT | = | "%5s: %s\n" |
[ show source ]
# File lib/glue/logger.rb, line 142
142: def self.debug(str)
143: @@global_logger.debug(str)
144: end
[ show source ]
# File lib/glue/logger.rb, line 146
146: def self.error(str)
147: @@global_logger.error(str)
148: end
[ show source ]
# File lib/glue/logger.rb, line 130
130: def self.get
131: @@global_logger
132: end
[ show source ]
# File lib/glue/logger.rb, line 138
138: def self.info(str)
139: @@global_logger.info(str)
140: end
Set the global Logger.
[ show source ]
# File lib/glue/logger.rb, line 116
116: def self.set(logger)
117: if logger.is_a?(String) || logger.is_a?(IO)
118: @@global_logger = Logger.new(logger)
119: elsif logger.is_a?(Logger)
120: @@global_logger = logger
121: else
122: raise ArgumentError
123: end
124:
125: @@global_logger.setup_format do |severity, timestamp, progname, msg|
126: SIMPLE_FORMAT % [severity, msg]
127: end
128: end
[ show source ]
# File lib/glue/logger.rb, line 134
134: def self.warn(str)
135: @@global_logger.warn(str)
136: end
Dictate the way in which this logger should format the messages it displays. This method requires a block. The block should return formatted strings given severity, timestamp, msg, progname.
Example
logger = Logger.new logger.setup_format do |severity, timestamp, msg, progname|
"#{progname}@#{timestamp} - #{severity}::#{msg}"
end
[ show source ]
# File lib/glue/logger.rb, line 88
88: def setup_format(&format_proc)
89: raise "Formating block needed" unless format_proc
90: @format_proc = format_proc
91: end
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 <gsinclair@soyabean.com.au>
[ show source ]
# File lib/glue/logger.rb, line 44
44: def trace(expr, style=:p)
45: unless expr.respond_to? :to_str
46: warn "trace: Can't evaluate the given value: #{caller.first}"
47: else
48: require 'facets/core/binding/self/of_caller'
49:
50: Binding.of_caller do |b|
51: value = b.eval(expr.to_str)
52: formatter = TRACE_STYLES[style] || :inspect
53: case formatter
54: when :pp then require 'pp'
55: when :y, :yaml, :to_yaml then require 'yaml'
56: end
57: value_s = value.send(formatter)
58: message = "#{expr} = #{value_s}"
59: lines = message.split(/\n/)
60: indent = " "
61: debug(lines.shift)
62: lines.each do |line|
63: debug(indent + line)
64: end
65: end
66: end
67: end