module Tennpipes::Logger::Extensions

Constants

SOURCE_LOCATION_REGEXP

Public Instance Methods

bench(action, began_at, message, level=:debug, color=:yellow) click to toggle source

Append a to development logger a given action with time.

@param [string] action

The action.

@param [float] time

Time duration for the given action.

@param [message] string

The message that you want to log.

@example

logger.bench 'GET', started_at, '/blog/categories'
# => DEBUG - GET (0.0056s) - /blog/categories
# File lib/tennpipes-base/logger.rb, line 125
def bench(action, began_at, message, 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('%0.4fs' % duration, color, :bold)
  push "#{action} (#{duration}) #{message}", level
end
colorize(string, *colors) click to toggle source

Colorizes a string for colored console output. This is a noop and can be reimplemented to colorize the string as needed.

@see

ColorizedLogger

@param [string]

The string to be colorized.

@param [Array<Symbol>]

The colors to use. Should be applied in the order given.
# File lib/tennpipes-base/logger.rb, line 188
def colorize(string, *colors)
  string
end
colorize!() click to toggle source

Turns a logger with LoggingExtensions into a logger with colorized output.

@example

Tennpipes.logger = Logger.new($stdout)
Tennpipes.logger.colorize!
Tennpipes.logger.debug("Fancy Tennpipes debug string")
# File lib/tennpipes-base/logger.rb, line 199
def colorize!
  self.extend(Colorize)
end
enable_source_location?() click to toggle source

Returns true if :source_location is set to true.

# File lib/tennpipes-base/logger.rb, line 92
def enable_source_location?
  respond_to?(:source_location?) && source_location?
end
exception(boom, verbosity = :long, level = :error) click to toggle source

Logs an exception.

@param [Exception] exception

The exception to log

@param [Symbol] verbosity

:short or :long, default is :long

@example

Tennpipes.logger.exception e
Tennpipes.logger.exception(e, :short)
# File lib/tennpipes-base/logger.rb, line 215
def exception(boom, verbosity = :long, level = :error)
  return unless Levels.has_key?(level)
  text = ["#{boom.class} - #{boom.message}:"]
  trace = boom.backtrace
  case verbosity
  when :long
    text += trace
  when :short
    text << trace.first
  end if trace.kind_of?(Array)
  send level, text.join("\n ")
end
format(message, level) click to toggle source

Formats the log message. This method is a noop and should be implemented by other logger components such as {Tennpipes::Logger}.

@param [String] message

The message to format.

@param [String,Symbol] level

The log level, one of :debug, :warn ...
# File lib/tennpipes-base/logger.rb, line 159
def format(message, level)
  message
end
push(message = nil, level = nil) click to toggle source

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.

@param [String] message

The message that you want write to your stream.

@param [String] level

The level one of :debug, :warn etc. ...
# File lib/tennpipes-base/logger.rb, line 146
def push(message = nil, level = nil)
  add(Tennpipes::Logger::Levels[level], format(message, level))
end
resolve_source_location(message) click to toggle source

Resolves a filename and line-number from caller.

# File lib/tennpipes-base/logger.rb, line 99
def resolve_source_location(message)
  path, line = *message.scan(SOURCE_LOCATION_REGEXP).first
  return unless path && line
  root = Tennpipes.root
  path = File.realpath(path) if Pathname.new(path).relative?
  if path.start_with?(root) && !path.start_with?(Tennpipes.root("vendor"))
    "[#{path.gsub("#{root}/", "")}:#{line}] "
  end
end
stylized_level(level) click to toggle source

The debug level, with some style added. May be reimplemented.

@example

stylized_level(:debug) => DEBUG

@param [String,Symbol] level

The log level.
# File lib/tennpipes-base/logger.rb, line 172
def stylized_level(level)
  level.to_s.upcase.rjust(7)
end