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 `.+?')?$/.freeze

Instance Method Summary collapse

Instance Method Details

#bench(action, began_at, message, level = :debug, color = :yellow) ⇒ Object

Append a to development logger a given action with time.

Examples:

logger.bench 'GET', started_at, '/blog/categories'
# => DEBUG - GET (0.0056s) - /blog/categories

Parameters:

  • action (string)

    The action.

  • time (float)

    Time duration for the given action.

  • string (message)

    The message that you want to log.



134
135
136
137
138
139
140
141
142
# File 'padrino-core/lib/padrino-core/logger.rb', line 134

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) ⇒ Object

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

Parameters:

  • The (string)

    string to be colorized.

  • The (Array<Symbol>)

    colors to use. Should be applied in the order given.

See Also:

  • ColorizedLogger


197
198
199
# File 'padrino-core/lib/padrino-core/logger.rb', line 197

def colorize(string, *colors)
  string
end

#colorize!Object

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

Examples:

Padrino.logger = Logger.new($stdout)
Padrino.logger.colorize!
Padrino.logger.debug("Fancy Padrino debug string")


208
209
210
# File 'padrino-core/lib/padrino-core/logger.rb', line 208

def colorize!
  self.extend(Colorize)
end

#enable_source_location?Boolean

Returns true if :source_location is set to true.

Returns:

  • (Boolean)


101
102
103
# File 'padrino-core/lib/padrino-core/logger.rb', line 101

def enable_source_location?
  respond_to?(:source_location?) && source_location?
end

#exception(boom, verbosity = :long, level = :error) ⇒ Object

Logs an exception.

Examples:

Padrino.logger.exception e
Padrino.logger.exception(e, :short)

Parameters:

  • exception (Exception)

    The exception to log

  • verbosity (Symbol) (defaults to: :long)

    :short or :long, default is :long



224
225
226
227
228
229
230
231
232
233
234
235
# File 'padrino-core/lib/padrino-core/logger.rb', line 224

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) ⇒ Object

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

Parameters:

  • message (String)

    The message to format.

  • level (String, Symbol)

    The log level, one of :debug, :warn …



168
169
170
# File 'padrino-core/lib/padrino-core/logger.rb', line 168

def format(message, level)
  message
end

#nameObject

Generate the logging methods for Padrino.logger for each log level.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'padrino-core/lib/padrino-core/logger.rb', line 78

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
      if location = resolve_source_location(caller(1).shift)
        args.unshift(location)
      end if enable_source_location?
      push(args * '', name)
    end
  end

  define_method(:"#{name}?") do
    number >= level
  end
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.

Parameters:

  • message (String) (defaults to: nil)

    The message that you want write to your stream.

  • level (String) (defaults to: nil)

    The level one of :debug, :warn etc. …



155
156
157
# File 'padrino-core/lib/padrino-core/logger.rb', line 155

def push(message = nil, level = nil)
  add(Padrino::Logger::Levels[level], format(message, level))
end

#resolve_source_location(message) ⇒ Object

Resolves a filename and line-number from caller.



108
109
110
111
112
113
114
115
116
# File 'padrino-core/lib/padrino-core/logger.rb', line 108

def resolve_source_location(message)
  path, line = *message.scan(SOURCE_LOCATION_REGEXP).first
  return unless path && line
  root = Padrino.root
  path = File.realpath(path) if Pathname.new(path).relative?
  if path.start_with?(root) && !path.start_with?(Padrino.root("vendor"))
    "[#{path.gsub("#{root}/", "")}:#{line}] "
  end
end

#stylized_level(level) ⇒ Object

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

Examples:

stylized_level(:debug) => DEBUG

Parameters:

  • level (String, Symbol)

    The log level.



181
182
183
# File 'padrino-core/lib/padrino-core/logger.rb', line 181

def stylized_level(level)
  level.to_s.upcase.rjust(7)
end