Skip to content

Instantly share code, notes, and snippets.

@mauriciomdea
Forked from romanbsd/colored_logger.rb
Created December 24, 2011 08:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauriciomdea/1516778 to your computer and use it in GitHub Desktop.
Save mauriciomdea/1516778 to your computer and use it in GitHub Desktop.
Color logging for Mongoid
require 'logger'
class ColoredLogger < Logger
WHITE = "\e[37m"
CYAN = "\e[36m"
MAGENTA = "\e[35m"
BLUE = "\e[34m"
YELLOW = "\e[33m"
GREEN = "\e[32m"
RED = "\e[31m"
BLACK = "\e[30m"
BOLD = "\e[1m"
CLEAR = "\e[0m"
def color(text, color, bold=false)
color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
bold = bold ? BOLD : ""
"#{bold}#{color}#{text}#{CLEAR}"
end
def odd?
@odd_or_even = ! @odd_or_even
end
%w[debug info warn error fatal].each do |method|
define_method method do |message|
message = message.sub('MONGODB', color('MONGODB', odd? ? CYAN : MAGENTA)).
sub(%r{(?<=\[')([^']+)}) {|m| color(m, BLUE)}.
sub(%r{(?<=\]\.)\w+}) {|m| color(m, YELLOW)}
super(message)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment