Module: Padrino::Logger::Extensions
- Included in:
- Padrino::Logger
- Defined in:
- padrino-core/lib/padrino-core/logger.rb
Constant Summary collapse
- SOURCE_LOCATION_REGEXP =
/^(.*?):(\d+?)(?::in `.+?')?$/
Instance Method Summary collapse
-
#bench(action, began_at, message, level = :debug, color = :yellow) ⇒ Object
Append a to development logger a given action with time.
-
#colorize(string, *colors) ⇒ Object
Colorizes a string for colored console output.
-
#colorize! ⇒ Object
Turns a logger with LoggingExtensions into a logger with colorized output.
-
#enable_source_location? ⇒ Boolean
Returns true if :source_location is set to true.
-
#exception(boom, verbosity = :long, level = :error) ⇒ Object
Logs an exception.
-
#format(message, level) ⇒ Object
Formats the log message.
-
#name ⇒ Object
Generate the logging methods for Padrino.logger for each log level.
-
#push(message = nil, level = nil) ⇒ Object
Appends a message to the log.
-
#resolve_source_location(message) ⇒ Object
Resolves a filename and line-number from caller.
-
#stylized_level(level) ⇒ Object
The debug level, with some style added.
Instance Method Details
#bench(action, began_at, message, level = :debug, color = :yellow) ⇒ Object
Append a to development logger a given action with time.
136 137 138 139 140 141 142 143 144 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 136 def bench(action, began_at, , level = :debug, color = :yellow) @_pad ||= 8 @_pad = action.to_s.size if action.to_s.size > @_pad duration = Time.now - began_at color = :red if duration > 1 action = colorize(action.to_s.upcase.rjust(@_pad), color) duration = colorize(Kernel.format('%0.4fs', duration), color, :bold) push "#{action} (#{duration}) #{message}", level end |
#colorize(string, *colors) ⇒ Object
Colorizes a string for colored console output. This is a noop and can be reimplemented to colorize the string as needed.
199 200 201 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 199 def colorize(string, *colors) string end |
#colorize! ⇒ Object
Turns a logger with LoggingExtensions into a logger with colorized output.
210 211 212 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 210 def colorize! self.extend(Colorize) end |
#enable_source_location? ⇒ Boolean
Returns true if :source_location is set to true.
102 103 104 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 102 def enable_source_location? respond_to?(:source_location?) && source_location? end |
#exception(boom, verbosity = :long, level = :error) ⇒ Object
Logs an exception.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 226 def exception(boom, verbosity = :long, level = :error) return unless Levels.key?(level) text = ["#{boom.class} - #{boom.message}:"] trace = boom.backtrace if trace.is_a?(Array) case verbosity when :long then text += trace when :short then text << trace.first end end send level, text.join("\n ") end |
#format(message, level) ⇒ Object
Formats the log message. This method is a noop and should be implemented by other logger components such as Padrino::Logger.
170 171 172 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 170 def format(, level) end |
#name ⇒ Object
Generate the logging methods for Padrino.logger for each log level.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 81 Padrino::Logger::Levels.each_pair do |name, number| define_method(name) do |*args| return if number < level if args.size > 1 bench(args[0], args[1], args[2], name) else location = resolve_source_location(caller(1).shift) args.unshift(location) if location && enable_source_location? push(args * '', name) end end define_method(:"#{name}?") { number >= level } end |
#push(message = nil, level = nil) ⇒ Object
Appends a message to the log. The methods yield to an optional block and the output of this block will be appended to the message.
157 158 159 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 157 def push( = nil, level = nil) add(Padrino::Logger::Levels[level], format(, level)) end |
#resolve_source_location(message) ⇒ Object
Resolves a filename and line-number from caller.
109 110 111 112 113 114 115 116 117 118 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 109 def resolve_source_location() path, line = *.scan(SOURCE_LOCATION_REGEXP).first return unless path && line root = Padrino.root path = File.realpath(path) if Pathname.new(path).relative? return unless path.start_with?(root) && !path.start_with?(Padrino.root('vendor')) "[#{path.gsub("#{root}/", '')}:#{line}] " end |
#stylized_level(level) ⇒ Object
The debug level, with some style added. May be reimplemented.
183 184 185 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 183 def stylized_level(level) level.to_s.upcase.rjust(7) end |