Sha256: fe67050c650d8ce602862b6cd4271b0d617888e92ad3652430a050db877ae5a6

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require "logger"
require "colorized_string"

module Ductr
  module Log
    #
    # A log formatter which colorize the text with ANSI colors.
    #
    class ColorFormatter < ::Logger::Formatter
      #
      # Colorizes the given log entry.
      #
      # @param [Integer] level The log's severity level
      # @param [Time] time The log's timestamp
      # @param [Symbol] prog_name The log's "program" name, used to add job method name to the log
      # @param [String] message The log's message
      #
      # @return [String] The formatted log
      #
      def call(level, time, prog_name, message)
        format(format_str(level), level[0], format_datetime(time), Process.pid, level, prog_name, msg2str(message))
      end

      private

      #
      # Generates the colorized format string based on the log level.
      #
      # @param [String] level The log level
      #
      # @return [String] The colored format string
      #
      def format_str(level)
        colors = {
          "DEBUG" => :green, "INFO" => :cyan, "WARN" => :yellow, "ERROR" => :red, "FATAL" => { background: :red }
        }

        timestamp = ColorizedString["%s, [%s #%d]"].colorize(:light_black)
        level_name = ColorizedString["%5s"].colorize(colors[level])
        prog_name = ColorizedString["%s:"].colorize(:blue)

        "#{timestamp} #{level_name} -- #{prog_name} %s\n"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ductr-0.2.3 lib/ductr/log/formatters/color_formatter.rb
ductr-0.2.2 lib/ductr/log/formatters/color_formatter.rb
ductr-0.2.1 lib/ductr/log/formatters/color_formatter.rb
ductr-0.2.0 lib/ductr/log/formatters/color_formatter.rb
ductr-0.1.2 lib/ductr/log/formatters/color_formatter.rb
ductr-0.1.1 lib/ductr/log/formatters/color_formatter.rb
ductr-0.1.0 lib/ductr/log/formatters/color_formatter.rb