Class: BrainzLab::Utilities::LogFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/brainzlab/utilities/log_formatter.rb

Overview

Beautiful log formatter for Rails development Provides colorized, structured output with request timing

Examples:

Usage in Rails

# config/environments/development.rb
config.log_formatter = BrainzLab::Utilities::LogFormatter.new

# Or use the Rails integration
BrainzLab::Utilities::LogFormatter.install!

Constant Summary collapse

COLORS =
{
  debug: "\e[36m",    # Cyan
  info: "\e[32m",     # Green
  warn: "\e[33m",     # Yellow
  error: "\e[31m",    # Red
  fatal: "\e[35m",    # Magenta
  reset: "\e[0m",
  dim: "\e[2m",
  bold: "\e[1m",
  blue: "\e[34m",
  gray: "\e[90m"
}.freeze
SEVERITY_ICONS =
{
  'DEBUG' => '🔍',
  'INFO' => 'â„šī¸ ',
  'WARN' => 'âš ī¸ ',
  'ERROR' => '❌',
  'FATAL' => '💀'
}.freeze
HTTP_METHODS =
{
  'GET' => "\e[32m",     # Green
  'POST' => "\e[33m",    # Yellow
  'PUT' => "\e[34m",     # Blue
  'PATCH' => "\e[34m",   # Blue
  'DELETE' => "\e[31m",  # Red
  'HEAD' => "\e[36m",    # Cyan
  'OPTIONS' => "\e[36m"  # Cyan
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colorize: nil, show_timestamp: true, show_severity: true, compact: false) ⇒ LogFormatter

Returns a new instance of LogFormatter.



47
48
49
50
51
52
53
# File 'lib/brainzlab/utilities/log_formatter.rb', line 47

def initialize(colorize: nil, show_timestamp: true, show_severity: true, compact: false)
  super()
  @colorize = colorize.nil? ? $stdout.tty? : colorize
  @show_timestamp = show_timestamp
  @show_severity = show_severity
  @compact = compact
end

Class Method Details

.install!Object

Install as Rails logger formatter



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/brainzlab/utilities/log_formatter.rb', line 66

def self.install!
  return unless defined?(Rails)

  Rails.application.configure do
    config.log_formatter = BrainzLab::Utilities::LogFormatter.new(
      colorize: BrainzLab.configuration.log_formatter_colors,
      compact: BrainzLab.configuration.log_formatter_compact_assets
    )
  end

  # Also hook into ActiveSupport::TaggedLogging if present
  return unless defined?(ActiveSupport::TaggedLogging) && Rails.logger.respond_to?(:formatter=)

  Rails.logger.formatter = new
end

Instance Method Details

#call(severity, timestamp, progname, msg) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/brainzlab/utilities/log_formatter.rb', line 55

def call(severity, timestamp, progname, msg)
  return '' if msg.nil? || msg.to_s.strip.empty?

  message = format_message(msg)
  return '' if skip_message?(message)

  formatted = build_output(severity, timestamp, progname, message)
  "#{formatted}\n"
end