Permalink
Browse files

Refactored the code, so it works with Mongoid 3.x too.

  • Loading branch information...
1 parent d4eb872 commit 70187ac497e94c8aec46404b1428be5984169fc5 @ream88 ream88 committed Oct 31, 2012
View
@@ -1,6 +1,6 @@
source "http://rubygems.org"
-gem 'mongoid', '~>2.4'
+gem 'mongoid'
gem 'mongo', '~>1.6'
group :development do
View
@@ -36,5 +36,5 @@ DEPENDENCIES
bundler (~> 1.0)
jeweler (~> 1.8.0)
mongo (~> 1.6)
- mongoid (~> 2.4)
+ mongoid
rdoc (~> 3.12)
@@ -1,15 +1,5 @@
module MongoidColoredLogger
class LoggerDecorator
- module Severity
- DEBUG = 0
- INFO = 1
- WARN = 2
- ERROR = 3
- FATAL = 4
- UNKNOWN = 5
- end
- include Severity
-
WHITE = "\e[37m"
CYAN = "\e[36m"
MAGENTA = "\e[35m"
@@ -26,28 +16,23 @@ def initialize(logger)
@logger = logger
end
- def add(severity, message = nil, progname = nil, &block)
- message = block.call if message.nil? and block_given?
- message = message.to_s.sub('MONGODB', color('MONGODB', odd? ? CYAN : MAGENTA)).
- sub(%r{(?<=\[')([^']+)}) {|m| color(m, BLUE)}.
- sub(%r{(?<=\]\.)\w+}) {|m| color(m, YELLOW)}
- @logger.add(severity, message, progname, &block)
- end
-
- %w[debug info warn error fatal unknown].each do |method|
- class_eval <<-EOT, __FILE__, __LINE__ + 1
- def #{method}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
- add(#{method.upcase}, message, progname, &block) # add(DEBUG, message, progname, &block)
- end # end
- EOT
+ %w[debug info warn error fatal unknown].each.with_index do |method, severity|
+ define_method(method) do |message = nil, progname = nil, &block|
+ colorize_method = Mongoid::VERSION.to_f >= 3.0 ? :colorize_message : :colorize_legacy_message
+
+ message = block.call if message.nil? and block_given?
+ message = self.send(colorize_method, message.to_s)
+
+ @logger.add(severity, message, progname, &block)
+ end
end
# Proxy everything else to the logger instance
def respond_to?(method)
super || @logger.respond_to?(method)
end
- private
+ private
def method_missing(method, *args, &block)
@logger.send(method, *args, &block)
end
@@ -58,10 +43,26 @@ def color(text, color, bold=false)
"#{bold}#{color}#{text}#{CLEAR}"
end
+ # Used for Mongoid < 3.0
+ def colorize_legacy_message(message)
+ message.sub('MONGODB', color('MONGODB', odd? ? CYAN : MAGENTA)).
+ sub(%r{(?<=\[')([^']+)}) {|m| color(m, BLUE)}.
+ sub(%r{(?<=\]\.)\w+}) {|m| color(m, YELLOW)}
+ end
+
+ # Used for Mongoid >= 3.0
+ def colorize_message(message)
+ message = message.sub('MONGODB', color('MONGODB', odd? ? CYAN : MAGENTA)).
+ sub(%r{(?<=\[')([^']+)}) {|m| color(m, BLUE)}.
+ sub(%r{(?<=\]\.)\w+}) {|m| color(m, YELLOW)}
+ message.sub('MOPED:', color('MOPED:', odd? ? CYAN : MAGENTA)).
+ sub(/\{.+?\}\s/) { |m| color(m, BLUE) }.
+ sub(/COMMAND|QUERY|KILL_CURSORS/) { |m| color(m, YELLOW) }.
+ sub(/[\d\.]+ms/) { |m| color(m, GREEN) }
+ end
+
def odd?
@odd_or_even = ! @odd_or_even
end
-
end
-
end
@@ -3,14 +3,15 @@
module MongoidColoredLogger
class Railtie < Rails::Railtie
+ base = Mongoid::VERSION.to_f >= 3.0 ? Moped : config.mongoid
+
initializer 'logger_decorator', :after => :initialize_logger do
- config.mongoid.logger = MongoidColoredLogger::LoggerDecorator.new(Rails.logger)
+ base.logger = MongoidColoredLogger::LoggerDecorator.new(Rails.logger)
end
# Make it output to STDERR in console
console do |app|
- config.mongoid.logger = MongoidColoredLogger::LoggerDecorator.new(Logger.new(STDERR))
+ base.logger = MongoidColoredLogger::LoggerDecorator.new(Logger.new(STDERR))
end
end
-
end
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Roman Shterenzon"]
- s.date = "2012-09-10"
+ s.date = "2012-10-31"
s.description = "Beautiful logging for Mongoid"
s.email = "romanbsd@yahoo.com"
s.extra_rdoc_files = [
@@ -33,27 +33,27 @@ Gem::Specification.new do |s|
s.homepage = "http://github.com/romanbsd/mongoid_colored_logger"
s.licenses = ["MIT"]
s.require_paths = ["lib"]
- s.rubygems_version = "1.8.24"
+ s.rubygems_version = "1.8.23"
s.summary = "Beautiful logging for Mongoid"
if s.respond_to? :specification_version then
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<mongoid>, ["~> 2.4"])
+ s.add_runtime_dependency(%q<mongoid>, [">= 0"])
s.add_runtime_dependency(%q<mongo>, ["~> 1.6"])
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.8.0"])
else
- s.add_dependency(%q<mongoid>, ["~> 2.4"])
+ s.add_dependency(%q<mongoid>, [">= 0"])
s.add_dependency(%q<mongo>, ["~> 1.6"])
s.add_dependency(%q<rdoc>, ["~> 3.12"])
s.add_dependency(%q<bundler>, ["~> 1.0"])
s.add_dependency(%q<jeweler>, ["~> 1.8.0"])
end
else
- s.add_dependency(%q<mongoid>, ["~> 2.4"])
+ s.add_dependency(%q<mongoid>, [">= 0"])
s.add_dependency(%q<mongo>, ["~> 1.6"])
s.add_dependency(%q<rdoc>, ["~> 3.12"])
s.add_dependency(%q<bundler>, ["~> 1.0"])

0 comments on commit 70187ac

Please sign in to comment.