Skip to content

Commit

Permalink
Introduce Padrino::Logging Module
Browse files Browse the repository at this point in the history
  • Loading branch information
skade committed Dec 23, 2011
1 parent f7b3bd3 commit 2f35dd9
Showing 1 changed file with 134 additions and 139 deletions.
273 changes: 134 additions & 139 deletions padrino-core/lib/padrino-core/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,132 @@
PADRINO_LOGGER = ENV['PADRINO_LOGGER'] unless defined?(PADRINO_LOGGER)

module Padrino
module Logging
##
# Ruby (standard) logger levels:
#
# :fatal:: An unhandleable error that results in a program crash
# :error:: A handleable error condition
# :warn:: A warning
# :info:: generic (useful) information about system operation
# :debug:: low-level information for developers
#
Levels = {
:fatal => 7,
:error => 6,
:warn => 4,
:info => 3,
:debug => 0,
:devel => -1,
} unless const_defined?(:Levels)

module LoggingExtensions

##
# Generate the logging methods for {Padrino.logger} for each log level.
#
Padrino::Logging::Levels.each_pair do |name, number|
define_method(name) do |*args|
return if number < level
if args.size > 1
bench(*args)
else
push(args * '', name)
end
end

define_method(:"#{name}?") do
number >= level
end
end

##
# 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.056ms) - /blog/categories
#
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
push "%s (" % colorize(action.to_s.upcase.rjust(@_pad), color) + colorize("%0.4fms", :bold, color) % duration + ") %s" % message.to_s, level
end

##
# 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...
#
#
def push(message = nil, level = nil)
add(level, format_message(message, level))
end

def format_message(message, level)
message
end

##
# Colorize our level
#
# @param [String, Symbol] level
#
# @see Padrino::Logger::ColoredLevels
#
def stylized_level(level)
level.to_s.upcase.rjust(7)
end

def colorize(string, *colors)
string
end

def colorize!
self.extend(ColorizedLogger)
end
end

module ColorizedLogger
# Colors for levels
ColoredLevels = {
:fatal => [:bold, :red],
:error => [:red],
:warn => [:yellow],
:info => [:green],
:debug => [:cyan],
:devel => [:magenta]
} unless defined?(ColoredLevels)

def colorize(string, *colors)
colors.each do |c|
string = string.send(c)
end
string
end

def stylized_level(level)
style = ColoredLevels[level].map { |c| "\e[%dm" % String.colors[c] } * ''
[style, super, "\e[0m"] * ''
end
end
end

##
# @return [Padrino::Logger]
Expand Down Expand Up @@ -35,14 +161,16 @@ def self.logger
# Padrino.logger = Buffered.new(STDOUT)
#
def self.logger=(value)
value.extend(Padrino::LoggingExtensions) unless (Padrino::LoggingExtensions === value)
value.extend(Padrino::Logging::LoggingExtensions) unless (Padrino::Logging::LoggingExtensions === value)
Thread.current[:padrino_logger] = value
end

##
# Extensions to the built in Ruby logger.
#
class Logger
include Logging::LoggingExtensions
include Logging::ColorizedLogger

attr_accessor :level
attr_accessor :auto_flush
Expand All @@ -51,24 +179,6 @@ class Logger
attr_reader :init_args
attr_accessor :log_static

##
# Ruby (standard) logger levels:
#
# :fatal:: An unhandleable error that results in a program crash
# :error:: A handleable error condition
# :warn:: A warning
# :info:: generic (useful) information about system operation
# :debug:: low-level information for developers
#
Levels = {
:fatal => 7,
:error => 6,
:warn => 4,
:info => 3,
:debug => 0,
:devel => -1,
} unless const_defined?(:Levels)

@@mutex = {}

##
Expand Down Expand Up @@ -165,7 +275,7 @@ def self.setup!
def initialize(options={})
@buffer = []
@auto_flush = options.has_key?(:auto_flush) ? options[:auto_flush] : true
@level = options[:log_level] ? Levels[options[:log_level]] : Levels[:debug]
@level = options[:log_level] ? Logging::Levels[options[:log_level]] : Logging::Levels[:debug]
@log = options[:stream] || $stdout
@log.sync = true
@mutex = @@mutex[@log] ||= Mutex.new
Expand Down Expand Up @@ -213,6 +323,10 @@ def <<(message = nil)
end
alias :write :<<

def format_message(message, level)
@format_message % [stylized_level(level), colorize(Time.now.strftime(@format_datetime), :yellow), message.to_s.strip]
end

##
# Padrino::Loggger::Rack forwards every request to an +app+ given, and
# logs a line in the Apache common log format to the +logger+, or
Expand Down Expand Up @@ -258,125 +372,6 @@ def code_to_name(status)
end
end # Rack
end # Logger

module LoggingExtensions
##
# Generate the logging methods for {Padrino.logger} for each log level.
#
Padrino::Logger::Levels.each_pair do |name, number|
define_method(name) do |*args|
return if number < level
if args.size > 1
bench(*args)
else
push(args * '', name)
end
end

define_method(:"#{name}?") do
number >= level
end
end

##
# 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.056ms) - /blog/categories
#
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
push "%s (" % colorize(action.to_s.upcase.rjust(@_pad), color) + colorize("%0.4fms", :bold, color) % duration + ") %s" % message.to_s, level
end

##
# 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...
#
#
def push(message = nil, level = nil)
add level, format_message(message, level)
end

def format_message(message, level)
message
end

##
# Colorize our level
#
# @param [String, Symbol] level
#
# @see Padrino::Logger::ColoredLevels
#
def stylized_level(level)
level.to_s.upcase.rjust(7)
end

def colorize(string, *colors)
string
end

def colorize!
self.extend(ColorizedLogger)
end
end

module ColorizedLogger
# Colors for levels
ColoredLevels = {
:fatal => [:bold, :red],
:error => [:red],
:warn => [:yellow],
:info => [:green],
:debug => [:cyan],
:devel => [:magenta]
} unless defined?(ColoredLevels)

def colorize(string, *colors)
colors.each do |c|
string = string.send(c)
end
string
end

def stylized_level(level)
style = ColoredLevels[level].map { |c| "\e[%dm" % String.colors[c] } * ''
[style, super, "\e[0m"] * ''
end
end

module LogFormatter
def format_message(message, level)
@format_message % [stylized_level(level), colorize(Time.now.strftime(@format_datetime), :yellow), message.to_s.strip]
end
end

class Logger
include LoggingExtensions
include ColorizedLogger
include LogFormatter
end

end # Padrino

module Kernel # @private
Expand Down

0 comments on commit 2f35dd9

Please sign in to comment.