Skip to content

Instantly share code, notes, and snippets.

@ab
Forked from janlelis/logger-colors.rb
Created July 23, 2012 01:53
Show Gist options
  • Select an option

  • Save ab/3161648 to your computer and use it in GitHub Desktop.

Select an option

Save ab/3161648 to your computer and use it in GitHub Desktop.

Revisions

  1. ab revised this gist Jul 23, 2012. 1 changed file with 18 additions and 16 deletions.
    34 changes: 18 additions & 16 deletions logger-colors.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,8 @@
    # Colorizes the output of the standard library logger, depending on the logger level:
    # To adjust the colors, look at Logger::Colors::SCHEMA and Logger::Colors::constants

    require 'logger'

    class Logger
    class ColoredLogger < Logger
    module Colors
    VERSION = '1.0.0'
    VERSION = '1.0.1'

    NOTHING = '0;0'
    BLACK = '0;30'
    @@ -27,27 +24,32 @@ module Colors

    SCHEMA = {
    STDOUT => %w[nothing green brown red purple cyan],
    STDERR => %w[nothing green yellow light_red light_purple light_cyan],
    #STDERR => %w[nothing green yellow light_red light_purple light_cyan],
    STDERR => %w[dark_gray nothing yellow light_red light_purple light_cyan],
    }
    end
    end

    class Logger

    alias format_message_colorless format_message

    def format_message(level, *args)
    if Logger::Colors::SCHEMA[@logdev.dev] && @logdev.dev.tty?
    color = begin
    Logger::Colors.const_get \
    Logger::Colors::SCHEMA[@logdev.dev][Logger.const_get(level.sub "ANY","UNKNOWN")].to_s.upcase
    if self.class::Colors::SCHEMA[@logdev.dev] && @logdev.dev.tty?
    begin
    index = self.class.const_get(level.sub('ANY','UNKNOWN'))
    color_name = self.class::Colors::SCHEMA[@logdev.dev][index]
    color = self.class::Colors.const_get(color_name.to_s.upcase)
    rescue NameError
    "0;0"
    color = '0;0'
    end
    "\e[#{ color }m#{ format_message_colorless(level, *args) }\e[0;0m"
    "\e[#{color}m#{format_message_colorless(level, *args)}\e[0;0m"
    else
    format_message_colorless(level, *args)
    end
    end
    end

    # J-_-L
    def rainbow(*args)
    SEV_LABEL.each_with_index do |level, i|
    add(i, *args)
    end
    end
    end
  2. ab revised this gist Jul 23, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logger-colors.rb
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ class Logger
    alias format_message_colorless format_message

    def format_message(level, *args)
    if Logger::Colors::SCHEMA[@logdev.dev]
    if Logger::Colors::SCHEMA[@logdev.dev] && @logdev.dev.tty?
    color = begin
    Logger::Colors.const_get \
    Logger::Colors::SCHEMA[@logdev.dev][Logger.const_get(level.sub "ANY","UNKNOWN")].to_s.upcase
  3. @janlelis janlelis created this gist May 9, 2011.
    53 changes: 53 additions & 0 deletions logger-colors.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    # Colorizes the output of the standard library logger, depending on the logger level:
    # To adjust the colors, look at Logger::Colors::SCHEMA and Logger::Colors::constants

    require 'logger'

    class Logger
    module Colors
    VERSION = '1.0.0'

    NOTHING = '0;0'
    BLACK = '0;30'
    RED = '0;31'
    GREEN = '0;32'
    BROWN = '0;33'
    BLUE = '0;34'
    PURPLE = '0;35'
    CYAN = '0;36'
    LIGHT_GRAY = '0;37'
    DARK_GRAY = '1;30'
    LIGHT_RED = '1;31'
    LIGHT_GREEN = '1;32'
    YELLOW = '1;33'
    LIGHT_BLUE = '1;34'
    LIGHT_PURPLE = '1;35'
    LIGHT_CYAN = '1;36'
    WHITE = '1;37'

    SCHEMA = {
    STDOUT => %w[nothing green brown red purple cyan],
    STDERR => %w[nothing green yellow light_red light_purple light_cyan],
    }
    end
    end

    class Logger
    alias format_message_colorless format_message

    def format_message(level, *args)
    if Logger::Colors::SCHEMA[@logdev.dev]
    color = begin
    Logger::Colors.const_get \
    Logger::Colors::SCHEMA[@logdev.dev][Logger.const_get(level.sub "ANY","UNKNOWN")].to_s.upcase
    rescue NameError
    "0;0"
    end
    "\e[#{ color }m#{ format_message_colorless(level, *args) }\e[0;0m"
    else
    format_message_colorless(level, *args)
    end
    end
    end

    # J-_-L