Sha256: 4c23e29ba667b73a7e00c204dda2a3d3d72fa5e4498fbc706a01b24c0f2cedfb

Contents?: true

Size: 1.93 KB

Versions: 124

Compression:

Stored size: 1.93 KB

Contents

require_relative 'formatter'

class ReeLogger::ColorizedFormatter < ReeLogger::Formatter
  include Ree::BeanDSL

  bean :colorized_formatter do
    link 'ree_logger/log_event', -> { LogEvent }
    link :not_blank, from: :ree_object
    link :now, from: :ree_datetime
  end

  contract LogEvent, Nilor[String] => String
  def format(event, progname = nil)
    prefix = if progname
      "[#{progname}] "
    else
      ""
    end

    level = "%-5s" % "#{event.level.to_s}:"
    level = colorize_by_level(level, event.level)
    out = "#{prefix}%-6s %s" % ["[#{now.strftime("%d/%m/%y %H:%M:%S")}]", colorize_message(level, event.message)]

    if not_blank(event.parameters)
      out += "#{prefix}\n#{colorize_blue('PARAMETERS:')} #{event.parameters}"
    end

    if event.exception
      backtrace = (event.exception.backtrace || []).join("\n")
      out += "#{prefix}\n#{colorize_red('EXCEPTION:')} #{event.exception.class} (#{event.exception.message})\n#{backtrace}"
    end

    out
  end

  private

  def colorize_message(level, message)
    if message =~ /(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)/
      level = "\n\n#{level}"
      message = colorize_blue(message)
    elsif message =~ /Parameters: /
      message = message.gsub("Parameters:", colorize_blue("Parameters:"))
    elsif message =~ /SELECT/
      message = message.gsub("SELECT", colorize_green("SELECT"))
    elsif message =~ /(INSERT|UPDATE|DELETE)/
      message = message.gsub($1, colorize_red($1))
    end

    "#{level} #{message}"
  end

  def colorize_by_level(string, level)
    out = Rainbow(string)
    case level
    when :info
      out.cyan
    when :debug
      out.yellow
    when :warn
      out.yellow
    when :error
      out.red
    when :fatal
      out.red
    else
      out.red
    end
  end

  def colorize_green(string)
    Rainbow(string).green
  end

  def colorize_blue(string)
    Rainbow(string).blue
  end

  def colorize_red(string)
    Rainbow(string).red
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_logger/package/ree_logger/formatters/colorized_formatter.rb