Skip to content

Commit

Permalink
Colorize logs in dev (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
santib authored Nov 13, 2024
1 parent 1d89062 commit 7ed2703
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 50 additions & 0 deletions config/initializers/colorized_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

if Rails.env.local?
# This initializer adds color to the Rails logger output. It's a nice way to
# visually distinguish log levels.
module ColorizedLogger
COLOR_CODES = {
debug: "\e[36m", # Cyan
info: "\e[32m", # Green
warn: "\e[33m", # Yellow
error: "\e[31m", # Red
fatal: "\e[35m", # Magenta
unknown: "\e[37m" # White (or terminal default)
}.freeze

RESET = "\e[0m"

def debug(progname = nil, &)
super(colorize(:debug, progname, &))
end

def info(progname = nil, &)
super(colorize(:info, progname, &))
end

def warn(progname = nil, &)
super(colorize(:warn, progname, &))
end

def error(progname = nil, &)
super(colorize(:error, progname, &))
end

def fatal(progname = nil, &)
super(colorize(:fatal, progname, &))
end

def unknown(progname = nil, &)
super(colorize(:unknown, progname, &))
end

private

def colorize(level, message, &block)
"#{COLOR_CODES[level]}#{message || block&.call}#{RESET}"
end
end

Rails.logger.extend(ColorizedLogger)
end
2 changes: 1 addition & 1 deletion config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

Rails.application.configure do
config.lograge.enabled = true
config.lograge.enabled = Rails.env.production?
config.lograge.base_controller_class = ['ActionController::API', 'ActionController::Base']
config.lograge.custom_payload do |controller|
request = controller.request
Expand Down

0 comments on commit 7ed2703

Please sign in to comment.