Last active
January 8, 2025 07:07
-
-
Save kyrylo/3d90f7a656d1a0accf244b8f1d25999b to your computer and use it in GitHub Desktop.
Nice colorized logs for Rails apps! With this initializer, you can instantly colorize your Rails development logs. Just copy and paste the code, and it’ll work. https://x.com/kyrylosilin/status/1852308566201237815
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# config/initializers/colorized_logger.rb | |
# 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, &block) | |
super(colorize(:debug, progname, &block)) | |
end | |
def info(progname = nil, &block) | |
super(colorize(:info, progname, &block)) | |
end | |
def warn(progname = nil, &block) | |
super(colorize(:warn, progname, &block)) | |
end | |
def error(progname = nil, &block) | |
super(colorize(:error, progname, &block)) | |
end | |
def fatal(progname = nil, &block) | |
super(colorize(:fatal, progname, &block)) | |
end | |
def unknown(progname = nil, &block) | |
super(colorize(:unknown, progname, &block)) | |
end | |
private | |
def colorize(level, message, &block) | |
"#{COLOR_CODES[level]}#{message || (block && block.call)}#{RESET}" | |
end | |
end | |
Rails.logger.extend(ColorizedLogger) |
Thanks for the tip, i changed it a bit, but remains the same in terms of features.
# frozen_string_literal: true
module ColorizedLogger
RESET = "\e[0m"
COLOR_CODES = {
unknown: RESET, # Terminal default
debug: "\e[0;36m", # Cyan text
info: "\e[0;32m", # Green text
warn: "\e[1;33m", # Yellow text
error: "\e[1;31m", # Red text
fatal: "\e[1;31m", # Bold Red text
}.freeze
%i[debug info warn error fatal unknown].each do |level|
define_method(level) { |progname = nil, &block| super(colorize_message(level, progname || (block && block.call))) }
end
private
def colorize_message(level, message)
"#{COLOR_CODES[level]}#{message}#{RESET}"
end
end
Rails.logger.extend(ColorizedLogger)
@navidemad If you like code golfing, here's an even shorter version:
# frozen_string_literal: true
module ColorizedLogger
%i[debug info warn error fatal unknown].each do |level|
color = case level
when :debug then "\e[0;36m" # Cyan text
when :info then "\e[0;32m" # Green text
when :warn then "\e[1;33m" # Yellow text
when :error, :fatal then "\e[1;31m" # Red text
else "\e[0m" # Terminal default
end
define_method(level) do |progname = nil, &block|
super(color + (progname || (block && block.call)).to_s + "\e[0m")
end
end
end
Rails.logger.extend(ColorizedLogger)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What’s great about it is that you can use the default Rails logger as you normally would. For example, if you want to log an error, just use
Rails.logger.error("there was a problem")
, and it will print the message in red.